How can I set the swagger documentation to use dd/MM/yyyy format?

Viewed 837

I am parsing a date to a rest service using spring-boot and Swagger for documenting the api.

On the endpoint, I only want to have dates without hours. And is easily achieved with @DateTimeFormat(pattern = "dd/MM/yyyy"). Note that I prefer that days comes first than months.

The endpoint is working fine when using curl, then I do not think that is the issue about it. For example to set the 30 of November of current year, I can call it with Curl as follows:

curl -X GET "http://localhost:8085/app/api/endpoint?creationTime=30%2F11%2F2020" -H  "accept: application/json"

And works fine. But if I use directly the Swagger page for testing I have these cases:

First, if I set date as 30/11/2020, swagger does not allow me to proceed:

enter image description here

If I set date as 11/30/2020, swagger allows me to continue, but I get this error:

"message": "Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@io.swagger.annotations.ApiParam @org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '11/30/2020'; nested exception is org.joda.time.IllegalFieldValueException: Cannot parse \"11/30/2020\": Value 30 for monthOfYear must be in the range [1,12]",

As I am telling that is the month number 30. And makes sense to me.

If I set 05/11/2020, Swagger allows me to send the request and the final date is the 5 of November, as is the format I have defined.

For me seems that swagger is filtering me by MM/dd/yyyy format and not using my dd/MM/yyyy that is really defined on the endpoint. Is there any option to set the swagger validation as dd/MM/yyyy?

Note: swagger version used 2.9.2. Spring-boot version 2.1.8.RELEASE

1 Answers
We can use,
birthday:
    description: Date of birth
    type: date
    format: DD-MM-YYYY
    example: "30/11/2022"
 
For nestjs you can use 
@ApiProperty({ example: "20-09-2022", type:Date,  default: 'DD-MM-YYYY', format: 'DD-MM-YYYY' })
Related