-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[TypeScript] Implement oneOf type resolution without discriminator #22201
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?
[TypeScript] Implement oneOf type resolution without discriminator #22201
Conversation
|
The PR contains breaking change to generated TypeScript code, |
modules/openapi-generator/src/main/resources/typescript/model/ObjectSerializer.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/typescript/model/OneOfClass.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/typescript/model/OneOfClass.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/typescript/model/OneOfClass.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/typescript/model/OneOfClass.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/typescript/model/modelOneOf.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/typescript/model/modelOneOf.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/typescript/model/modelOneOf.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/typescript/model/modelOneOf.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/typescript/model/modelOneOf.mustache
Outdated
Show resolved
Hide resolved
macjohnny
left a comment
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.
thanks for the changes!
modules/openapi-generator/src/main/resources/typescript/model/TypeMatcher.mustache
Outdated
Show resolved
Hide resolved
|
Could you please check the PR again? |
Summary
OneOfClasswithfindMatchingType()method to determine correct type at runtimeObjectSerializerto use type deduction when discriminator is unavailableWhy
Previously, the TypeScript generator could only resolve oneOf types when a discriminator property was defined in the OpenAPI spec. This limitation meant that valid oneOf schemas without discriminators would fail at runtime.
This change enables automatic type resolution by checking which schema matches the provided data based on required fields, making the generated TypeScript clients more robust and easier to use for specs that don't define discriminators.
Implementation Details
OneOfClassbase class withinstanceOf()method to validate objects against schema requirementsOneOfClassand includefindMatchingType()to iterate through possible typesObjectSerializerchecks forfindMatchingType()method when discriminator is undefined and uses it for type resolutionfindMatchingType()is availableTests
Added integration tests at
samples/openapi3/client/petstore/typescript/tests/one-of/:modules/openapi-generator/src/test/resources/3_0/typescript/oneOf.yamldefines two endpoints with oneOf responsesPetResponse(oneOf without discriminator) correctly deserializes toCatbased on required fields (name,petType)PetDiscriminatorResponse(oneOf with discriminator) correctly deserializes toDogusing discriminator propertynpm testin the test directoryRelated issue: #19868
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)"fixes #123"present in the PR description)@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov @davidgamero @mkusaka @joscha