关于java:Swagger / OpenAPI注释V3-在swagger注释中使用Enum值

Swagger/OpenAPI annotations V3 - use Enum values in swagger annotations

我正在使用swagger / openApi V3注释创建应用程序的API描述,该注释是从以下依赖项导入的:

1
2
3
4
5
        <dependency>
            <groupId>org.springdoc</groupId>
            springdoc-openapi-ui</artifactId>
            <version>1.1.45</version>
        </dependency>

注释之一是@Schema注释,该注释接受名为allowableValues的属性,该属性允许字符串数组:

1
2
@Schema(description ="example", allowableValues = {"exampleV1","exampleV2"}, example ="exampleV1", required = true)
private String example;

现在,我想使用在我们的Enum类上构造的自定义方法,该方法返回允许的字符串数组,因此不需要在每次向Enum添加类型时都添加它。这样我们就可以像这样使用它:

1
2
3
4
5
6
7
8
9
10
11
12
public enum ExampleEnum {
    EXAMPLEV1(), EXAMPLEV2();


    public static String[] getValues() {
        ...
    }
}


@Schema(description ="example", allowableValues = ExampleEnum.getValues(), example ="exampleV1", required = true)
private String example;

现在,由于执行注释时该方法未知,因此无法编译。
有没有这样的解决方案,允许在招摇的V3注释属性值中使用枚举?

看过以下资源:

  • https://swagger.io/docs/specification/data-models/enums/

    You can define reusable enums in the global components section and reference them via $ref elsewhere.

最糟糕的情况是,我的确可以在一个常量位置定义它,并且在向Enum添加类型之后,只需要在另一个位置添加类型即可。但是我首先想探讨上述解决方案。

  • https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations#schema

关于使用任何类或动态生成的值什么也没说。

  • 昂首阔步

关于在swagger中记录枚举,而不是在swagger注释API中使用它们。


尝试使用@Schema(implementation = ExampleEnum.class, ...),您可以添加所需的所有其他属性。 我需要有关您的实现的更多信息,但请首先尝试。