Skip to content

[BUG][CORE] Use intersection of required fields across anyOf/oneOf members#23670

Open
winrid wants to merge 3 commits intoOpenAPITools:masterfrom
winrid:pr/anyof-required-intersection
Open

[BUG][CORE] Use intersection of required fields across anyOf/oneOf members#23670
winrid wants to merge 3 commits intoOpenAPITools:masterfrom
winrid:pr/anyof-required-intersection

Conversation

@winrid
Copy link
Copy Markdown
Contributor

@winrid winrid commented Apr 30, 2026

Closes #23667.

The merged composed model from anyOf/oneOf adds every member's required fields to the resulting required set. Per OAS / JSON Schema semantics, a value is only guaranteed present if EVERY member of an anyOf/oneOf requires it, so the merged set should be the intersection.

response:
  anyOf:
    - $ref: '#/components/schemas/VoteResponse'   # required: [status, voteId]
    - $ref: '#/components/schemas/APIError'       # required: [status, reason, code]

Previously: status, voteId, reason, code all marked required. Now: only status is required (the only field present in both).

allOf semantics are unchanged (still union).

Affects

Every generator that consumes CodegenModel.required for non-null types or constructor signatures (Swift, Kotlin, Java POJOs, Dart, etc.). Sample regeneration may flip previously-required fields to optional in generated code where this pattern is in use; that is the intended behavior.

Test

anyOfRequiredFieldsIntersectionTest in Swift6ClientCodegenModelTest covers the contract against swift6_anyof_required.yaml (chosen because swift6 is one of the affected generators; the change itself lives in DefaultCodegen and applies everywhere). All 164 DefaultCodegenTest cases and 39 swift6 codegen tests pass.

Sample regeneration

The source change is intentionally landed without a multi-language sample regen since this affects ~every generator and the diff would be unmanageable. Maintainers may want to follow up with a samples regeneration pass; happy to spin out a follow-up PR if that's preferred.

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
    
  • File the PR against the correct branch: master (5.x.x) (patches will be cascaded from master to other branches by the maintainers)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

cc @wing328 @jimschubert

…mbers

The merged composed model from anyOf/oneOf added every member's required
fields to the resulting `required` set. Per OAS / JSON Schema semantics,
a value is only guaranteed present if EVERY member of an anyOf/oneOf
requires it, so the merged set should be the intersection.

Example:

    response:
      anyOf:
        - $ref: '#/components/schemas/VoteResponse'   # required: [status, voteId]
        - $ref: '#/components/schemas/APIError'       # required: [status, reason, code]

Previously: status, voteId, reason, code all marked required.
Now:        only status is required (the only field present in both).

allOf semantics are unchanged (still union).

Affects every generator that consumes CodegenModel.required for non-null
types or constructor signatures (Swift, Kotlin, Java POJOs, Dart, etc.).
Sample regeneration may flip previously-required fields to optional in
generated code where this pattern is in use; that is the intended
behavior.

Test: anyOfRequiredFieldsIntersectionTest in Swift6ClientCodegenModelTest
covers the contract via swift6_anyof_required.yaml (chosen because
swift6 is one of the affected generators; the change itself lives in
DefaultCodegen and applies everywhere).

Closes OpenAPITools#23667
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.

No issues found across 3 files

winrid added 2 commits April 30, 2026 15:38
Cascade from the union->intersection change in DefaultCodegen.
Fields previously marked required across all members of an anyOf/oneOf
are now optional in the merged composed model. Touches Go, Kotlin, R,
C#, Java, JavaScript, PowerShell, Rust, TypeScript, Python, Dart-dio,
Terraform samples that contain anyOf composed schemas with overlapping
required fields.
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] [Core] anyOf/oneOf marks union of required fields as required, should be intersection

1 participant