Skip to content

Conversation

btpnlsl
Copy link
Contributor

@btpnlsl btpnlsl commented Sep 4, 2025

Fixes #21811

The JSON serialization/deserialization code generated for oneOf schemas which do not use a discriminator follows a pattern of validating & serializing first the model schemas, then arrays of non-primitive objects, then primitive objects (including arrays).

The area of the template which validates & serializes / deserializes arrays of non-primitive objects includes a return value; or return json; statement if it does not find a matching object. For example:

    if (Array.isArray(value)) {
        if (value.every(item => typeof item === 'object')) {
            if (value.every(item => instanceOfTestA(item))) {
                return value.map(value => TestAToJSON(value as TestA));
            }
            if (value.every(item => instanceOfTestB(item))) {
                return value.map(value => TestBToJSON(value as TestB));
            }
        }
        return value;
    }

This return short circuits further validation / serialization that should happen if the schema is an array of a primitive type.

Additionally there are a couple of other fixes for:

  • Code which is deserializing arrays of Dates is using the entire json string instead of deserializing each item in the array.
  • Variable name duplication in the lambdas for some calls to .map (e.g. value.map(value => value.toISOString()))
  • Not casting array items as object when calling instanceOf methods
  • Several paces where the code which has unbalanced parenthesis

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.

@@ -44,14 +44,13 @@ export function TestArrayResponseFromJSONTyped(json: any, ignoreDiscriminator: b
}
if (Array.isArray(json)) {
if (json.every(item => typeof item === 'object')) {
if (json.every(item => instanceOfTestA(item))) {
return json.map(value => TestAFromJSONTyped(value, true));
if (json.every(item => instanceOfTestA(item as object))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using item as object we could also tell typescript that json is object[] if the condition that checks that every item is object is true:

if (json.every((item): item is object => typeof item === 'object')) {
    if (json.every(item => instanceOfTestA(item))) {
      // ...
    }
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing: The check, that every item in the array is an object should also check that the item is not null, because typeof null also returns 'object'.

if (json.every((item): item is object => item !== null && typeof item === 'object')) {
// ...
}

Otherwise instanceOfTestA will be called with null if json contains [null], and that will cause a TypeError because the in operator cannot be used with null.

export function instanceOfTestA(value: object): value is TestA {
    if (!('foo' in value) || value['foo'] === undefined) return false;
    return true;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG][typescript-fetch] oneOf with arrays generate Typescript error
3 participants