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
16.96k stars 6.04k forks source link

[Go] Types with arrays of objects produce broken code #10559

Open danielchatfield opened 3 years ago

danielchatfield commented 3 years ago
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
Geogboe commented 3 years ago

Thanks for reporting this, I'm having the exact same issue. @danielchatfield did you ever find a workaround that didn't involve re-editing the client code after generation?

When I look at the mustache template for the model it looks like it supports non-struct types so it's not clear why it's generating struct types for arrays. See: https://github.com/swagger-api/swagger-codegen/blob/35adbd552d5f99b3ff1e0e59da228becc85190f2/modules/swagger-codegen/src/main/resources/go/model.mustache#L8

I don't know much about java but it looks like the model's type is being determined here: https://github.com/swagger-api/swagger-codegen/blob/35adbd552d5f99b3ff1e0e59da228becc85190f2/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractGoCodegen.java#L226. Maybe someone else who understands this better can provide some insight?

Edit: Also note, it looks like initially when the code was added there may have been support for arrays but it got commented out: https://github.com/swagger-api/swagger-codegen/pull/7197/commits/1bd77278af10f5fd5a2afe72bc0b983746efcf65#diff-218fa02fec22b278ce7a40c9fbee90a6f7bacf730a693f5a7f5b5e8ae3aadac0R51-R52 @bvwells I know it's been a while but any idea what the problem might have been and specifically why this was commented out?

 /*instantiationTypes.put("array", "GoArray");
        instantiationTypes.put("map", "GoMap");*/