Skip to content

[BUG][typescript-fetch] Default case for oneOf serialization method does not serialize correctly #21810

@aykborstelmann

Description

@aykborstelmann

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

In #20983 was introduced that instead of throwing an error in the default case of a one of switch, the value itself is returned.
However this value is not serialized and instead directly returned.
This does lead to problem when for example a discriminator @type is used.
In typescript this is a field type which is serialized to @type during ToJSONTyped and thus is not serialized.

openapi-generator version

I am using 7.15, I don't know for which versions it worked

OpenAPI declaration file content or url
{
  "components": {
    "schemas": {
      "AbstractRequest": {
        "discriminator": {
          "propertyName": "@type"
        },
        "properties": {
          "@type": {
            "type": "string"
          }
        },
        "required": [
          "@type"
        ]
      },
      "SubRequest": {
        "allOf": [
          {
            "$ref": "#/components/schemas/AbstractRequest"
          },
          {
            "properties": {
              "@type": {
                "type": "string"
              },
              "id": {
                "type": "string"
              }
            },
            "type": "object"
          }
        ],
        "discriminator": {
          "propertyName": "@type"
        },
        "required": [
          "@type",
          "id"
        ]
      }
    }
  },
  "info": {
    "title": "API",
    "version": "0.1"
  },
  "openapi": "3.1.0"
}

The SubRequest.ts model contains the following method

export function SubRequestToJSONTyped(value?: SubRequest | null, ignoreDiscriminator: boolean = false): any {
    if (value == null) {
        return value;
    }

    if (!ignoreDiscriminator) {
        switch (value['type']) {
            default:
                return value;
        }
    }

    return {
        ...AbstractRequestToJSONTyped(value, true),
        '@type': value['type'],
        'id': value['id'],
    };
}

while the Request.ts contains

export function AbstractRequestToJSONTyped(value?: AbstractRequest | null, ignoreDiscriminator: boolean = false): any {
    if (value == null) {
        return value;
    }

    if (!ignoreDiscriminator) {
        switch (value['type']) {
            case 'SubRequest':
                return SubRequestToJSONTyped(value as SubRequest, ignoreDiscriminator);
            default:
                return value;
        }
    }

    return {
        
        '@type': value['type'],
    };
}

If we now serialize a SubRequest using SubRequestToJSONTyped we obtain

{
    "type": "SubRequest",
    "id": "id"
}

while we would expect

{
    "@type": "SubRequest",
    "id": "id"
}
Related issues/PRs

#20983

Suggest a fix

I would expect that the default case should be

default:
    return SubRequestToJSONTyped(value, true);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions