Skip to content

fix(go): emit the oneOf no-match error once instead of once per member - #24683

Open
kdelay wants to merge 1 commit into
OpenAPITools:masterfrom
kdelay:fix/issue-24661-go-oneof-no-match-error
Open

fix(go): emit the oneOf no-match error once instead of once per member#24683
kdelay wants to merge 1 commit into
OpenAPITools:masterfrom
kdelay:fix/issue-24661-go-oneof-no-match-error

Conversation

@kdelay

@kdelay kdelay commented Aug 12, 2026

Copy link
Copy Markdown

Description

go/model_oneof.mustache renders the "no match" error return inside a {{#oneOf}} section, so the if err != nil { ... } else { ... } block added by #24349 is emitted once per oneOf member. 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. err is declared unconditionally as var err error at the top of UnmarshalJSON, so the {{^oneOf}} fallback that existed for the empty-oneOf case is no longer needed. The else is 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 (useOneOfDiscriminatorLookup on 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 ./... and go vet ./... on the regenerated samples/openapi3/client/petstore/go/go-petstore and samples/client/others/go/oneof-anyof-required (golang:1.23) — both clean.

  • Runtime check that the #24349 behaviour survives, against the regenerated petstore sample:

    payload true    -> data failed to match schemas in oneOf(OneOfPrimitiveType): json: cannot unmarshal bool into Go value of type int32
    payload "abc"   -> data failed to match schemas in oneOf(OneOfPrimitiveType): json: cannot unmarshal string into Go value of type int32
    

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.
  • File the PR against the correct branch: master.
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

/cc @antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5


Summary by cubic

Emit the oneOf no-match error once in generated Go UnmarshalJSON methods instead of once per member, removing unreachable duplicates as oneOf grows. Behavior is unchanged; regenerated Go samples now contain a single error block.

  • Bug Fixes
    • Move the no-match error block outside the {{#oneOf}} section in model_oneof.mustache; drop the empty-oneOf fallback and replace else with an early return.
    • Apply the fix to both discriminator paths; update Go samples to collapse duplicated error code.

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

Review in cubic

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

@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.

No issues found across 9 files

Re-trigger cubic

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] model_oneof.mustache renders the "no match" error block once per oneOf member instead of once, causing duplicated/unreachable code

1 participant