java - 如何将附加属性设置为布尔值
问题描述
我正在尝试将Additional Properties元素设置到 Open API Schema 3.X 中,但不幸的是,我无法在文档中找到任何可以帮助我的内容。我在 Spring Boot 中有一个应用程序,它使用依赖 Swagger OAS 作为传递依赖的 Spring doc 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) { }
解决方案
如果您想将属性显式设置为 false,则可以使用 TransformationFilter(注释为 Spring 的 @Component)将您规范的每个组件的附加属性设置为 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;
}
}
推荐阅读
- http - 使用 Go 标准库,为什么我在这种两层架构中不断泄漏 TCP 连接?
- python - 如何使生成器中的范围数工作
- html - HTML不要让垂直滚动条创建水平滚动条
- c++ - 为什么我们称数组为顺序容器?
- postgresql - Postgres:如何计算范围条件的记录?
- python - 如何重塑我的 Python 数据框以适应“PowerBI”维恩图?
- jenkins - 使用 .war 文件运行 jenkins 和使用安装程序有什么区别?
- regex - Notepad++ 突出显示两个方括号之间的任何内容
- javascript - 使用分页过滤数据
- redis - 在同一个 Redis 中将所有 KEYS 从一个数据库复制到另一个数据库