[typescript-fetch] Fix `instanceOf` type guards for discriminated unions

Merged
JA
6mo ago
+1229-9

The generated instanceOf functions have two bugs:

  1. Discriminator value not checked: For oneOf unions where variants share the same discriminator field (e.g., type), instanceOf only checks field presence, not its value.

  2. Field name casing mismatch: instanceOf checks name (camelCase, e.g., someProperty) but when called on raw JSON in FromJSONTyped, the keys use baseName (original casing, e.g., some_property or some-property). This causes instanceOf to fail on raw JSON when name !== 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 because instanceOf is called on both raw JSON (decoding) and converted TS objects (encoding). The template uses {{#hasSanitizedName}} to only emit the dual name/baseName check 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.
  • ExtendedCodegenProperty fix: The hasSanitizedName flag (already computed by DefaultCodegen) was not being copied in ExtendedCodegenProperty'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:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (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 ./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 (upcoming 7.x.0 minor 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 both name and baseName for required fields when they differ (via hasSanitizedName) without duplicating checks when they match. Ensure hasSanitizedName is copied in ExtendedCodegenProperty.
    • ToJSONTyped: emit the discriminator using the wire-format key (propertyBaseName); keep numeric singleton enum literals unquoted.
    • FromJSONTyped: fix oneOf decoding and array type narrowing to avoid TS “every on never” errors and support enumUnknownDefaultCase. Do not flatten oneOf variant properties when an allOf item is a $ref to a oneOf schema without a discriminator.