[英]How can @JsonValue be used for Swagger enum values using springdoc-openapi with a Lombok getter
Given a Spring Boot project that uses the springdoc-openapi library to expose an OpenAPI (Swagger) endpoint documenting the Spring MVC controller endpoints of the project.给定一个 Spring 引导项目,该项目使用springdoc-openapi库公开一个OpenAPI (Swagger) 端点,该端点记录了项目的 Spring MVC controller 端点。
One of the enums in the project uses @JsonValue
from Jackson on a field to change the JSON representation of the enum.项目中的枚举之一在字段上使用来自@JsonValue
的@JsonValue来更改枚举的 JSON 表示。 This enum field is exposed as a getter using the @Getter
annotation from Project Lombok :此枚举字段使用Project Lombok中的@Getter
注释作为 getter 公开:
@Getter
public enum Suit {
HEARTS("Hearts"), DIAMONDS("Diamonds"), CLUBS("Clubs"), SPADES("Spades");
@JsonValue
private final String name;
Suit(String name) { this.name = name; }
}
However, despite the Jackson representation being based on the field, the enum representation returned by the OpenAPI endpoint uses the toString
value of the enum
instead:然而,尽管 Jackson 表示基于字段,OpenAPI 端点返回的枚举表示使用enum
的toString
值代替:
"suit": {
"type": "string",
"enum": [
"HEARTS",
"DIAMONDS",
"CLUBS",
"SPADES"
]
}
Expected:预期的:
"suit": {
"type": "string",
"enum": [
"Hearts",
"Diamonds",
"Clubs",
"Spades"
]
}
Based on springdoc-openapi#1244 and swagger-core#3998 , it's clear that the @JsonValue
annotation needs to be applied to the method, and not the field.基于springdoc-openapi#1244和swagger-core#3998 ,很明显@JsonValue
注释需要应用于方法,而不是字段。 However, neither the above attempted approach, nor the following, work:但是,上述尝试的方法和以下方法都不起作用:
@Getter @JsonValue
public enum Suit {
HEARTS("Hearts"), DIAMONDS("Diamonds"), CLUBS("Clubs"), SPADES("Spades");
private final String name;
Suit(String name) { this.name = name; }
}
How can this enum be exposed with the proper values in Swagger, while still using Lombok to generate the getter?如何使用 Swagger 中的正确值公开此枚举,同时仍使用 Lombok 生成 getter?
The solution is to tell Lombok to use the annotation on the generated getter method, using @Getter(onMethod_ = @JsonValue)
on the field.解决方案是告诉 Lombok 在生成的 getter 方法上使用注解,在字段上使用@Getter(onMethod_ = @JsonValue)
。
public enum Suit {
HEARTS("Hearts"), DIAMONDS("Diamonds"), CLUBS("Clubs"), SPADES("Spades");
@Getter(onMethod_ = @JsonValue)
private final String name;
Suit(String name) { this.name = name; }
}
The onMethod
property is documented in the @Getter and @Setter and the onX documentation: onMethod
属性记录在@Getter 和 @Setter以及onX文档中:
To put annotations on the generated method, you can use
onMethod=@__({@AnnotationsHere})
.要在生成的方法上添加注释,可以使用onMethod=@__({@AnnotationsHere})
。 […] For more details see the documentation on the onX feature. [...] 更多详情请参阅onX功能的文档。
The syntax is a little strange and depends on the javac you are using.语法有点奇怪,取决于您使用的 javac。
On javac7, to use any of the 3onX
features, you must wrap the annotations to be applied to the constructor / method / parameter in@__(@AnnotationGoesHere)
.在 javac7 上,要使用 3 个onX
特性中的任何一个,您必须将要应用于构造函数/方法/参数的注释包装在@__(@AnnotationGoesHere)
中。 To apply multiple annotations, use@__({@Annotation1, @Annotation2})
.要应用多个注释,请使用@__({@Annotation1, @Annotation2})
。 The annotations can themselves obviously have parameters as well.注释本身显然也可以具有参数。
On javac8 and up, you add an underscore afteronMethod
,onParam
, oronConstructor
.在 javac8 及更高版本上,在onMethod
、onParam
或onConstructor
之后添加下划线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.