如何将附加属性设置为布尔值

huangapple go评论68阅读模式
英文:

How to set Additional Properties to boolean

问题

我正在尝试将Additional Properties元素设置到Open API Schema 3.X中,但不幸的是,在文档中我没有找到任何有助于此的信息。
我有一个在Spring Boot中的应用程序,它使用Spring Doc OAS,它依赖于Swagger OAS作为传递性依赖。
让我在这里选取一些代码片段:

@GetMapping("/{accountId}")
@Operation(summary = "通过账户ID获取账户", tags = TAG)
@ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "返回特定路径查询的特定账户",
                content = { @Content(mediaType = "application/json",
                        schema = @Schema(implementation = AccountDetailsDTO.class)) }),
        @ApiResponse(responseCode = "404", description = "未找到账户",
                content = @Content) })
public ResponseEntity<AccountDetailsDTO> getAccountDetailsByClientId(@PathVariable("accountId") Integer accountId) { }

该属性的默认值为true,我想要看到的是将其设置为false,如下所示:

如何将附加属性设置为布尔值

英文:

I am trying to set Additional Properties element into the Open API Schema 3.X but unfortunatel I was not able to find anything in the documentation that help me on it.
I have a Application in Spring boot and it is using Spring doc OAS that relies on Swagger OAS as transitive dependency.
Let me pick some code snippet here:

@GetMapping(&quot;/{accountId}&quot;)
@Operation(summary = &quot;Get account by account id&quot;, tags = TAG)
@ApiResponses(value = {
        @ApiResponse(responseCode = &quot;200&quot;, description = &quot;Return a specific account queried by path&quot;,
                content = { @Content(mediaType = &quot;application/json&quot;,
                        schema = @Schema(implementation = AccountDetailsDTO.class)) }),
        @ApiResponse(responseCode = &quot;404&quot;, description = &quot;No accounts found&quot;,
                content = @Content) })
public ResponseEntity&lt;AccountDetailsDTO&gt; getAccountDetailsByClientId(@PathVariable(&quot;accountId&quot;) Integer accountId) { }

This attribute is default to true and What I would like to see is as false like that below:
如何将附加属性设置为布尔值

答案1

得分: 6

如果您想显式地将属性设置为 false您可以使用 TransformationFilter使用 Spring 的 @Component 进行注解来为您的规范中的每个组件设置 additionalProperties 为 false如果您正在使用 Springfox

如果您正在使用 Springdoc您可以添加一个 OpenApiCustomiser bean示例请参见以下内容

**使用 Springdoc OpenAPI 的示例**
```java
@Bean
public OpenApiCustomiser openApiCustomiser() {
    return openApi -&gt; openApi.getComponents().getSchemas().values().forEach( s -&gt; s.setAdditionalProperties(false));
}

使用 Springfox 框架的示例

@Component
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
public class OpenApiTransformationFilter implements WebMvcOpenApiTransformationFilter
{
    public boolean supports(@NotNull DocumentationType delimiter)
    {
        return SwaggerPluginSupport.pluginDoesApply(delimiter);
    }

    @Override
    public OpenAPI transform(OpenApiTransformationContext&lt;HttpServletRequest&gt; context)
    {
        OpenAPI openApi = context.getSpecification();
        openApi.getComponents().getSchemas().values().forEach(schema -&gt; schema.setAdditionalProperties(false));
        return openApi;
    }
}
英文:

If you want explicitly set the attribute to false you can a TransformationFilter (annoted @Component for Spring) to set additionalProperties to false for each component of you specification if you are using Springfox.

If you are using Springdoc, you can add a OpenApiCustomiser bean, see examples

Example with Springdoc OpenAPI

    @Bean
    public OpenApiCustomiser openApiCustomiser() {
        return openApi -&gt; openApi.getComponents().getSchemas().values().forEach( s -&gt; s.setAdditionalProperties(false));
    }

Example with Springfox framework

@Component
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
public class OpenApiTransformationFilter implements WebMvcOpenApiTransformationFilter
{
    public boolean supports(@NotNull DocumentationType delimiter)
    {
        return SwaggerPluginSupport.pluginDoesApply(delimiter);
    }

    @Override
    public OpenAPI transform(OpenApiTransformationContext&lt;HttpServletRequest&gt; context)
    {
        OpenAPI openApi = context.getSpecification();
        openApi.getComponents().getSchemas().values().forEach(schema -&gt; schema.setAdditionalProperties(false));
        return openApi;
    }
}

答案2

得分: 1

一个解决方法可能是定义一个包含类型信息的虚拟类,然后在你的 @APIResponse 中将其用作 @Schema#implementation 类。

static class YourTypeMap extends java.util.HashMap<String, YourType> {};

@APIResponse(
  responseCode = "200",
  content = @Content(
    mediaType = "application/json", 
    schema = @Schema(implementation = YourTypeMap.class)))

来源: MikeEdgar

英文:

One workaround might be define a dummy class that contains the type information, then use that as the @Schema#implementation class in your @APIResponse.

static class YourTypeMap extends java.util.HashMap&lt;String, YourType&gt; {};

Then:

@APIResponse(
  responseCode = &quot;200&quot;,
  content = @Content(
    mediaType = &quot;application/json&quot;, 
    schema = @Schema(implementation = YourTypeMap.class)))

Credits: MikeEdgar

huangapple
  • 本文由 发表于 2020年10月23日 04:08:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64489812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定