[typescript-fetch] Fix instanceOf type guards for discriminated unions#23497
[typescript-fetch] Fix instanceOf type guards for discriminated unions#23497jasperpatterson wants to merge 6 commits intoOpenAPITools:masterfrom
instanceOf type guards for discriminated unions#23497Conversation
There was a problem hiding this comment.
2 issues found across 27 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/resources/typescript-fetch/modelGeneric.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache:40">
P2: Enum discriminator check always compares against a quoted literal, which breaks numeric/boolean singleton enums by forcing string comparison in instanceOf guards.</violation>
</file>
<file name="samples/client/petstore/typescript-fetch/builds/oneOf/models/TestDashedDiscriminatorResponse.ts">
<violation number="1" location="samples/client/petstore/typescript-fetch/builds/oneOf/models/TestDashedDiscriminatorResponse.ts:65">
P2: Serializer emits camelCase discriminatorField instead of dashed wire name, causing mismatch with FromJSON and model serializers.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
|
cc @TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02) @davidgamero (2022/03) @mkusaka (2022/04) @joscha (2024/10) @dennisameling (2026/02) |
macjohnny
left a comment
There was a problem hiding this comment.
thanks for your contribution
| {{#vars}} | ||
| {{#required}} | ||
| {{#hasSanitizedName}} | ||
| if ((!('{{name}}' in value) && !('{{baseName}}' in value)) || (value['{{name}}'] === undefined && value['{{baseName}}'] === undefined)) return false; |
There was a problem hiding this comment.
shouldnt it be either name or baseName?
There was a problem hiding this comment.
instanceOf is called in both directions, so it needs to handle both casings:
- Decoding in
FromJSONTyped: called on raw JSON, where keys usebaseName(e.g.,discriminator-field) - Encoding in
ToJSONTyped: called on the TS object, where keys usename(e.g.,discriminatorField)
The {{#hasSanitizedName}} guard ensures we only emit the dual check when it's actually needed — for the common case where name === baseName, it falls back to the simpler single check.
I did consider making the function aware of direction but it felt too complex for not much gain, and would change the public instanceOf API.
a333298 to
7fdd856
Compare
…r-discriminated-unions # Conflicts: # samples/client/petstore/typescript-fetch/builds/oneOf/apis/DefaultApi.ts
The generated
instanceOffunctions have two bugs:Discriminator value not checked: For oneOf unions where variants share the same discriminator field (e.g.,
type),instanceOfonly checks field presence, not its value.Field name casing mismatch:
instanceOfchecksname(camelCase, e.g.,someProperty) but when called on raw JSON inFromJSONTyped, the keys usebaseName(original casing, e.g.,some_propertyorsome-property). This causesinstanceOfto fail on raw JSON whenname !== baseName.Before
Both
instanceOfOptionOneandinstanceOfOptionTworeturntruefor{ discriminatorField: 'optionTwo' }.After
How it works
name) or the original JSON property name (baseName). This is needed becauseinstanceOfis called on both raw JSON (decoding) and converted TS objects (encoding). The template uses{{#hasSanitizedName}}to only emit the dualname/baseNamecheck when the two actually differ, keeping the common case clean.ExtendedCodegenPropertyfix: ThehasSanitizedNameflag (already computed byDefaultCodegen) was not being copied inExtendedCodegenProperty's constructor, so the template conditional was always false. Added the missing copy.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)Summary by cubic
Fixes
instanceOftype guards intypescript-fetchfor discriminated unions so they validate discriminator values and work withnamevsbaseName(snake_case and dashed). Also updatesoneOfencoding to use wire-format discriminator keys and tightens decoding and enum handling.instanceOf: validate discriminator values for single-value enums (string or numeric); check bothnameandbaseNamefor required fields when they differ (viahasSanitizedName) without duplicating checks when they match. EnsurehasSanitizedNameis copied inExtendedCodegenProperty.ToJSONTyped: emit the discriminator using the wire-format key (propertyBaseName); keep numeric singleton enum literals unquoted.FromJSONTyped: fixoneOfdecoding and array type narrowing to avoid TS “every on never” errors and supportenumUnknownDefaultCase. Do not flattenoneOfvariant properties when anallOfitem is a$refto aoneOfschema without a discriminator.Written for commit 2416c5b. Summary will update on new commits.