fix(go): emit the oneOf no-match error once instead of once per member - #24683
Open
kdelay wants to merge 1 commit into
Open
fix(go): emit the oneOf no-match error once instead of once per member#24683kdelay wants to merge 1 commit into
kdelay wants to merge 1 commit into
Conversation
The no-match branch of the generated UnmarshalJSON was wrapped in a
{{#oneOf}} section, so the error return introduced by OpenAPITools#24349 was rendered
once per oneOf member. Only the first copy is reachable; the rest is dead
code that grows with the number of members.
Render the block once, outside the section. err is declared unconditionally
at the top of UnmarshalJSON, so the {{^oneOf}} fallback is no longer needed.
Behaviour is unchanged: the first copy is what executed before.
Regenerate the affected go samples.
fix OpenAPITools#24661
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
go/model_oneof.mustacherenders the "no match" error return inside a{{#oneOf}}section, so theif err != nil { ... } else { ... }block added by #24349 is emitted once peroneOfmember. Only the first copy can ever run; the remaining copies are dead code and grow with the member count.Committed sample output shows it today, e.g.
samples/openapi3/client/petstore/go/go-petstore/model_one_of_primitive_type.go(3 members, so 3 copies).The fix renders the block once, outside the section.
erris declared unconditionally asvar err errorat the top ofUnmarshalJSON, so the{{^oneOf}}fallback that existed for the empty-oneOfcase is no longer needed. Theelseis dropped in favour of an early return, which is what gofmt-style Go looks like here.Behaviour is unchanged: the first (and only reachable) copy is exactly what the new single block renders. Both branches of the template (
useOneOfDiscriminatorLookupon and off) had the same duplication and both are fixed.fix #24661
Verification
./mvnw clean package -DskipTests— BUILD SUCCESS../bin/generate-samples.sh ./bin/configs/go-*.yaml— 21 generators, no unrelated churn; 8 sample files changed, all of them the duplicated block collapsing to one.Go generator unit tests:
AbstractGoCodegenTest,GoClientCodegenTest,GoClientOptionsTest,GoModelTest— 53 tests, 0 failures.go build ./...andgo vet ./...on the regeneratedsamples/openapi3/client/petstore/go/go-petstoreandsamples/client/others/go/oneof-anyof-required(golang:1.23) — both clean.Runtime check that the
#24349behaviour survives, against the regenerated petstore sample:PR checklist
master./cc @antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5
Summary by cubic
Emit the
oneOfno-match error once in generated GoUnmarshalJSONmethods instead of once per member, removing unreachable duplicates asoneOfgrows. Behavior is unchanged; regenerated Go samples now contain a single error block.{{#oneOf}}section inmodel_oneof.mustache; drop the empty-oneOffallback and replaceelsewith an early return.Written for commit 0050666. Summary will update on new commits.