3

I have a POST rest service that receive dates, one in queryParameter and the other one in requestBody. Here are parts of the yaml definition.

paths:
...
parameters:
        - in: query
          name: queryDate
          schema:
            type: string
            format: date
schemas:
...
TestObject:
      type: object
      properties:
        bodyDate:
          type: string
          format: date

The format is date, not date-time, so I expect to get a date like

  • "2021-08-23" and not like
  • "2021-08-23T05:39:12Z"

QueryDate is OK This behaviour works well for queryDate, the query parameter.

  • If I put "2021-08-23", I get a HTTP 200, which is correct.
  • If I put "2021-08-23T05:39:12Z" I get a 400 error Bad request, which is correct.

bodyDate is NOK This behaviour is different for bodyDate, contained in the requestBody.

  • If I put "2021-08-23", I get a HTTP 200 which is correct.
  • If I put "2021-08-23T05:39:12Z", I get a HTTP 200, which is NOT CORRECT -> I should get a HTTP 400 error for bad request.

I tried to add a pattern to bodyDate but it adds an @Pattern annotation on the generated TestObject.queryDate field which is already a LocalDate so it does not work.

Is there a way to force the validation of bodyDate to respect the format yyyy-mm-dd without removing "format: date"? Thanks

Helen
  • 87,344
  • 17
  • 243
  • 314
Valéry
  • 31
  • 2
  • 1
    What software are you using to process these requests and validate based on the OpenAPI spec? Each implementation of OpenAPI will be slightly different, and may contain bugs. I suspect you have just found one of them – tom redfern Aug 23 '21 at 10:43
  • Thanks for your question. I use openapi-generator-maven-plugin (5.1.0) to generate model and api in my springboot Java application. – Valéry Aug 23 '21 at 11:48
  • And I test application using swagger – Valéry Aug 24 '21 at 05:57
  • 1
    I meant how are you doing the validation. Are you using Hibernate Validator or something similar? – tom redfern Aug 24 '21 at 08:20
  • actually I don't perform any manual validation, but if I tried to put a pattern, the message complains with a No Hibernate validator for Pattern on localDate. So I guess the response to your question is Hibernate validator. – Valéry Aug 24 '21 at 11:38
  • 1
    Other information: if I add `@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")` above `public LocalDate getCreatedDate() {` in the generated model class. It works. But it makes no sense to edit manually a generated class so I'd like to find a way to generate it from the openapi yaml specification. – Valéry Aug 24 '21 at 11:38
  • this post have the same problem but is not solved: https://stackoverflow.com/questions/55425645/can-we-generate-jsonformat-on-model-variable-using-swagger-generater – Valéry Aug 24 '21 at 13:37

0 Answers0