我在 spring 项目中使用 OpenAPI java 生成器 [1] 和 library:resttemplate, dateLibrary:java8 从规范生成客户端。
对于规范中的属性:
targetDate:
type: string
format: date
Run Code Online (Sandbox Code Playgroud)
生成以下代码:
public static final String JSON_PROPERTY_TARGET_DATE = "targetDate";
private LocalDate targetDate;
@javax.annotation.Nonnull
@JsonProperty(JSON_PROPERTY_TARGET_DATE)
@JsonInclude(value = JsonInclude.Include.ALWAYS)
public LocalDate getTargetDate() {
return targetDate;
}
@JsonProperty(JSON_PROPERTY_TARGET_DATE)
@JsonInclude(value = JsonInclude.Include.ALWAYS)
public void setTargetDate(LocalDate targetDate) {
this.targetDate = targetDate;
}
Run Code Online (Sandbox Code Playgroud)
我希望该字段能够序列化为完整日期,例如规范所承诺的“2023-01-01”:https: //spec.openapis.org/oas/v3.0.0#data-types。然而它实际上被序列化为一个数组:[2023,1,1]。
同样的另一个属性
otherDate:
type: string
format: date-time
Run Code Online (Sandbox Code Playgroud)
被序列化为自纪元以来的秒数,而不是全时。(我认为这是生成器中的错误)
由于生成了代码,我无法添加任何注释。我怎样才能确保日期正确序列化?
[1] openapi-generator-maven-plugin 6.3.0
java openapi jackson-databind openapi-generator openapi-generator-maven-plugin