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

2023-12-02

我正在尝试设置附加属性元素添加到 Open API Schema 3.X 中,但不幸的是我无法在文档中找到任何可以帮助我的内容。 我在 Spring boot 中有一个应用程序,它使用 Spring doc OAS,该 OAS 依赖于 Swagger OAS 作为传递依赖项。 让我在这里挑选一些代码片段:

@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: enter image description here


如果您想显式将该属性设置为 false,则可以使用 TransformationFilter(对于 Spring,注释为 @Component)将规范的每个组件的 extraProperties 设置为 false(如果您使用的是 Springfox)。

如果您使用 Springdoc,您可以添加 OpenApiCustomiser bean,请参阅示例

Springdoc OpenAPI 示例

    @Bean
    public OpenApiCustomiser openApiCustomiser() {
        return openApi -> openApi.getComponents().getSchemas().values().forEach( s -> 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<HttpServletRequest> context)
    {
        OpenAPI openApi = context.getSpecification();
        openApi.getComponents().getSchemas().values().forEach(schema -> schema.setAdditionalProperties(false));
        return openApi;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将附加属性设置为布尔值 的相关文章

随机推荐