且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-10-17 18:33:55

如果您想将属性显式设置为 false,您可以使用 TransformationFilter(Spring 的 @Component 注释)为您规范的每个组件将 additionalProperties 设置为 false正在使用 Springfox.

如果您使用的是 Springdoc,您可以添加一个 OpenApiCustomiser bean,参见示例

Springdoc OpenAPI 示例

 @Bean公共 OpenApiCustomiser openApiCustomiser() {返回 openApi ->openApi.getComponents().getSchemas().values().forEach(s -> s.setAdditionalProperties(false));}

Springfox 框架示例

@Component@Order(Ordered.HIGHEST_PRECEDENCE + 1)公共类 OpenApiTransformationFilter 实现 WebMvcOpenApiTransformationFilter{公共布尔支持(@NotNull DocumentationType 分隔符){返回 SwaggerPluginSupport.pluginDoesApply(delimiter);}@覆盖公共 OpenAPI 转换(OpenApiTransformationContext 上下文){OpenAPI openApi = context.getSpecification();openApi.getComponents().getSchemas().values().forEach(schema -> schema.setAdditionalProperties(false));返回 openApi;}}

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("/{accountId}")
@Operation(summary = "Get account by account id", tags = TAG)
@ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "Return a specific account queried by path",
                content = { @Content(mediaType = "application/json",
                        schema = @Schema(implementation = AccountDetailsDTO.class)) }),
        @ApiResponse(responseCode = "404", description = "No accounts found",
                content = @Content) })
public ResponseEntity<AccountDetailsDTO> getAccountDetailsByClientId(@PathVariable("accountId") Integer accountId) { }

This attribute is default to true and What I would like to see is as false like that below:

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 -> openApi.getComponents().getSchemas().values().forEach( s -> 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<HttpServletRequest> context)
    {
        OpenAPI openApi = context.getSpecification();
        openApi.getComponents().getSchemas().values().forEach(schema -> schema.setAdditionalProperties(false));
        return openApi;
    }
}