fix(typescript-fetch): use simple union when variants already declare discriminator property#23178
Open
guizmaii wants to merge 3 commits intoOpenAPITools:masterfrom
Conversation
… discriminator property
When an OpenAPI spec declares the discriminator property on each variant
schema (e.g., as a single-value enum), the generated TypeScript union type
should use a simple union (ApiKey | Basic) instead of intersection wrappers
({ type: 'APIKEY' } & ApiKey).
The intersection wrapper causes TypeScript to evaluate
'APIKEY' & ApiKeyTypeEnum as never (string literals and string enums are
distinct nominal types), collapsing the entire union to never. This breaks
all downstream code that references the union type.
The fix detects when all discriminator variant models already have the
discriminator as a required property and sets a vendor extension flag
(x-variants-have-discriminator) on the discriminator. The templates then
conditionally skip the intersection wrapper and Object.assign calls when
this flag is set.
Backward compatibility: when variants do NOT have the discriminator
property (legacy specs), the intersection wrapper behavior is preserved.
Contributor
There was a problem hiding this comment.
1 issue found across 5 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java:497">
P2: Discriminator-variant detection ignores requiredness, so optional discriminator fields can wrongly enable simple-union generation and altered JSON conversion behavior.</violation>
</file>
Since this is your first cubic review, here's how it works:
- cubic automatically reviews your code and comments on bugs and improvements
- Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
- Add one-off context when rerunning by tagging
@cubic-dev-aiwith guidance or docs links (includingllms.txt) - Ask questions if you need clarification on any suggestion
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
...generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java
Outdated
Show resolved
Hide resolved
Extract predicates into named boolean variables to make the discriminator variant detection code easier to follow. Replace the nested loop with a stream-based lookup for finding variant models.
Update TestDiscriminatorResponse sample output to use simple unions and direct delegation since the spec's variants already declare the discriminator property.
e220f93 to
3035ab0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
When an OpenAPI spec declares the discriminator property on each variant schema (as a required single-value enum -- which is the correct behavior per the OpenAPI 3.x spec), the
typescript-fetchgenerator produces union types with intersection wrappers that break TypeScript compilation.OpenAPI spec:
Generated code (broken):
The intersection
{ type: 'APIKEY' } & ApiKeyrequirestypeto be both the string literal'APIKEY'andApiKeyTypeEnumsimultaneously. TypeScript treats string literals andconstenum members as distinct nominal types, so'APIKEY' & ApiKeyTypeEnumevaluates tonever. This collapses the discriminant property tonever, making the entire branch of the union uninhabitable:This happens whenever variant schemas correctly include the discriminator as a required property -- which is the valid approach per the OpenAPI 3.x spec.
Solution
In
TypeScriptFetchClientCodegen.postProcessAllModels(), detect when all discriminator variant models already declare the discriminator as a required property. When they do, set a vendor extension flag (x-variants-have-discriminator) on the discriminator.The Mustache templates (
modelOneOfInterfaces.mustache,modelOneOf.mustache) then conditionally:ApiKey | Basic) instead of intersection wrappers ({ type: 'APIKEY' } & ApiKey)FromJSONTyped/ToJSONinstead of usingObject.assignwith literal wrappersGenerated code (fixed):
Backward compatibility: When variants do NOT have the discriminator property (legacy/non-compliant specs), the intersection wrapper behavior is preserved unchanged.
Test plan
testOneOfModelsDoNotImportPrimitiveTypestest -- the existingoneOf.yamlspec already has variants with discriminator properties (OptionOne.discriminatorField), so the test now correctly asserts simple unionstestDiscriminatorWithoutPropertyOnVariantsUsesIntersectionWrappertest with a new spec (discriminator-without-property.yaml) to verify backward compatibility