Swagger/OpenAPI annotations V3 - 在 swagger 注释中使用枚举值

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

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

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

其中一个注释是 @Schema 注释,它接受一个名为 allowableValues 的属性,它允许一个字符串数组:

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

现在我想使用在我们的枚举 class 上构建的自定义方法,即 returns 允许的字符串数组,因此不需要在每次添加类型时都添加它我们的枚举。这样我们就可以像这样使用它:

public enum ExampleEnum {
    EXAMPLEV1, EXAMPLEV2;
    public static String[] getValues() {...}
}

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

现在无法编译,因为执行注释时方法未知。 是否有这样的解决方案允许在 swagger V3 注释属性值中使用枚举?

查看了以下资源:

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

最坏的情况是我确实可以将它定义在一个不变的地方,并且在向 Enum 添加类型之后只需要在另一个地方添加该类型。但如果可能的话,我首先想探索上述解决方案。

没有提及使用任何 classes 或动态生成的值。

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

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

就我而言,我在我的枚举中添加了注释:

@Schema(enumAsRef = true)
public enum WikipediaLanguage {
  ...
}

然后在 REST 控制器方法的参数中将其注释为参数:

@Parameter(
    description = "Language of the Wikipedia in use",
    required = true
) @RequestParam WikipediaLanguage lang