pixel
pixel

Reputation: 10587

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;

My birthday is of OffsetDateTime type, however, request comming 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 some post (https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#data-types, 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 due to legacy code or no ability to change, 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: enter image description here

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.

Upvotes: 0

Views: 1367

Answers (1)

user29831793
user29831793

Reputation: 1

you can delete the format = "yyyyMMdd" like this

@Schema(example = "19680228", type = "String",  pattern = "([0-9]{4})(?:[0-9]{2})([0-9]{2})", required = true, nullable = false)
@JsonDeserialize(using = CustomDateDeserializer.class)
private OffsetDateTime birthDate;

Upvotes: 0

Related Questions