OpenAPITools / OpenAPITools/openapi-generator

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

Open
#13,878 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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"],
  };
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked bad and good OpenAPI examples, run npm ci and npm run generate in the repro's middleware directory, and compare the generated modules/typescript-fetch-bad/src/models/Animal.ts. Trace how the discriminator property is represented in the typescript-fetch generator and verify that the generated discriminator checks preserve the @ name.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.