Springdoc-openapi: @Schema example ignored when data type is LocalDate

Created on 4 Dec 2019  Â·  2Comments  Â·  Source: springdoc/springdoc-openapi

If I use this variable and @Schema

public class OrderApplication {

    ....

    @Schema(description = "Date consumer apply for order. If not provided, will use current date..", required = false, example = "2020-02-17T00:00:00")
    private LocalDate applyDate;

....
}

The generated documentation is using current date : "apply_date": "2019-12-04"
If I use the example, removing the time part, e.g. example = "2020-02-17", it generates correct date as example but without time part, e.g. "apply_date": "2020-02-17"

The problem is, client's standard ignore timezone in ISO-8601 format (the Z part).
If I change the datatype into LocalDateTime, but my example does not include timezone (example = "2020-02-17T00:00:00"), springdoc also ignores the example

Most helpful comment

Hi,

This is the default behaviour of swagger-models.
When you use LocalDate, the related schema is to DateSchema, which doesn't accept any timezone.

  • @Schema(type="string" ,format = "date", example = "2020-02-17")
    When you use LocalDateTime, you need to define time timezone.
  • @Schema(type="string" , format = "date-time", example = "2018-01-01T00:00:00Z")

You can just in your case use, and you can add your custom pattern to validate the input.

  • @Schema(type="string" , example = "2020-02-17T00:00:00")

All 2 comments

Hi,

This is the default behaviour of swagger-models.
When you use LocalDate, the related schema is to DateSchema, which doesn't accept any timezone.

  • @Schema(type="string" ,format = "date", example = "2020-02-17")
    When you use LocalDateTime, you need to define time timezone.
  • @Schema(type="string" , format = "date-time", example = "2018-01-01T00:00:00Z")

You can just in your case use, and you can add your custom pattern to validate the input.

  • @Schema(type="string" , example = "2020-02-17T00:00:00")

Had the same problem with LocalDateTime, which was completely ignoring my custom example and format.

The problem is that example does not match the pattern from the @Schema
annotation.
According to swagger docs, the default patterns are

date – full-date notation as defined by RFC 3339, section 5.6, for example, 2017-07-21
date-time – the date-time notation as defined by RFC 3339, section 5.6, for example, 2017-07-21T17:32:28Z

Code snippet

There is not completely clear the solution from the previous answer, so I will post the actual code next:

  @Schema(
    pattern = "20[0-2][0-9][1-2][0-9][0-3][0-9]T[0-5][0-9][0-5][0-9]",
    example = "20170131T2359" // <-- that example must match the pattern above
  )
  @JsonFormat(pattern = "yyyyMMdd'T'HHmm")
  private LocalDateTime dateFrom;
Was this page helpful?
0 / 5 - 0 ratings