Skip to content

[BUG] Spring Boot fails to parse LocalDate query parameter #2053

Closed

Description

@macjohnny

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

When defining a query parameter of type: string and format: date

  /bla:
    get:
      parameters:
        - $ref: '#/parameters/analysisDate'
      produces:
        - application/json
      responses:
          200:
            description: 'Successful operation'

parameters:
  analysisDate:
    name: analysisDate
    in: query
    required: true
    type: string
    format: date

the server fails to parse a url like /bla?analysisDate=2017-11-30

The generated code looks like

 blaGet(@NotNull @ApiParam(value = "Analysis Date", required = true) @Valid @RequestParam(value = "analysisDate", required = true) LocalDate analysisDate) 
openapi-generator version

3.3.4

Related issues/PRs

https://blog.codecentric.de/en/2017/08/parsing-of-localdate-query-parameters-in-spring-boot/
spring-projects/spring-boot#9930

Suggest a fix

adding @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) resolves the issue:

blaGet(@NotNull @ApiParam(value = "Analysis Date", required = true) @Valid @RequestParam(value = "analysisDate", required = true) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate analysisDate)

Activity

  1. macjohnny commented on May 24, 2019

    @macjohnny
    MemberAuthor

    relevant code:

    {{#isQueryParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}@ApiParam(value = "{{{description}}}"{{#required}}, required = true{{/required}}{{#allowableValues}}, allowableValues = "{{#values}}{{{.}}}{{^-last}}, {{/-last}}{{#-last}}{{/-last}}{{/values}}"{{/allowableValues}}{{^isContainer}}{{#defaultValue}}, defaultValue = {{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}{{{defaultValue}}}{{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}{{/defaultValue}}{{/isContainer}}) {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestParam(value = "{{baseName}}"{{#required}}, required = true{{/required}}{{^required}}, required = false{{/required}}{{^isContainer}}{{#defaultValue}}, defaultValue={{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}{{{defaultValue}}}{{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}{{/defaultValue}}{{/isContainer}}) {{>optionalDataType}} {{paramName}}{{/isQueryParam}}

  2. macjohnny commented on Sep 3, 2019

    @macjohnny
    MemberAuthor

    See also #3248

  3. melissapalmer commented on Sep 5, 2019

    @melissapalmer

    See also #2053

    @macjohnny which issue did you mean to link this too #2053 is this issue?

    I am having the same issue that Spring server generated code is not parsing LocalDate correctly, reason I found is because the code being generated for these request parameters is as:

    @ApiParam(value = "Filter on KYC applications since a date")  LocalDate filterDateFrom
    

    where as it should (include @RequestParam)

     @RequestParam(value = "filterDateFrom", required = false) @ApiParam(value = "Filter on KYC applications since a date")  LocalDate filterDateFrom
    

    I am using version 4.1.1 of the generator

    And this seems to be caused by the change you pointed out at:
    #2053 (comment)

    What did you mean with these last comments you added please.

  4. macjohnny commented on Sep 5, 2019

    @macjohnny
    MemberAuthor

    I updated my comment to link the correct issue

  5. peyerroger commented on Sep 8, 2019

    @peyerroger
    Contributor

    Hi guys

    Even with the RequestParam annotation spring is not able to parse date/date-time into LocalDate/OffsetDateTime.

    The suggested fix of @macjohnny to add @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) is from my point of view the correct way. At the moment we add this on the implementation. But it would be nice to have it in the generated code. I will create a PR if you agree.

    @macjohnny The line needs an import statement. Is it ok to add the full class name in the queryParam.mustache file to prevent unused imports if no date/datetime params are defined?

    Solution:
    If 'isDate' (found in CodegenParameter.java)

    @org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)
    

    If 'isDateTime' (found in CodegenParameter.java)

    @org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
    
  6. peyerroger commented on Sep 8, 2019

    @peyerroger
    Contributor

    @macjohnny Had some time and created the PR right away. #3860 Could you take a look? Do you know who can add the technical committee? Could you do that?

    Please note this only works correctly when issue #3248 is resolved as well.

  7. melissapalmer commented on Sep 9, 2019

    @melissapalmer
  8. added this to the 4.1.2 milestone on Sep 9, 2019
  9. modified the milestones: 4.1.2, 4.1.3 on Sep 11, 2019
  10. added a commit that references this issue on Sep 11, 2019
  11. added a commit that references this issue on Sep 12, 2019
  12. added a commit that references this issue on Sep 13, 2019
  13. baurceanu commented on Nov 29, 2019

    @baurceanu
    Contributor

    4Googlers:
    You can disable this OOTB validation by using custom formats for date / time.
    yaml

    - name: dateFrom
      in: query
      schema:
        type: string
        format: my-custom-date-format
    

    pom.xml

    <plugin>
      ...
      <executions>
        <execution>
          ...
          <configuration>
            ...
            <typeMappings>my-custom-date-format=java.time.LocalDate</typeMappings>
          </configuration>
        </execution>
      </executions>
    </plugin>
    
  14. zalito12 commented on Jan 16, 2020

    @zalito12
    Contributor

    Hi! Note this is not working when you have your query parameters definied inside an object, for example:

    paths:
      /elephants:
        get:
          operationId: getElephants
          parameters:
            - in: query
              name: count
              schema:
                type: integer
    	- $ref: '#/components/parameters/SearchParams'
    components:
      parameters:
        SearchParams:
          name: searchParams
          description: Possible search params to filter case search
          in: query
          style: form
          explode: true
          schema:
            type: object
            properties:
              prop1:
                type: integer
                example: 1
              start:
                type: string
                format: date
                example: '2019-12-31'
              end:
                type: string
                format: date
                example: '2019-12-31'
    

    If I'm correct the @DateTimeFormat annotation also works in class fields. Has anyone faced this problem too?

  15. prateek2007 commented on Jan 30, 2020

    @prateek2007

    in case of "dateLibrary": "java8", DateFormatter is =DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
    which accepts date of format 12/6/19

  16. qbixsd commented on Feb 20, 2020

    @qbixsd

    in case of "dateLibrary": "java8", DateFormatter is =DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
    which accepts date of format 12/6/19

    according to documentation, string with date format should be formatted with ISO format (yyyy-MM-dd): https://swagger.io/docs/specification/data-models/data-types/
    So this default approach is in contradiction to openapi specification.

    Also, is there a way to override this setting?

  17. fditolla commented on May 6, 2021

    @fditolla

    In my case in the implemntation method I added @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) to the field and it seems to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions