swagger/openapi向每个枚举值添加@schema(description)

kiz8lqtg  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(1347)

我正在使用swagger/openapi v3注解创建应用程序的api描述,这些注解是从以下依赖项导入的:

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

我想向每个枚举值添加说明:

public enum Gender {

    @Schema(description = "NotSpecified")
    NOT_SPECIFIED,

    @Schema(description = "Male")
    MALE,

    @Schema(description = "Female")
    FEMALE;
}

但是springdoc不会为enum生成描述。

Gender:
  type: string
  example: MALE
  enum:
  - NOT_SPECIFIED
  - MALE
  - FEMALE

是否有方法向每个枚举值添加说明?

9rygscc1

9rygscc11#

我相信您只能在描述枚举时描述枚举值(https://swagger.io/docs/specification/data-models/enums/). 所以你可以这样做:

@Schema(enumAsRef = true, description = "gender: \n" +
        "* `NOT_SPECIFIED` - NotSpecified\n" +
        "* `MALE` - Male\n" +
        "* `FEMALE` - Female\n" +
        "")
enum Gender {
    NOT_SPECIFIED,
    MALE,
    FEMALE
}

然后,生成的招摇过市看起来相当不错。

相关问题