swagger-api / swagger-api/swagger-codegen

[Go] Types with arrays of objects produce broken code

Open
#10,559 2 comments 3 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

If an API has an array of objects it currently produces broken Go code – it produces an empty struct rather than a type that represents a slice of objects.

Swagger-codegen version

2.4.17

Swagger declaration file content or url

A minimal example can be seen by generating a Go client from the following.

swagger: '2.0'
info:
  description: 'Example of codegen bug'
  version: 0.0.0
  title: Codegen Bug
  termsOfService: 'http://swagger.io/terms/'
  license:
    name: Apache 2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
host: foo.example.com
schemes:
  - https
paths:
  '/foo':
    get:
      summary: Foo
      description: 'example request'
      operationId: foo
      produces:
        - application/json
      responses:
        '200':
          description: successful operation
          schema:
            $ref: '#/definitions/FooResponse'
definitions:
  FooResponse:
    type: array
    minItems: 0
    maxItems: 10
    items:
      type: object
      properties:
        Bar:
          type: string
          description: Example field
          enum:
            - "a"
            - "b"
Command line used for generation

Used editor.swagger.io

Steps to reproduce

Paste the above into editor.swagger.io and then click generate Go client.

Suggest a fix/enhancement

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 reproducing the issue with the provided Swagger YAML in editor.swagger.io and inspect the generated Go client for FooResponse. Trace how an array schema whose items are inline objects is represented during Go generation. Done means the generated type represents a slice of objects rather than an empty struct, with the minimal example still generating valid Go code.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
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.