我正在使用从以下依赖项导入的 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;
现在我想使用在我们的 Enum 类上构造的自定义方法,该方法返回允许的字符串数组,因此不需要在每次向 Enum 添加类型时添加它。这样我们就可以像这样使用它: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 之后,只需要在另一个位置添加类型。但如果可能的话,我首先想探索上述解决方案。
没有说明使用任何类或动态生成的值。
是关于在 swagger 中记录枚举而不是在 swagger annotations API 中使用它们。
最佳答案
尝试使用 @Schema(implementation = ExampleEnum.class, ...)
,您可以添加所需的所有其他属性。我需要更多关于你的实现的信息,但先试试这个。
关于java - Swagger/OpenAPI 注释 V3 - 在 swagger 注释中使用枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59282771/