Skip to content

[swift5/swift6] Fix oneOf decoding when enumUnknownDefaultCase is enabled#23496

Open
jasperpatterson wants to merge 4 commits intoOpenAPITools:masterfrom
jasperpatterson:swift-oneof-decoding-unknown-default-case
Open

[swift5/swift6] Fix oneOf decoding when enumUnknownDefaultCase is enabled#23496
jasperpatterson wants to merge 4 commits intoOpenAPITools:masterfrom
jasperpatterson:swift-oneof-decoding-unknown-default-case

Conversation

@jasperpatterson
Copy link
Copy Markdown

@jasperpatterson jasperpatterson commented Apr 8, 2026

When enumUnknownDefaultCase=true is used with oneOf types, the generated Swift decoding logic always matches the first variant and never tries subsequent ones, effectively breaking oneOf discrimination.

Root Cause

Two features interact badly:

  1. enumUnknownDefaultCase=true adds CaseIterableDefaultsLast conformance to every generated enum, making enum decoding never throw — unknown values silently fall back to .unknownDefaultOpenApi.
  2. The oneOf decoder uses try? container.decode(Variant.self) to attempt each variant in order. Since inner enum properties never fail to decode, the first variant always succeeds.

Fix

Introduces an UnknownCaseCheckable protocol that lets the oneOf decoder reject a variant whose enum properties only matched due to the unknown-case fallback:

  • Models.mustache — Defines the UnknownCaseCheckable protocol with a default implementation returning false (only when enumUnknownDefaultCase is enabled)
  • model.mustache — Generates conformance extensions only on types that have enum properties ({{#hasEnums}}):
  • modelOneOf.mustache — Adds a guard to the non-discriminator decode path:
    if let value = try? container.decode(Variant.self),
       (value as? UnknownCaseCheckable)?.containsUnknownDefaultOpenApiCase != true {

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 || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Fixes Swift oneOf decoding when enumUnknownDefaultCase=true by rejecting variants that only decode due to .unknownDefaultOpenApi, including values inside arrays. Restores correct oneOf matching without discriminators in Swift 5 and 6.

  • Bug Fixes
    • Add UnknownCaseCheckable protocol with a default false implementation (Swift5/Swift6 Models.mustache).
    • Generate conformance for enums and models with enum fields; skip oneOf interfaces. Detect unknown-defaults in single enum fields, arrays of enums, and NullEncodable-wrapped enums; map/dictionary enum fields are ignored.
    • Guard the non-discriminator oneOf decode path to skip values where containsUnknownDefaultOpenApiCase == true (Swift5/Swift6 modelOneOf.mustache).
    • Add Swift6 test to verify the guard/protocol and update Petstore samples for Swift5/Swift6.
    • No change when enumUnknownDefaultCase is disabled.

Written for commit 58dacc1. Summary will update on new commits. Review in cubic

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.

4 issues found across 23 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/swift6/model.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/swift6/model.mustache:35">
P2: Unknown-case detection skips enums inside containers, so oneOf decoding can still accept a variant even when enum elements fell back to `.unknownDefaultOpenApi`.</violation>
</file>

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

<violation number="1" location="modules/openapi-generator/src/main/resources/swift5/model.mustache:39">
P2: containsUnknownDefaultOpenApiCase skips enum containers, so unknown enum values inside arrays/maps are not detected and oneOf decoding can still accept the wrong variant.</violation>
</file>

<file name="samples/client/petstore/swift6/resultLibrary/PetstoreClient/Classes/OpenAPIs/Models/EnumArrays.swift">

<violation number="1" location="samples/client/petstore/swift6/resultLibrary/PetstoreClient/Classes/OpenAPIs/Models/EnumArrays.swift:47">
P2: UnknownCaseCheckable ignores arrayEnum, so unknown enum values in array_enum won’t be detected and oneOf discrimination can accept a variant based on unknown-enum fallback.</violation>
</file>

<file name="samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/Models/EnumArrays.swift">

<violation number="1" location="samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/Models/EnumArrays.swift:50">
P2: UnknownCaseCheckable ignores arrayEnum, so unknown enum defaults inside arrayEnum are not detected.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread modules/openapi-generator/src/main/resources/swift6/model.mustache
Comment thread modules/openapi-generator/src/main/resources/swift5/model.mustache
Copy link
Copy Markdown

@Dustin4444 Dustin4444 left a comment

Choose a reason for hiding this comment

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

Fixes #23501

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 13, 2026

thanks for the PR

cc @jgavris (2017/07) @ehyche (2017/08) @Edubits (2017/09) @jaz-ah (2017/09) @4brunu (2019/11) @dydus0x14 (2023/06)

@4brunu
Copy link
Copy Markdown
Contributor

4brunu commented Apr 28, 2026

Hi @jasperpatterson thanks for your PR. Can you please take a look at the CI errors? Thanks

@wing328 wing328 modified the milestones: 7.22.0, 7.23.0 Apr 28, 2026
@jasperpatterson
Copy link
Copy Markdown
Author

Hi @jasperpatterson thanks for your PR. Can you please take a look at the CI errors? Thanks

Hey @4brunu as far I can tell these CI errors aren't related to my changes, not really sure what to do with them.

@4brunu
Copy link
Copy Markdown
Contributor

4brunu commented Apr 28, 2026

You are correct, CI failures are not related to this PR. Can you please take a look at the open issues, related to detecting unknown in containers? Thanks

@jasperpatterson
Copy link
Copy Markdown
Author

You are correct, CI failures are not related to this PR. Can you please take a look at the open issues, related to detecting unknown in containers? Thanks

@4brunu Thanks, fixed up that issue.

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.

4 participants