0

My specification says that the dates must be in format dd-MM-yyyy. But default example value in Swagger does have different format (yyyy-MM-dd).

What I would like the json to be:

{
"SOME_DATE": "22-08-2023"
}

What actually is in json:

{
"SOME_DATE": "2023-08-22"
}

My implementation looks like this :

@JsonProperty(value = "SOME_DATE")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
@ApiModelProperty(required = true, example = "22-08-2023")
@Schema(name = "SOME_DATE", format = "dd-MM-yyyy", example = "22-08-2023")
private LocalDate someDate = null;

but somehow swagger always shows the someDate in default value in normal form.. When I send example request via swagger-ui it fails (which is correct behavior as of @JsonFormat)...

Neither the @ApiModelProperty nor the @Schema have any positive effect on the format.


edit1: the swagger definition

{
  "openapi": "3.0.1",
  ...
    "SOME_DATE": {
      "pattern": "dd-MM-yyyy",
      "type": "string",
      "format": "date",
      "example": "2023-08-22"
    },
  ...
}
Polostor
  • 187
  • 6
  • 25
  • Show us your swagger please – Youcef LAIDANI Aug 22 '23 at 11:43
  • 1
    have you tried to use `@DateTimeFormat(pattern = "dd-MM-yyyy")` ? from `org.springframework.format.annotation` – Andrei Lisa Aug 22 '23 at 11:50
  • Haven't tried it before.. But unfortunately it stays the same :( – Polostor Aug 22 '23 at 12:01
  • 1
    Is it your own specification, i.e. are you writing the specification yourself? Then I'd advise to stick to the standard ISO-8601 format for date and time formatting, which uses `yyyy-MM-dd`, instead of inventing your own non-standard format. – Jesper Aug 22 '23 at 12:10
  • @duffymo That link https://stackoverflow.com/questions/49379006/what-is-the-correct-way-to-declare-a-date-in-an-openapi-swagger-file actually helps a bit.. It clarifies that it must not be a date if it has a pattern.. – Polostor Aug 22 '23 at 12:10
  • 1
    @Jesper unfortunately no. I ought to be following other systems Spec. But it feels like I should push for change on their side anyway.. – Polostor Aug 22 '23 at 12:12
  • 1
    Maybe my answer could looks so stupid, but you try use each annotation separately, instead of use `@Schema` `@JsonFormat` together – Adrian Lagartera Aug 22 '23 at 12:17
  • Nope it is not stupid @AdrianLagartera , but yeah I tried with no real results.. :/ – Polostor Aug 22 '23 at 12:27
  • 1
    No good reason for the other systems to deviate, but they might not be willing to change. I see that sort of thing all the time with legacy systems. They'd prefer to propagate their bad choice than admit to being wrong. – duffymo Aug 22 '23 at 12:28
  • And is posible the name of the variable do something too? Try to use something without camelCase and try it, I didn't know why the both annotations didn't take the change xD – Adrian Lagartera Aug 22 '23 at 12:32

0 Answers0