Skip to content

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

Open
jasperpatterson wants to merge 3 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 3 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 skipping variants that only decode due to .unknownDefaultOpenApi fallbacks. This restores correct variant detection for oneOf without discriminators in Swift 5 and 6.

  • Bug Fixes
    • Add UnknownCaseCheckable protocol with a default false implementation (Swift5/Swift6 Models.mustache).
    • Generate UnknownCaseCheckable conformance only for types/enums with enum properties; skip arrays, maps, and oneOf interfaces.
    • Guard oneOf decoding to reject values where containsUnknownDefaultOpenApiCase == true (Swift5/Swift6 modelOneOf.mustache).
    • Add Swift6 test to verify guard generation; update Petstore samples for Swift5/Swift6.
    • No change when enumUnknownDefaultCase is disabled.

Written for commit 97c5c3b. Summary will update on new commits.

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.

{{#allVars}}
{{#isEnum}}
{{^isContainer}}
{{#vendorExtensions.x-null-encodable}}
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 8, 2026

Choose a reason for hiding this comment

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

P2: Unknown-case detection skips enums inside containers, so oneOf decoding can still accept a variant even when enum elements fell back to .unknownDefaultOpenApi.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/swift6/model.mustache, line 35:

<comment>Unknown-case detection skips enums inside containers, so oneOf decoding can still accept a variant even when enum elements fell back to `.unknownDefaultOpenApi`.</comment>

<file context>
@@ -26,4 +26,40 @@ extension {{projectName}}API {
+        {{#allVars}}
+        {{#isEnum}}
+        {{^isContainer}}
+        {{#vendorExtensions.x-null-encodable}}
+        if {{{name}}} == .encodeValue(.unknownDefaultOpenApi) { return true }
+        {{/vendorExtensions.x-null-encodable}}
</file context>
Fix with Cubic

{{#allVars}}
{{#isEnum}}
{{^isContainer}}
{{#vendorExtensions.x-null-encodable}}
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 8, 2026

Choose a reason for hiding this comment

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

P2: containsUnknownDefaultOpenApiCase skips enum containers, so unknown enum values inside arrays/maps are not detected and oneOf decoding can still accept the wrong variant.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/swift5/model.mustache, line 39:

<comment>containsUnknownDefaultOpenApiCase skips enum containers, so unknown enum values inside arrays/maps are not detected and oneOf decoding can still accept the wrong variant.</comment>

<file context>
@@ -30,4 +30,40 @@ extension {{projectName}}API {
+        {{#allVars}}
+        {{#isEnum}}
+        {{^isContainer}}
+        {{#vendorExtensions.x-null-encodable}}
+        if {{{name}}} == .encodeValue(.unknownDefaultOpenApi) { return true }
+        {{/vendorExtensions.x-null-encodable}}
</file context>
Fix with Cubic


extension EnumArrays: UnknownCaseCheckable {
internal var containsUnknownDefaultOpenApiCase: Bool {
if justSymbol == .unknownDefaultOpenApi { return true }
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 8, 2026

Choose a reason for hiding this comment

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

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.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/swift6/resultLibrary/PetstoreClient/Classes/OpenAPIs/Models/EnumArrays.swift, line 47:

<comment>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.</comment>

<file context>
@@ -41,3 +41,10 @@ internal struct EnumArrays: Sendable, Codable {
+
+extension EnumArrays: UnknownCaseCheckable {
+    internal var containsUnknownDefaultOpenApiCase: Bool {
+        if justSymbol == .unknownDefaultOpenApi { return true }
+        return false
+    }
</file context>
Fix with Cubic

Comment on lines +50 to +52
if justSymbol == .unknownDefaultOpenApi { return true }
return false
}
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 8, 2026

Choose a reason for hiding this comment

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

P2: UnknownCaseCheckable ignores arrayEnum, so unknown enum defaults inside arrayEnum are not detected.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/Models/EnumArrays.swift, line 50:

<comment>UnknownCaseCheckable ignores arrayEnum, so unknown enum defaults inside arrayEnum are not detected.</comment>

<file context>
@@ -44,3 +44,10 @@ internal struct EnumArrays: Codable, JSONEncodable {
+
+extension EnumArrays: UnknownCaseCheckable {
+    internal var containsUnknownDefaultOpenApiCase: Bool {
+        if justSymbol == .unknownDefaultOpenApi { return true }
+        return false
+    }
</file context>
Suggested change
if justSymbol == .unknownDefaultOpenApi { return true }
return false
}
if justSymbol == .unknownDefaultOpenApi { return true }
if arrayEnum?.contains(.unknownDefaultOpenApi) == true { return true }
return false
Fix with Cubic

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

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.

2 participants