[go] add useAnyOfAllMatches option for spec-compliant anyOf unmarshalling#23194
Open
AriehSchneier wants to merge 4 commits intoOpenAPITools:masterfrom
Open
[go] add useAnyOfAllMatches option for spec-compliant anyOf unmarshalling#23194AriehSchneier wants to merge 4 commits intoOpenAPITools:masterfrom
AriehSchneier wants to merge 4 commits intoOpenAPITools:masterfrom
Conversation
…ling Add useAnyOfAllMatches option (default false) to try all anyOf schemas and populate every matching field, instead of returning on first match. Default behaviour is unchanged. Includes tests and test YAML spec.
Contributor
There was a problem hiding this comment.
1 issue found across 6 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="modules/openapi-generator/src/main/resources/go/model_anyof.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/go/model_anyof.mustache:62">
P1: `useAnyOfAllMatches` can populate multiple anyOf schema fields, but `MarshalJSON` still returns only the first non-nil schema, causing silent lossy, order-dependent re-serialization.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
When useAnyOfAllMatches=true, UnmarshalJSON can populate multiple anyOf
schema fields simultaneously. The previous MarshalJSON returned on the
first non-nil field, which silently discarded all other populated fields,
making round-trip serialization lossy and order-dependent.
Fix: under useAnyOfAllMatches, MarshalJSON marshals every non-nil schema
field to a map[string]interface{} and merges the results before encoding.
Non-object schemas (primitives, arrays) that cannot be merged are skipped.
The default (first-match) path is unchanged.
Contributor
There was a problem hiding this comment.
1 issue found across 2 files (changes from recent commits).
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="modules/openapi-generator/src/main/resources/go/model_anyof.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/go/model_anyof.mustache:112">
P2: useAnyOfAllMatches silently drops non-object anyOf variants during MarshalJSON because it only treats map-unmarshal success as data, leading to data loss or nil output for primitive/array matches.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
…oss anyOf schemas When multiple anyOf object schemas share the same field name and are both populated, the merge loop now compares existing and incoming values by their JSON representation. If they differ, MarshalJSON returns an error instead of silently overwriting (last-write-wins), which would produce incorrect output. Identical values for a shared key are allowed (merge succeeds normally). Also adds: - anyof_primitive_schemas.yaml test spec (string | integer anyOf) - anyof_clashing_fields.yaml test spec (two object schemas sharing 'id' with different types) - testAnyOfMarshalConflictingKeysReturnsError in GoClientCodegenTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The
anyOfspecification requires that data validates against one or more of the subschemas. The current generatedUnmarshalJSONforanyOfschemas returns on the first successful match, which means:This is the same pattern used by C# and Java generators, but it is not spec-compliant.
Fix
Add a new generator option
useAnyOfAllMatches(default:false) that, when enabled, tries all anyOf schemas and populates every field whose schema matches. Returnsnilif at least one schema matched, or an error if none did.The default remains
falseto preserve backwards compatibility with existing generated code.Changes
GoClientCodegen.java: addUSE_ANYOF_ALL_MATCHESconstant,useAnyOfAllMatchesfield, CLI option,processOptshandling, and gettermodel_anyof.mustache:{{#useAnyOfAllMatches}}block emits a try-all + match-count loop;{{^useAnyOfAllMatches}}preserves existing first-match behaviourGoClientOptionsProvider/GoClientOptionsTest: coverage for the new optionanyof_multiple_matches.yaml: test spec with two schemas (SpecAhasname,SpecBhasvalue) that can simultaneously match the same payload under aContactanyOfGoClientCodegenTest: two new tests verifying default behaviour is unchanged and new behaviour works correctlyPR checklist
./bin/generate-samples.shGoClientCodegenTestplus options test coverageSummary by cubic
Adds
useAnyOfAllMatchesto the Go generator for spec-compliantanyOfhandling. When enabled, unmarshal tries all subschemas and populates every matching field; marshal merges object fields, errors on conflicting values, and falls back to the first non-object value. Default remains first-match.New Features
useAnyOfAllMatches(default:false): unmarshal tries allanyOfschemas; succeeds if at least one matches; populates all matching fields. Marshal merges object keys from all non-nil schemas, errors on conflicting values for the same key, and falls back to the first non-object value.model_anyof.mustache: adds try-all loop inUnmarshalJSONand merged output with conflict detection and primitive fallback inMarshalJSONwhen enabled; preserves current first-match paths otherwise.GoClientCodegen: adds option constant, CLI flag, processing, and getter. Tests cover default vs. enabled behavior, object merge with conflict detection (anyof_clashing_fields.yaml), primitive fallback (anyof_primitive_schemas.yaml), and multiple matches (anyof_multiple_matches.yaml).Migration
--additional-properties useAnyOfAllMatches=true. Default behavior is unchanged.Written for commit e0a11c7. Summary will update on new commits.