swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[Java][Spring] Adds missing import for Map - Fixes #7432

Open aykutakin opened this issue 7 years ago • 3 comments

PR checklist

  • [x] Read the contribution guidelines.
  • [x] Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • [x] Filed the PR against the correct branch: 3.0.0 branch for changes related to OpenAPI spec 3.0. Default: master.
  • [x] Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

@bbdouglas @JFCote @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger

Description of the PR

Java Spring code generator has missing java.util.Map import when api return type is Map.

Tested with:

paths:
  /customer:
    get:
      tags:
        - Customers
      summary: Retrieve all customers
      produces:
        - application/json
      responses:
        '200':
          description: OK
          schema:
            type: object
            additionalProperties:
              type: array
              items:
                type: string

Fixes #7432

aykutakin avatar Jan 27 '18 20:01 aykutakin

@aykutakin thanks for the PR. Would this result in duplicated import of "java.util.Map" if there are more than 1 response with the type Map?

wing328 avatar Jan 28 '18 06:01 wing328

I thought I've tested it, but apparently tested it wrong. Thanks for pointing that out again @wing328. When I change the input yaml as in below, as you've foreseen, it generates 2 java.util.Map imports:

paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          type: integer
          format: int32
      responses:
        "200":
          description: A paged array of pets
          headers:
            x-next:
              type: string
              description: A link to the next page of responses
          schema:
            type: object
            additionalProperties:
              type: array
              items:
                type: string
        default:
          description: unexpected error
          schema:
            $ref: '#/definitions/Error'
  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          type: string
      responses:
        "200":
          description: Expected response to a valid request
          schema:
            type: object
            additionalProperties:
              type: array
              items:
                type: string
        default:
          description: unexpected error
          schema:
            $ref: '#/definitions/Error'

Template file also has hard coded java.util.List import. Which results with also unnecessary multi import of java.util.List, since another import also comes from {{#imports}} section in this case. I think cleaning up is a different issue and for now doesn't cause catastrophic results. Importing java.util.Map like List is one way of resolving this issue, but I'll look for a better way to handle this before jumping to easiest solution.

aykutakin avatar Jan 28 '18 15:01 aykutakin

Change has been made to import complex type's through {{import}} tag. There is already additional complex type check for Parameters. With this change, it has been also added to Response.

aykutakin avatar Jan 28 '18 20:01 aykutakin