| title | OpenAPI 3.2: Use discriminator defaultMapping for more accurate union types | ||
|---|---|---|---|
| parentIssue | 1673 | ||
| labels |
|
||
| assignees | |||
| milestone |
OpenAPI 3.2 adds a defaultMapping field to the Discriminator Object. This field specifies which schema to use when propertyName is absent or the discriminator value is not found in the mapping. It improves the accuracy of polymorphic schema representations.
SchemaTypeCoder.writeGroup() in src/typescript-generator/schema-type-coder.ts generates union types based on allOf/anyOf/oneOf but does not inspect the discriminator.defaultMapping field. As a result, the default variant of a discriminated union may be missing from the generated TypeScript type.
- In
SchemaTypeCoder.writeGroup(), read thediscriminator.defaultMappingfield when present - Include the default variant schema in the emitted union type so the generated TypeScript accurately represents all possible values
- A discriminated schema with
defaultMappinggenerates a union type that includes the default variant - The generated TypeScript for such a schema passes
tsc --noEmit - A discriminated schema without
defaultMappingcontinues to generate the same types as before - A unit test covers a discriminator with
defaultMappingand confirms the correct union type is emitted