Skip to content

Issue generating enum in paths #399

@vishen

Description

@vishen

I may be doing something wrong, or using it wrong, but I am getting errors when generating the HTTP client with the following openapi spec:

# openapi.yaml

openapi: 3.0.0
info:
  description: example
  title: example-api
  version: 0.0.1
paths:
  '/path':
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    enum:
                      - online
                      - offline
                    example: 'online'
components:
  schemas: {}

Using master, I generate this with: go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen -package=clients -generate=client,types -o http.gen.go openapi.yaml

And that generates the following Go code:

...
type GetPathResponse struct {
        Body         []byte
        HTTPResponse *http.Response
        JSON200      *struct {
                Status *N200Status `json:"status,omitempty"`
        }
}
...

However N200Status doesn't exist as a type, it hasn't been generated for some reason. Prior to #241 it used to generate it as:

type GetPathResponse struct {
        Body         []byte
        HTTPResponse *http.Response
        JSON200      *struct {
                Status *string `json:"status,omitempty"`
        }
}

I think the issue might be because enum types are only generated for swagger.Components but this enum is on swagger.Paths: https://github.com/deepmap/oapi-codegen/blob/master/pkg/codegen/codegen.go#L301-L304

I am able to get around this issue by moving the enum into the components section which will then generate all the types:

openapi: 3.0.0
info:
  description: example
  title: example-api
  version: 0.0.1
paths:
  '/path':
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Response'
components:
  schemas:
    Response:
      type: object
      properties:
        status:
          type: string
          enum:
            - online
            - offline
          example: 'online'

Just wondering if this expected behaviour and if I should always put everything in the components section?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions