Nikita
Nikita

Reputation: 1515

Java 8 LocalDate displaying in swagger

I have a DTO which contains field of Java 8 LocalDate type. With Jackson annotations it's possible to set format to ISO.DATE and everything works good. But Swagger (I have version 2.+) see the LocalDate.class as object

LocalDate {
month (integer, optional),
year (integer, optional)
}

(That's true but...) I want to dipsay this as string with format as it works with util.Date. How can I solve it?

Upvotes: 3

Views: 11835

Answers (1)

staszek
staszek

Reputation: 251

I was facing same problem, so I added

@Bean
public Docket docket() {
    return new Docket(DocumentationType.SWAGGER_2)
                .groupName("name")
                .directModelSubstitute(LocalDateTime.class, String.class)
                .directModelSubstitute(LocalDate.class, String.class)
                .directModelSubstitute(LocalTime.class, String.class)
                .directModelSubstitute(ZonedDateTime.class, String.class)
                .apiInfo(apiInfo())
                .select()
                .paths(paths())
                .build();
}

in docket configuration.

directModelSubstitute makes swagger to treat LocalDate as String class

Upvotes: 7

Related Questions