简体   繁体   English

如何在 OpenApi Spring 上添加不同的@Schema 描述?

[英]How to add different @Schema descriptions on OpenApi Spring?

I'm having some issues defining my API using OpenApi with Spring.我在使用 OpenApi 和 Spring 定义我的 API 时遇到了一些问题。 I'm using this dependency:我正在使用此依赖项:

<dependency>
   <groupId>org.springdoc</groupId>
   <artifactId>springdoc-openapi-ui</artifactId>
   <version>1.5.2</version>
</dependency>

My problem is that I want to define a @Schema on different Api Responses but use a different description and example in each one of the responses.我的问题是我想在不同的 Api 响应上定义一个 @Schema,但在每个响应中使用不同的描述和示例。

Now I have one response that looks like this:现在我有一个看起来像这样的响应:

public class LoginResponse {

    @Schema(description = "User identifier", example = "12")
    private Long id;
    @Schema(description = "User balance", example = "{"type":"COINS","amount":1000}")
    private Balance balance;
...

and other response that looks like this...和其他看起来像这样的回应......

public class ModifyBalanceResponse {

    @Schema(description = "User identifier", example = "12")
    private Long id;
    @Schema(description = "Added balance", example = "{"type":"COINS","amount":500}")
    private Balance addedBalance;
    @Schema(description = "Updated balance", example = "{"type":"COINS","amount":1500}")
    private Balance updatedBalance;
...

So, I have the object "Balance" with three different descriptions and examples, but when it generates the documentation, all the responses and fields using this object takes the same description and example.所以,我有 object “余额”,其中包含三个不同的描述和示例,但是当它生成文档时,使用此 object 的所有响应和字段都采用相同的描述和示例。

I have seen that in the generated file that I get all this items with a "$ref" tag to Balance schema and this is generated with only one of the description/example defined, like this:我已经看到,在生成的文件中,我将所有带有“$ref”标签的项目都添加到 Balance 模式,并且仅使用定义的描述/示例之一生成,如下所示:

addedBalance:
   $ref: '#components/schemas/Balance'

I have tried to edit the file manually and I made possible to see the swagger doc as I want by replacing this with...我试图手动编辑该文件,并且通过将其替换为 ...

addedBalance:
   title: Balance
   description: Added balance
   example: '{"type":"COINS","amount":500}'

There is any way to do something like this with the Spring annotations provided by openapi?有什么办法可以用openapi提供的Spring注解来做这样的事情吗? Something like ignoring the schema object and taking literally the description and the example.就像忽略架构 object 并从字面上理解描述和示例。 I don't mind if it is not referenced to the schema object.我不介意它是否没有引用架构 object。

Thanks on advantage.感谢优势。

It's lame but you can solve this by making a different response class for each item.这很糟糕,但您可以通过对每个项目做出不同的响应 class 来解决这个问题。

public class AddedBalance extends Balance {}
public class UpdatedBalance extends Balance {}

There must be a better solution...应该有更好的解决方案...

You can also use a Generic on your class to force SpringDoc to think it's a different Type.您还可以在 class 上使用 Generic 来强制 SpringDoc 认为它是不同的类型。

public static class Balance<T> {
   ...
}

public static class ModifyBalanceResponse {

    @Schema(description = "User identifier", example = "12")
    private Long id;
    @Schema(description = "Added balance", example = "{"type":"COINS","amount":500}")
    private Balance<Added> addedBalance;
    @Schema(description = "Updated balance", example = "{"type":"COINS","amount":1500}")
    private Balance<Updated> updatedBalance;
}

public static class Added {}
public static class Updated {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swagger 3 / OpenApi 如何为同一个状态码添加两个描述 - Swagger 3 / OpenApi How to add two descriptions to the same status code Java Spring Boot OpenApi 3 - 如何为 RequestBody 添加描述? - Java Spring Boot OpenApi 3 - How to add description for RequestBody? 如何在 Spring Boot 中使用 OpenAPI 3 从“响应”和“请求正文”中隐藏“模式”? - How to hide "Schema" from "response" and "Request body" using OpenAPI 3 in Spring Boot? 使用 springdoc-openapi 为 spring Rest Api 显示响应代码的多个描述 - Show multiple descriptions for a response code using springdoc-openapi for a spring Rest Api OpenApi 如何从 @RequestBody -&gt; @Content -&gt; @Schema -&gt; 示例的资源文件中添加示例 - OpenApi how to add example from resources file for @RequestBody -> @Content -> @Schema -> example 为 spring 启动 json 响应测试集成 OpenAPI 3.0 模式 - Integrate OpenAPI 3.0 schema for spring boot json response test 使用 @Schema(allowableValues=) 在 spring 引导中使用 openapi 的枚举参数 - Using @Schema(allowableValues=) for enum param using openapi in spring boot 如何在java.util.Properties中添加描述? - How to add descriptions to java.util.Properties? 如何将 OpenAPI 客户端添加为子项目? - How to add OpenAPI client as a subproject? OpenAPI Generator - Java Client 生成与 Spring Server 不同的模型 - OpenAPI Generator - Java Client generating different model than Spring Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM