feat(transformers): add support for discriminator/oneOf schemas #2817
+2,306
−50
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.