-
-
Notifications
You must be signed in to change notification settings - Fork 265
feat(transformers): add support for discriminator/oneOf schemas #2817
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: main
Are you sure you want to change the base?
Conversation
|
|
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Co-authored-by: mrlubos <[email protected]>
Co-authored-by: mrlubos <[email protected]>
|
when considering support for OAS 3.1/OAS3.2+), an alternative and recommended approach for discriminated unions is to use #2764 here's an example |
Problem
The
@hey-api/transformersplugin did not support discriminated unions (oneOf with discriminator). When using schemas with discriminator and oneOf, the plugin would generate a warning and fail to produce transformer functions:This prevented users from combining discriminated unions with transformers for date-time and other transformable properties.
Example
Given an OpenAPI schema with a discriminated union:
{ "Animal": { "oneOf": [ { "$ref": "#/components/schemas/Dog" }, { "$ref": "#/components/schemas/Cat" } ], "discriminator": { "propertyName": "type" } }, "Dog": { "properties": { "type": { "type": "string", "enum": ["dog"] }, "firstBark": { "type": "string", "format": "date-time" } } }, "Cat": { "properties": { "type": { "type": "string", "enum": ["cat"] }, "firstMeow": { "type": "string", "format": "date-time" } } } }Previously, no transformer would be generated. Now, the plugin correctly generates:
Solution
Added discriminated union support: Modified the transformers plugin to handle schemas with
logicalOperator === 'or'by processing each variant and collecting all transformations.Made transformations defensive: All property transformations now include existence checks to prevent creating Invalid Date objects when properties don't exist on certain union variants.
Changes
packages/openapi-ts/src/plugins/@hey-api/transformers/plugin.tsto:specs/3.1.x/transformers-discriminator-one-of.jsonTesting
Closes #2806
Original prompt
Fixes #2808
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.