swagger-api / swagger-api/swagger-codegen

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

Open
#9,471 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Swagger-codegen 2.4.5 Docker command with the sample declaration, then inspect api_animals.go and api_pets.go. The work is done when the generated Go package compiles without a redeclared ListPetsOpts error and the option structs use distinct tag-prefixed names.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, openapi
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.