Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kostyay opened this issue May 30, 2019 · 1 comment
Open

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

kostyay opened this issue May 30, 2019 · 1 comment

Comments

@kostyay
Copy link
Contributor

kostyay commented May 30, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant