Skip to content

fix(go): validate string enum exclusions - #25089

Merged
wing328 merged 10 commits into
OpenAPITools:masterfrom
AndreyVMarkelov:andreymarkelov/fix-go-not-enum
Oct 5, 2026
Merged

wing328 merged 10 commits into
OpenAPITools:masterfrom
AndreyVMarkelov:andreymarkelov/fix-go-not-enum

Conversation

@AndreyVMarkelov

@AndreyVMarkelov AndreyVMarkelov commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #25090

Fix Go client generation for string properties constrained by enum and not: { enum: [...] }.

Previously, generated models could accept values excluded by not: enum during JSON unmarshaling. This could make oneOf variants overlap when they were intended to be mutually exclusive.

This change adds validation during generated model unmarshaling so:

  • allowed enum values are enforced;
  • excluded enum values are rejected;
  • nullable enums preserve null semantics;
  • optional properties remain optional;
  • untyped not: enum does not incorrectly force a string type;
  • JSON property matching follows Go encoding/json case-insensitive field matching.

Existing UnmarshalJSON paths for required and additionalProperties models are preserved.

Related reports: #25065 (Java), #25069 (Python).

Tests

Added focused Go generator coverage for:

  • allowed and excluded string enum values
  • nullable allowed and excluded enums
  • optional properties
  • quoted string values
  • typed and untyped not: enum
  • case-insensitive JSON property names
  • validating UnmarshalJSON generation

Generated Go code compiles and passes runtime validation with go test ./....

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 4 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread modules/openapi-generator/src/main/resources/go/model_simple.mustache Outdated

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 17 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread samples/openapi3/client/petstore/go/go-petstore/model_enum_arrays.go Outdated
Comment thread samples/client/echo_api/go/model_pet.go Outdated
Comment thread samples/client/petstore/go/go-petstore/model_big_cat.go Outdated
Comment thread samples/client/petstore/go/go-petstore/model_enum_test_.go Outdated
Comment thread samples/client/others/go/allof_multiple_ref_and_discriminator/model_base_item.go Outdated
Comment thread samples/openapi3/client/petstore/go/go-petstore-aws-signature/model_pet.go Outdated
Comment thread samples/openapi3/client/petstore/go/go-petstore/model_zebra.go Outdated
Comment thread samples/client/petstore/go/go-petstore/model_order.go Outdated
Comment thread samples/openapi3/client/petstore/go/go-petstore/model_order.go Outdated
Comment thread samples/openapi3/client/petstore/go/go-petstore/model_order.go Outdated

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 5 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread modules/openapi-generator/src/main/resources/go/model_simple.mustache Outdated

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 3 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread modules/openapi-generator/src/test/resources/3_1/go/oneof-not-enum.yaml Outdated
@AndreyVMarkelov
AndreyVMarkelov force-pushed the andreymarkelov/fix-go-not-enum branch from 2bbd9d8 to 10b5284 Compare October 4, 2026 16:20

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 21 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread samples/openapi3/client/petstore/go/go-petstore/model_pet.go Outdated
Comment thread modules/openapi-generator/src/main/resources/go/model_simple.mustache Outdated

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 issue found across 22 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/go/model_simple.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/go/model_simple.mustache:510">
P2: For a required `Foo` with a default, `{"foo":""}` reaches this branch and leaves both `foo` and the defaulted `Foo` in `allProperties`; unmarshalling the marshaled map can let the alias overwrite the default. Remove or normalize the matched alias before adding the canonical default.</violation>
</file>

Comment thread modules/openapi-generator/src/main/resources/go/model_simple.mustache Outdated
@wing328

wing328 commented Oct 5, 2026

Copy link
Copy Markdown
Member

thanks for the PR

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

@@ -0,0 +1,106 @@
openapi: 3.1.0

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.

with the change in this PR, i generated a go client using this spec but got the following errors when running go test:

