我正在使用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注解属性值中是否存在允许使用Enums的解决方案?
查看了下面的资源:
您可以在全局组件部分定义可重用的枚举,并通过$ref引用它们。
最坏的情况是我确实可以将其定义在一个常量位置,并在将类型添加到枚举后只需要添加到另一个位置。但我首先想探索上述提到的解决方案是否可行。
没有关于使用任何类或动态生成的值的说明。
是关于在Swagger中记录枚举,而不是在Swagger注解API中使用它们。