[TypeScript] Implement oneOf type resolution without discriminator
Summary
- Implements oneOf type resolution for TypeScript generator when discriminator is not present
- Adds
OneOfClasswithfindMatchingType()method to determine correct type at runtime - Enhances
ObjectSerializerto use type deduction when discriminator is unavailable - Includes comprehensive tests validating both discriminator and non-discriminator scenarios
Why
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
- Created
OneOfClassbase class withinstanceOf()method to validate objects against schema requirements - Generated oneOf models now extend
OneOfClassand includefindMatchingType()to iterate through possible types -
ObjectSerializerchecks forfindMatchingType()method when discriminator is undefined and uses it for type resolution - Falls back to original behavior if neither discriminator nor
findMatchingType()is available - Throws descriptive error when type resolution is ambiguous (object matches multiple or no schemas)
Tests
Added integration tests at samples/openapi3/client/petstore/typescript/tests/one-of/:
-
Test spec:
modules/openapi-generator/src/test/resources/3_0/typescript/oneOf.yamldefines two endpoints with oneOf responses -
Without discriminator test: Validates that
PetResponse(oneOf without discriminator) correctly deserializes toCatbased on required fields (name,petType) -
With discriminator test: Validates that
PetDiscriminatorResponse(oneOf with discriminator) correctly deserializes toDogusing discriminator property - Tests use Mocha + Chai with Nock for HTTP mocking
- Run with:
npm testin the test directory
Related issue: https://github.com/OpenAPITools/openapi-generator/issues/19868
PR checklist
- [X] Read the contribution guidelines.
- [X] Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
- [X] Run the following to build the project and update samples:
(For Windows users, please run the script in WSL) 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./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit./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. - [ ] File the PR against the correct branch:
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks) - [X] If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having
"fixes #123"present in the PR description) - [X] If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request. @TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov @davidgamero @mkusaka @joscha
The PR contains breaking change to generated TypeScript code, attributeTypeMap structure changed: Added required: boolean field to all model attribute type maps
- Before: Array<{name: string, baseName: string, type: string, format: string}>
- After: Array<{name: string, baseName: string, type: string, format: string, required: boolean}>
I can create method in model getRequiredFields() or create PR agains 8.0.0
Please check the PR and provide code review
Could you please check the PR again? I've added significant changes. Do I need to create a new PR against the 8.0 branch? @macjohnny
Any updates? @macjohnny
sorry for the delay, will try to find some time, its quite a complex PR