英文:
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("/{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:
答案1
得分: 6
如果您想显式地将属性设置为 false,您可以使用 TransformationFilter(使用 Spring 的 @Component 进行注解)来为您的规范中的每个组件设置 additionalProperties 为 false,如果您正在使用 Springfox。
如果您正在使用 Springdoc,您可以添加一个 OpenApiCustomiser bean,示例请参见以下内容:
**使用 Springdoc OpenAPI 的示例**
```java
@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;
}
}
英文:
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;
}
}
答案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<String, YourType> {};
Then:
@APIResponse(
responseCode = "200",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = YourTypeMap.class)))
Credits: MikeEdgar
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论