Skip to content

[BUG] Go generator: no way to generate enums of integers with a named variable #21203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hw-claudio opened this issue May 2, 2025 · 3 comments

Comments

@hw-claudio
Copy link

There is currently no way to have the go code generator create a variable of integer type, with named const definitions for it.

I tried x-enum-varnames, x-enum-names,
oneOf: with title: ,

nothing at all seems to be supported for the Go generator.

This is pretty bad as I will need to open code each single variable for each enum.

Only enums with type: string currently work with the Go generator, which is too wasteful of space for my project, will need to use another generator.

@hw-claudio
Copy link
Author

I found a way to make it work, but this is still a bug.

Using x-enum-varnames DOES work, but specifically ONLY if using a separate component
that is #ref ed .

It does not work to have a structured component with an item inside that is of type: enum

For example one needs to do (WORKS):

vmruninfo:
  type: object
  properties:
    runstate:
      $ref: '#/components/schemas/vmrunstate'

...

vmrunstate:
  type: integer
  format: int16
  enum: [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
  x-enum-varnames:
    - RUNSTATE_NONE
    - RUNSTATE_POWEROFF
    - RUNSTATE_STARTUP
    - RUNSTATE_RUNNING
    - RUNSTATE_PAUSED
    - RUNSTATE_MIGRATING
    - RUNSTATE_TERMINATING
    - RUNSTATE_PMSUSPENDED
    - RUNSTATE_CRASHED

It does NOT work to inline it as follows:

vmruninfo:
  type: object
  properties:
    runstate:
      type: integer
      format: int16
      enum: [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
      x-enum-varnames:
      ...

The code generation will silently skip generating the const for the values in x-enum-varnames.

@ctreatma
Copy link
Contributor

You can configure the generator to resolve these inline enums without moving them to a $ref'ed schema by running the generator with --inline-schema-options RESOLVE_INLINE_ENUMS=true. If you're using a config file, you can specify that option in your config file instead:

{
    <your existing config>,
    "inlineSchemaOptions": {
        "RESOLVE_INLINE_ENUMS": true
    }
}

There are other customizations as well that can be useful to change how your code is generated based on the input spec: https://openapi-generator.tech/docs/customization/#inline-schema-naming

Also, a heads up that you may want to run the generator with disallowAdditionalPropertiesIfNotPresent=false so that your generated code will follow OpenAPI / JSONSchema rules for additionalProperties; the generator does not do that by default at the moment and it causes a lot of confusion.

@hw-claudio
Copy link
Author

oh I see, it's an optional feature. I didn't know that the --inline-schema-options existed, works as intended then.
I ended up just putting all enums as separate schemas, which works, but now I know I am not constrained by that.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants