swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.09k stars 6.03k forks source link

[GO] Options struct name collision when operation has multiple tags #9471

Open kostyay opened 5 years ago

kostyay commented 5 years ago
Description

When an operation belongs to multiple tags the go swagger generator will create an api_<tag>.go file for each tag. The Opts struct that is created will have the same name in all the files which causes name collision.

Below you can see that List Pets operation has both the animals and the pets tag. This results in 2 files being created, api_animals.go and api_pets.go. Both files will contain the following struct definition for List Pets Options:

type ListPetsOpts struct { 
    Limit optional.Int32
}
Swagger-codegen version

2.4.5

Swagger declaration file content or url
swagger: "2.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
host: petstore.swagger.io
basePath: /v1
schemes:
  - http
consumes:
  - application/json
produces:
  - application/json
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
        - animals
      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:
            $ref: '#/definitions/Pets'
        default:
          description: unexpected error
          schema:
            $ref: '#/definitions/PetError'

definitions:
  Pet:
    type: "object"
    required:
      - id
      - name
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
      tag:
        type: string
  Pets:
    type: array
    items:
      $ref: '#/definitions/Pet'
  PetError:
    type: "object"
    required:
      - code
      - message
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string
Command line used for generation
    -i /mnt/invalid_swagger.yml \
    -l go \
    -D packageName=petstore \
    -o /mnt/petstore
Steps to reproduce

Run the above command on the swagger file and try to compile in go. You will get a redeclared in this block error for ListPetsOpts struct that will appear both in api_animals.go and api_pets.go.

Related issues/PRs

n/a

Suggest a fix/enhancement

Prefix Tag name to struct Name to avoid collision.

eg in api_animals.go the struct will be called AnimalsListPetsOpts and in api_pets.go the struct will be called PetsListPetsOpts.

kostyay commented 5 years ago

Pull Request: https://github.com/swagger-api/swagger-codegen/pull/9472