oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Generated code references missing enum type

Open AlbinoDrought opened this issue 4 years ago • 5 comments

openapi: 3.0.2

servers:
  - url: /api

info:
  title: Demo
  version: 0.0.1

paths:
  /demo:
    get:
      responses:
        200:
          $ref: "#/components/responses/DemoBrokenResponse"

components:
  responses:
    DemoBrokenResponse:
      description: |
        Generated struct tries to reference an enum,
        but no type gets generated for that enum.
        Code does not compile.
      content:
        application/json:
          schema:
            type: object
            required: [some_property]
            properties:
              some_property:
                type: string
                enum: ["Some Value", "Some Other Value"]

Running oapi-codegen -o repro.gen.go -package openapi -generate types repro.yml gives you

// Package openapi provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen version v1.8.3 DO NOT EDIT.
package openapi

// DemoBrokenResponse defines model for DemoBrokenResponse.
type DemoBrokenResponse struct {
	SomeProperty DemoBrokenResponseSomeProperty `json:"some_property"`
}

The DemoBrokenResponseSomeProperty type is missing.

image

Running v1.8.3

AlbinoDrought avatar Oct 19 '21 22:10 AlbinoDrought

I have confirmed this to be the case on my end. One work around is to specify a schema and then reference the schema in the response.

components:
  schemas:
    this-worky:
      title: this-worky
      description: this should worky
      type: object
      properties:
        some_property:
          type: string
          enum: ["Some Value", "Some Other Value"]
  responses:
    this-worky:
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/this-worky'
      description: this is a workaround

octomad avatar Oct 21 '21 22:10 octomad

Are there any updates on this one? We are facing the same issue and our workaround is to define it as a string instead which is not great. Thank you!

sirotinhighline avatar May 11 '23 17:05 sirotinhighline

any updates?

o1o1o3 avatar Jul 15 '24 13:07 o1o1o3

I think I have got to the same issue

drummonds avatar Aug 19 '24 15:08 drummonds

Bumping this issue up. While referencing in a schema works as a workaround, it would be ideal to have this resolved to avoid cluttering schemas with types that are only used once in this specific context.

obrhoff avatar Sep 24 '24 10:09 obrhoff

I am having the same issue and workaround doesnt work for me as i have separate my spec into multiple packages :|

wgarunap avatar Feb 19 '25 18:02 wgarunap