.\model_optional_enum_child.go:63:16: o._ undefined (type *OptionalEnumChild has no field or method _)
.\model_optional_enum_child.go:70:4: o._ undefined (type *OptionalEnumChild has no field or method _)
.\model_optional_enum_child.go:70:16: decoded._ undefined (type struct{Kind *string "json:\"kind,omitempty\""; _ *string "json:\"-,omitempty\""; Label *string "json:\"label,omitempty\""} has no field or method _)
.\model_optional_enum_parent.go:77:25: o._ undefined (type *OptionalEnumParent has no field or method _)
.\model_optional_enum_parent.go:81:12: o._ undefined (type *OptionalEnumParent has no field or method _)
.\model_optional_enum_parent.go:87:25: o._ undefined (type *OptionalEnumParent has no field or method _)
.\model_optional_enum_parent.go:90:11: o._ undefined (type *OptionalEnumParent has no field or method _)
.\model_optional_enum_parent.go:120:14: o._ undefined (type OptionalEnumParent has no field or method _)
.\model_optional_enum_parent.go:121:24: o._ undefined (type OptionalEnumParent has no field or method _)
.\model_optional_enum_parent.go:90:11: too many errors

did you encounter similar errors when testing the change locally?

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.

fixing. Agree

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.

Fixed. The - property was only exercising the datatag path and caused the generated Go identifier _, so I replaced it with the regular dash property and removed the now-unnecessary name mapping. The generated sample now compiles successfully, including go test ./... and go test -count=1 ./....

@@ -0,0 +1,40 @@
package openapi

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.

i believe this file should be put in the auto-generated go client using the test spec oneof-not-enum.yaml, right?

if that's the case, please add a config similar to ./bin/configs/go-regex-test.yaml and update the go github workflow to have it tested moving forward: https://github.com/OpenAPITools/openapi-generator/blob/master/.github/workflows/samples-go-client.yaml#L12

@AndreyVMarkelov AndreyVMarkelov Oct 5, 2026 •

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.

Done. I added a dedicated oneof-not-enum Go sample config and included it in the Go client workflow paths and matrix. The runtime test is now generated into the sample as oneof-not-enum_test.go

return objs;
}

@Override

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.

suggestion would be nice to add more comments in the new code block (line 606 - 708)

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 concise comments around the non-obvious parts of the new logic: scoping allowed-enum checks to the affected oneOf variants, handling inherited allOf properties, and matching the case-insensitive JSON field behavior used by encoding/json.

return objs;
}

private static Map<String, CodegenProperty> effectiveVars(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 docstrings to newly-created functions effectiveVars, stringEnumComparison

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 Javadocs for both effectiveVars and stringEnumComparison, describing the effective/inherited property handling and the generated string/null enum comparison behavior.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 52 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread samples/client/others/go/oneof-not-enum/docs/OptionalEnumUnion.md
Comment thread samples/client/others/go/oneof-not-enum/docs/TaggedUnion.md
Comment thread samples/client/others/go/oneof-not-enum/docs/NullableEnum.md
Comment thread samples/client/others/go/oneof-not-enum/docs/Other.md
Comment thread samples/client/others/go/oneof-not-enum/docs/AnyOther.md
Comment thread samples/client/others/go/oneof-not-enum/utils.go
@AndreyVMarkelov

Copy link
Copy Markdown
Contributor Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@AndreyVMarkelov I have started the AI code review. It will take a few minutes to complete.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 54 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread samples/client/others/go/oneof-not-enum/model_enum_null_union.go
@wing328 wing328 added this to the 7.26.0 milestone Oct 5, 2026
@wing328
wing328 merged commit 9a3fb0a into OpenAPITools:master Oct 5, 2026
23 checks passed
ondrej-simon added a commit to ondrej-simon/openapi-generator that referenced this pull request Oct 6, 2026
The sample on master is out of date after OpenAPITools#25064 and OpenAPITools#25089 were merged
concurrently, which fails the "Samples up-to-date" check.
wing328 pushed a commit that referenced this pull request Oct 7, 2026
…#25138)

* [Java][webclient/restclient/resttemplate] Simplify generated API code

- Only null-check optional header, cookie and form parameters (and use
  braces); required parameters are already validated to be non-null
- Pass the request body to invokeAPI directly instead of through a
  redundant postBody local variable
- Use the diamond operator for generic instantiations; for anonymous
  ParameterizedTypeReference classes only when targeting Java 17
- Set the java17 flag for resttemplate when using Jakarta EE and use it
  to select the Java version in its pom.xml and build.gradle (which also
  removes duplicated source/target elements with Spring Boot 4)

* Update stale go-oneof-not-enum sample

The sample on master is out of date after #25064 and #25089 were merged
concurrently, which fails the "Samples up-to-date" check.
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] String enum constraints are not enforced during JSON unmarshaling

2 participants