Skip to content

fix(go): avoid duplicate unknown enum defaults#23909

Merged
wing328 merged 2 commits into
OpenAPITools:masterfrom
SJvaca30:fix-go-enum-unknown-default-prefix
Jun 1, 2026
Merged

fix(go): avoid duplicate unknown enum defaults#23909
wing328 merged 2 commits into
OpenAPITools:masterfrom
SJvaca30:fix-go-enum-unknown-default-prefix

Conversation

@SJvaca30
Copy link
Copy Markdown
Contributor

@SJvaca30 SJvaca30 commented May 30, 2026

Fixes #23844.

What

  • Adds a model-specific prefix to the synthetic Go unknown_default_open_api enum fallback when enumClassPrefix=false.
  • Leaves user-defined enum constants unprefixed.
  • Adds regression coverage with two enum models generated into the same Go package.

Testing

  • ./mvnw -pl modules/openapi-generator -am -Dtest=org.openapitools.codegen.go.GoClientCodegenTest#testEnumUnknownDefaultCaseUsesModelSpecificNamesWhenEnumClassPrefixDisabled -Dsurefire.failIfNoSpecifiedTests=false test
  • ./mvnw -pl modules/openapi-generator -am -Dtest=org.openapitools.codegen.go.GoClientCodegenTest -Dsurefire.failIfNoSpecifiedTests=false test
  • git diff --check

Summary by cubic

Avoids duplicate Go enum fallback constants by prefixing the synthetic unknown default with the model name when enumClassPrefix=false. Fixes #23844.

  • Bug Fixes
    • Prefixes the unknown_default_open_api fallback with the model name when enumUnknownDefaultCase=true and enumClassPrefix=false, preventing collisions across models in the same package.
    • Leaves user-defined enum constants unchanged.
    • Adds regression test with two enum models to verify unique names (e.g., STATUSA_UNKNOWN_DEFAULT_OPEN_API, STATUSB_UNKNOWN_DEFAULT_OPEN_API).
    • Documents the prefixing behavior in the Go generator docs.

Written for commit e230699. Summary will update on new commits.

Review in cubic

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

Re-trigger cubic

@wing328 wing328 added this to the 7.23.0 milestone Jun 1, 2026
}

@SuppressWarnings("unchecked")
private void prefixEnumUnknownDefaultCase(CodegenModel model) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a docstring explaining what this function does

a few inline comments explaining the if blocks below would be helpful too

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added Javadoc for prefixEnumUnknownDefaultCase and inline comments for the guard conditions and fallback-name update.

@wing328
Copy link
Copy Markdown
Member

wing328 commented Jun 1, 2026

tested locally and the result is good (no compilation error)

--- a/model_status_a.go
+++ b/model_status_a.go
@@ -22,7 +22,7 @@ type StatusA string
 const (
        ACTIVE StatusA = "active"
        INACTIVE StatusA = "inactive"
-       UNKNOWN_DEFAULT_OPEN_API StatusA = "unknown_default_open_api"
+       STATUSA_UNKNOWN_DEFAULT_OPEN_API StatusA = "unknown_default_open_api"
 )

 // All allowed values of StatusA enum
@@ -46,7 +46,7 @@ func (v *StatusA) UnmarshalJSON(src []byte) error {
                }
        }

-       *v = UNKNOWN_DEFAULT_OPEN_API
+       *v = STATUSA_UNKNOWN_DEFAULT_OPEN_API
        return nil
 }

will merge once all the tests pass

@wing328
Copy link
Copy Markdown
Member

wing328 commented Jun 1, 2026

cc @antihax (2017/11) @grokify (2018/07) @kemokemo (2018/09) @jirikuncar (2021/01) @ph4r5h4d (2021/04) @lwj5 (2023/04)

@wing328 wing328 merged commit 5044061 into OpenAPITools:master Jun 1, 2026
15 checks passed
@wing328
Copy link
Copy Markdown
Member

wing328 commented Jun 1, 2026

Thanks for the PR which has been merged.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][Go] enumUnknownDefaultCase generates colliding UNKNOWN_DEFAULT_OPEN_API constants when enumClassPrefix=false

2 participants