How to make Swagger core generate openapi.json documentation with a Year-Month field represented as String?
In code I already tried @JsonFormat
and @JsonbDateFormat
.
Returned entity is correctly represented as String, with correct format, but documentation always is generated as Object.
Environment:
Here a snippet:
@JsonbDateFormat(value = "uuuu-MM") // or "yyyy-MM", or @JsonFormat(shape = Shape.STRING, pattern = "uuuu-MM")
private YearMonth reference;
Entity return:
{
"reference": "2020-11",
}
Documentation generated:
{
"openapi" : "3.0.1",
...
"components" : {
"schemas" : {
"Entity" : {
"type" : "object",
"properties" : {
"reference" : {
"type" : "object",
"properties" : {
"year" : {
"type" : "integer",
"format" : "int32"
},
"month" : {
"type" : "string",
"enum" : [ "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" ]
},
"monthValue" : {
"type" : "integer",
"format" : "int32"
},
"leapYear" : {
"type" : "boolean"
}
}
}
}
}
}
}
}
Apparently Swagger core do not use @JsonFormat
or @JsonbDateFormat
as source for formatting template, it uses only attributes types, but it do not have a default for YearMonth type.
So, it's possible to define data representation with Swagger Annotation @Schema
.
In this particular case: @Schema(type = "string", format = "yearmonth", example = "2020-07")
Also, to use a default custom schema, see How to set a default custom schema for types in Swagger