Skip to content

bug: cross-enum conflict detection is order-dependent due to GetValues() returning prefixed names #2391

Closed

Description

@natalie-o-perret

GenerateEnums prefixes enum constant names when two enums share a value (e.g. "running" in both instance-state and volume-state becomes InstanceStateRunning / VolumeStateRunning). The conflict check calls e1.GetValues(), which returns already-prefixed names for enums marked in an earlier iteration. Those prefixed names no longer match the raw values of later enums, so some conflicts are missed and affected enums stay unprefixed.

Whether an enum gets prefixed ends up depending on iteration order rather than actual value overlap.

Minimal reproduction: three string enums where A shares "running" with B and "migrating" with C. A and B are compared first and both get marked. When A is later compared with C, A.GetValues() returns ["AStateRunning", "AStateMigrating"], neither of which matches C's raw value "Migrating", so C stays unprefixed.

Fix: compare Schema.EnumValues keys instead of GetValues() output. EnumValues always holds unprefixed identifiers regardless of whether the enum has been marked yet. PR: #2390

Activity

  1. frederikheber-freiheit commented on Jun 5, 2026

    @frederikheber-freiheit

    I have just stumbled upon the same bug in our code base: Here's a minimum working example.

    spec.json
      {
        "openapi": "3.0.0",
        "info": { "title": "oapi-codegen enum conflict bug", "version": "1.0" },
        "paths": {
          "/test": {
            "get": {
              "operationId": "GetTest",
              "parameters": [
                { "in": "query", "name": "a", "schema": { "$ref": "#/components/schemas/colorA" } },
                { "in": "query", "name": "b", "schema": { "$ref": "#/components/schemas/colorB" } },
                { "in": "query", "name": "c", "schema": { "$ref": "#/components/schemas/colorC" } }
              ],
              "responses": { "200": { "description": "ok" } }
            }
          }
        },
        "components": {
          "schemas": {
            "colorA": { "type": "string", "enum": ["RED", "BLUE"] },
            "colorB": { "type": "string", "enum": ["RED", "BLUE"] },
            "colorC": { "type": "string", "enum": ["RED", "BLUE"] }
          }
        }
      }
    
      oapi-codegen.yaml
      package: demo
      generate:
        - types
      output: out.go
      compatibility:
        always-prefix-enum-values: false
    

    Depending on the order of enums, this will generate

      // ColorA and ColorB: correctly prefixed (they conflicted with each other)
      ColorABLUE ColorA = "BLUE"
      ColorARED  ColorA = "RED"
      ColorBBLUE ColorB = "BLUE"
      ColorBRED  ColorB = "RED"
      
      // ColorC: incorrectly NOT prefixed (bug)
      BLUE ColorC = "BLUE"
      RED  ColorC = "RED"
    

    while we expect

    ColorCBLUE ...
    ColorCRED ...
    

    The issues results from GenerateEnums() in pkg/codegen/codegen.go (see

    // Now, go through all the enums, and figure out if we have conflicts with
    ). In the range over enums when a collision is detected, then the loop sets e1.PrefixTypeName = true (also for e2) and puts them back into the array enums. However, this has an effect on a third enum that would collide as EnumDefinition.GetValues() (see
    func (e *EnumDefinition) GetValues() map[string]string {
    ) uses that PrefixTypeName right away and the collision will no longer be detected.

    A solution would be to first loop over all enums and just note down which ones collide. In a second step, their PrefixTypeName is then set to true. But @natalie-o-perret 's solution seems more spot-on.

  2. frederikheber-freiheit commented on Jun 5, 2026

    @frederikheber-freiheit

    And then as a (potential) remedy (for one case) as a help for others stumbling over this issue: In our code base this popped up because kin-openapi inlined enum definitions in the swagger.json instead of using $ref. See the bug getkin/kin-openapi#1158 in kin-openapi.

    This caused a lot more conflicts to show up (because of enum definitions inline multiple times) and thus the conflict resolution kicking in (with the bug making it flaky as described in the issue here).

    In our case, we updated from 0.134.0 to 0.140.0 and the additionally inlined enum definitions were removed and the issue/flakiness on our code was solved.

  3. tcarpenter-eqix commented on Oct 8, 2026

    @tcarpenter-eqix

    Can confirm; multiple endpoints sharing an inline enum value set this latent landmine for us. When we went to add a new enum, the generated client changes were way larger than expected, and it turned out to be that the names of all of those enum constants just "happened" to come out to what they did based on this bug. We are biting the one time bullet and moving to 2.8.0 and accepting the breaking change.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions