Skip to content

[BUG] exotic discriminator property name leads to broken typescript-fetch client behaviour #13878

@helt

Description

@helt

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • 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

I use a "exotic" discriminator property name: @type. This leads to polymorphism issues when generating a typescript-fetch client, because the @ is swallowed by something.

openapi-generator version

generator version 6.2.0

https://github.com/helt/openapi-generator-discriminator-quirks/blob/9b8dbebb8168991b855c3c7b864548a9c6142ec3/middleware/openapitools.json#L4

OpenAPI declaration file content or url

The openapi spec of the bad example:
https://github.com/helt/openapi-generator-discriminator-quirks/blob/9b8dbebb8168991b855c3c7b864548a9c6142ec3/middleware/openapi-bad.json#L10

Openapi spec of the good example:
https://github.com/helt/openapi-generator-discriminator-quirks/blob/9b8dbebb8168991b855c3c7b864548a9c6142ec3/middleware/openapi-good.json#L10

Generation Details

go into the middleware directory of the repro repo and run

Steps to reproduce
npm ci
npm run generate

look into https://github.com/helt/openapi-generator-discriminator-quirks/blob/master/middleware/modules/typescript-fetch-bad/src/models/Animal.ts which has a portion like this:

For easier repro, I also added the generating spring boot + openapi app.

Related issues/PRs
Suggest a fix

https://github.com/helt/openapi-generator-discriminator-quirks/blob/9b8dbebb8168991b855c3c7b864548a9c6142ec3/middleware/modules/typescript-fetch-bad/src/models/Animal.ts#L60

I think the discriminator property has been altered by the code generator.

export function AnimalFromJSONTyped(
  json: any,
  ignoreDiscriminator: boolean
): Animal {
  if (json === undefined || json === null) {
    return json;
  }
  if (!ignoreDiscriminator) {
    if (json["fantatsticClass"] === "fish") {  // 👈 should read json["@fantatsticClass"]
      return FishFromJSONTyped(json, true);
    }
    if (json["fantatsticClass"] === "mammal") { // 👈 this one too.
      return MammalFromJSONTyped(json, true);
    }
  }
  return {
    fantatsticClass: json["@fantatsticClass"],
    label: !exists(json, "label") ? undefined : json["label"],
  };
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions