Commit 6f6f05c
committed
Fix Swift oneOf discriminator decoding with enumUnknownDefaultCase
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 #75491 parent 753330d commit 6f6f05c
4 files changed
Lines changed: 72 additions & 2 deletions
File tree
- modules/openapi-generator/src/main/resources
- swift5
- swift6
- samples/client/petstore
- swift5/oneOf/PetstoreClient/Classes/OpenAPIs/Models
- swift6/oneOf/PetstoreClient/Classes/OpenAPIs/Models
Lines changed: 34 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
23 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
24 | 56 | | |
25 | 57 | | |
26 | 58 | | |
| |||
39 | 71 | | |
40 | 72 | | |
41 | 73 | | |
| 74 | + | |
42 | 75 | | |
43 | 76 | | |
Lines changed: 34 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
23 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
24 | 56 | | |
25 | 57 | | |
26 | 58 | | |
| |||
39 | 71 | | |
40 | 72 | | |
41 | 73 | | |
| 74 | + | |
42 | 75 | | |
43 | 76 | | |
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
30 | 31 | | |
| 32 | + | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
| 29 | + | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
| |||
0 commit comments