[typescript-fetch] Fix `instanceOf` type guards for discriminated unions
The generated instanceOf functions 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
export function instanceOfOptionOne(value: object): value is OptionOne {
if (!('discriminatorField' in value) || value['discriminatorField'] === undefined) return false;
return true;
}
Both instanceOfOptionOne and instanceOfOptionTwo return true for { discriminatorField: 'optionTwo' }.
After
// When name === baseName (e.g., discriminatorField) — simple single check
export function instanceOfOptionOne(value: object): value is OptionOne {
if (!('discriminatorField' in value) || value['discriminatorField'] === undefined) return false;
if (value['discriminatorField'] !== 'optionOne') return false;
return true;
}
// When name !== baseName (e.g., discriminatorField vs discriminator-field) — dual check
export function instanceOfDashedOptionOne(value: object): value is DashedOptionOne {
if ((!('discriminatorField' in value) && !('discriminator-field' in value)) || (value['discriminatorField'] === undefined && value['discriminator-field'] === undefined)) return false;
if (value['discriminatorField'] !== 'dashedOptionOne' && value['discriminator-field'] !== 'dashedOptionOne') return false;
return true;
}
How it works
- baseName fix: Each required field check now accepts either the TypeScript property name (
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. - Discriminator value fix: For required enum properties with exactly one allowed value (i.e., discriminator fields on oneOf variants), an additional value check is generated.
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
- Read the contribution guidelines.
- 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.
- Run the following to build the project and update samples:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
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. - File the PR against the correct branch:
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks) - If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having
"fixes #123"present in the PR description) - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
Summary by cubic
Fixes instanceOf type guards in typescript-fetch for discriminated unions so they validate discriminator values and work with name vs baseName (snake_case and dashed). Also updates oneOf encoding to use wire-format discriminator keys and tightens decoding and enum handling.
- Bug Fixes
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.