-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Fix Swift oneOf discriminator decoding with enumUnknownDefaultCase #22635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fix Swift oneOf discriminator decoding with enumUnknownDefaultCase #22635
Conversation
This commit implements discriminator-first decoding for oneOf schemas in Swift5 and Swift6 generators to fix the bug where enumUnknownDefaultCase=true breaks discriminator-based routing. Problem: When enumUnknownDefaultCase=true is set, discriminator fields have an unknownDefaultOpenApi fallback case. With the previous sequential try? decoding approach, the first variant always matched because the discriminator field would accept any value via the fallback, causing incorrect type selection and data corruption. Solution: - Implement discriminator-first decoding strategy - When a discriminator exists, read its value FIRST using a keyed container - Switch on the discriminator value to route directly to the correct variant - Only use sequential try? decoding when NO discriminator is present Changes: - Modified swift5/modelOneOf.mustache to add discriminator support - Modified swift6/modelOneOf.mustache to add discriminator support - Added proper error messages that include the actual discriminator value - Fixed encoding bug: removed unused parameter from unknownDefaultOpenApi case - Maintained backward compatibility for non-discriminator oneOf schemas - Updated samples to reflect template changes Benefits: - Fixes discriminator-based oneOf decoding when enumUnknownDefaultCase=true - Better performance: O(1) switch vs O(n) sequential tries - Clearer error messages with actual discriminator values - No breaking changes for existing code Fixes OpenAPITools#7549
There was a problem hiding this 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 4 files
|
Hi @lastMove, thanks for the contribution. |
|
@4brunu yes I tested it, it works! |
|
That would be nice, thanks |
a17ea11 to
d7d3422
Compare
- Add 1 integration test for Swift5 generator - Add 1 integration test for Swift6 generator - Tests validate generated code uses discriminator-first decoding pattern - Tests verify switch on discriminator value instead of sequential try?
d7d3422 to
96b6a88
Compare
|
@lastMove can you merge master to your branch please? Thanks |
Description
This PR implements discriminator-first decoding for oneOf schemas in Swift5 and Swift6 generators to fix issue #7549 where
enumUnknownDefaultCase=truebreaks discriminator-based routing.Problem
When
enumUnknownDefaultCase=trueis set, discriminator fields in variant types have anunknownDefaultOpenApifallback case. With the previous sequentialtry?decoding approach:PredicateBetween)typedoesn't match (e.g., "matchesAny" ≠ "between")unknownDefaultOpenApifallback accepts it - decode succeeds!PredicateMatchesAny) is never triedSolution
Implement discriminator-first decoding:
try?approach (backward compatible)Changes
Templates Modified:
modules/openapi-generator/src/main/resources/swift5/modelOneOf.mustachemodules/openapi-generator/src/main/resources/swift6/modelOneOf.mustacheKey Implementation Details:
DiscriminatorCodingKeyenum for reading discriminator fieldunknownDefaultOpenApicaseExample Generated Code (with discriminator):
Benefits
enumUnknownDefaultCase=truePR Checklist
master(non-breaking change)cc @4brunu - This PR implements discriminator support for Swift oneOf schemas as discussed in #7549. The implementation uses discriminator-first decoding to avoid the bug where
unknownDefaultOpenApifallback cases break discriminator routing. Backward compatibility is maintained for non-discriminator oneOf schemas.Summary by cubic
Fix Swift oneOf decoding when enumUnknownDefaultCase=true by reading the discriminator first and routing to the correct variant. Keeps non-discriminator schemas using the original sequential decoding; fixes #7549.
Written for commit 96b6a88. Summary will update on new commits.