swagger-ui
swagger-ui copied to clipboard
OpenApi Spring Boot - Show date as yyyyMMdd
In my request model, I have a field like
@NotNull
@Schema(example = "19680228", type = "String", format = "yyyyMMdd", pattern = "([0-9]{4})(?:[0-9]{2})([0-9]{2})", required = true, nullable = false)
@JsonDeserialize(using = CustomDateDeserializer.class)
private OffsetDateTime birthDate;
Birthday is of OffsetDateTime
type, however, request coming in contains only date portion in format yyyyMMdd
. This is requirement and cannot be changed. And this is OK, I already take care of that with my CustomDateDeserializer
and it is all working fine.
Based on OpenAPI documentation and post @ https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#data-types, https://stackoverflow.com/questions/49379006/what-is-the-correct-way-to-declare-a-date-in-an-openapi-swagger-file, I know that OpenAPI supports ISO8601 date and date-time, in which case, no pattern is needed, and type should be provided as date or date-time.
However, in case you require some other format, the documentation states that type should be String
, format should specify which format the date is in, and pattern should be provided as regex.
And this is exactly what I am doing in the above @Schema
annotation.
However, when I go to https://editor.swagger.io/ and paste my .yaml file into it, the API generated for both my request model and my controller still contain incorrect formatting for birthdate and the example is not even taken into consideration:
Here is how it shows in my model:
As you can see, the format is still getting the format for OffsetDateTime
and there is no example
at all.
Same for my controller:
enter image description here
How do I make birthday show up as yyyyMMdd? For example, how to make it show as 19720226 in swagger editor?
I am using OpenApi/Swagger 3 in a Spring Boot application.
Q&A (please complete the following information)
- OS: Windows
- Browser: Chrome, Firefox, Brave, Edge
- Version: OpenApi/Swagger 3
- Method of installation: Spring Boot maven
- Swagger-UI version: OpenApi/Swagger 3
- Swagger/OpenAPI version: OpenApi/Swagger 3
Content & configuration
Swagger-UI configuration options: No configuration has changed, all default. I dont know where to change it even.
Describe the bug you're encountering
described above
To reproduce...
Steps to reproduce the behavior: described above
Expected behavior
described above
Screenshots
privided above
Additional context or thoughts
n/a
That looks weird. A LocalDate
is more than enough to represent a birth date, OffsetDateTime
doesn't make sense at all.
Same problem here, have you found a workaround?