Skip to content

feat(go): add enumUnknownDefaultCase config option#23417

Open
marceljk wants to merge 3 commits intoOpenAPITools:masterfrom
marceljk:feat/go-add-enumUnknownDefaultCase-config
Open

feat(go): add enumUnknownDefaultCase config option#23417
marceljk wants to merge 3 commits intoOpenAPITools:masterfrom
marceljk:feat/go-add-enumUnknownDefaultCase-config

Conversation

@marceljk
Copy link
Copy Markdown
Contributor

@marceljk marceljk commented Apr 1, 2026

This PR supersedes #18748
fixes the issue that the Go generator doesn't support the enumUnknownDefaultCase config option: #18747

Credits to @jonrosner for the original pull request

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5


Summary by cubic

Adds a new enumUnknownDefaultCase option to the Go generator to safely decode unknown enum values by falling back to a default during JSON unmarshalling. Fixes #18747 and prevents errors when servers add new enum cases.

  • New Features

    • Boolean enumUnknownDefaultCase in Go generator (default: false).
    • When enabled, generated enums include unknown_default_open_api, and UnmarshalJSON falls back to it instead of returning an error.
    • Documented in docs/generators/go.md and enabled in bin/configs/go-petstore.yaml.
  • Bug Fixes

    • Fixed typos and truncation in enumUnknownDefaultCase descriptions across generator docs; added the Go docs entry.

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

@marceljk marceljk changed the title feat[GO] add enumUnknownDefaultCase config option feat(go): add enumUnknownDefaultCase config option Apr 1, 2026
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.

2 issues found across 19 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="docs/generators/go.md">

<violation number="1" location="docs/generators/go.md:23">
P3: Fix the sentence break and pluralization in this option description (missing space after the period and "enum's" should be "enums").</violation>
</file>

<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java:157">
P3: This option is already registered in DefaultCodegen, so adding it again here duplicates the CLI option entry and creates redundant config setup. Rely on the base class definition instead.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_MARSHAL_JSON, CodegenConstants.GENERATE_MARSHAL_JSON_DESC, true));
cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_UNMARSHAL_JSON, CodegenConstants.GENERATE_UNMARSHAL_JSON_DESC, true));

CliOption enumUnknownDefaultCaseOpt = CliOption.newBoolean(
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 1, 2026

Choose a reason for hiding this comment

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

P3: This option is already registered in DefaultCodegen, so adding it again here duplicates the CLI option entry and creates redundant config setup. Rely on the base class definition instead.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java, line 157:

<comment>This option is already registered in DefaultCodegen, so adding it again here duplicates the CLI option entry and creates redundant config setup. Rely on the base class definition instead.</comment>

<file context>
@@ -153,6 +153,19 @@ public GoClientCodegen() {
         cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_MARSHAL_JSON, CodegenConstants.GENERATE_MARSHAL_JSON_DESC, true));
         cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_UNMARSHAL_JSON, CodegenConstants.GENERATE_UNMARSHAL_JSON_DESC, true));
+
+        CliOption enumUnknownDefaultCaseOpt = CliOption.newBoolean(
+                CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE,
+                CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE_DESC).defaultValue(Boolean.FALSE.toString());
</file context>
Fix with Cubic

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.

In the AbstractGoCodegen, all cliOptions from the DefaultCodegen get cleared. So it's needed to configure it again

cliOptions.clear();
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "Go package name (convention: lowercase).")
.defaultValue("openapi"));
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "Go package version.")
.defaultValue("1.0.0"));
cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC)
.defaultValue(Boolean.TRUE.toString()));
}

marceljk and others added 2 commits April 1, 2026 17:05
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
@marceljk marceljk force-pushed the feat/go-add-enumUnknownDefaultCase-config branch from 722b3c4 to f33a6a3 Compare April 1, 2026 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][Go] enumUnknownDefaultCase does not return default case on JSON unmarshal

1 participant