OpenAPITools / OpenAPITools/openapi-generator

[BUG] [TypeScript-Fetch] Incorrectly generated models when using oneOf

Open
#14,763 20 comments 26 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

When using oneOf with the TypeScript-Fetch generator, the models are generated incorrectly in two ways:

  • It generates references to functions that do not exist
  • It incorrectly handles generics

An example repository can be found at https://github.com/frederikprijck/openapi-typescript-fetch-one-of, which includes a minimal spec file, as well as the generated code.

The generated output can be seen on the reproduction repository on GitHub, but here are the relevant bits:

export function TestUrlFromJSONTyped(json: any, ignoreDiscriminator: boolean): TestUrl {
    if ((json === undefined) || (json === null)) {
        return json;
    }
    return { ...Array<string>FromJSONTyped(json, true), ...stringFromJSONTyped(json, true) };
}

export function TestUrlToJSON(value?: TestUrl | null): any {
    if (value === undefined) {
        return undefined;
    }
    if (value === null) {
        return null;
    }

    if (instanceOfArray<string>(value)) {
        return Array<string>ToJSON(value as Array<string>);
    }
    if (instanceOfstring(value)) {
        return stringToJSON(value as string);
    }

    return {};
}

As you can see:

  • stringFromJSONTyped, instanceOfArray and stringToJSON do not exist
  • Array<string>FromJSONTyped, instanceOfArray<string> and Array<string>ToJSON do not exist but are also not valid.

The first issue appears with different types as well, such as number. While the second is a bit of a different issue and not sure it requires a different issue report. Happy to do so if needed.

openapi-generator version

@openapitools/openapi-generator-cli version 2.5.2 on NPM with 6.3.0 in the OpenAPITools.json.

OpenAPI declaration file content or url

The entire (minimal) file is available at the reproduction repository on GitHub, but here are the relevant bits:

"components": {
  "schemas": {
    "test": {
      "type": "object",
      "properties": {
        "url": {
          "description": "Test Url(s).",
          "oneOf": [
            { "type": "array", "items": { "type": "string" } },
            { "type": "string" }
          ]
        }
      }
    }
  }
},
Generation Details

You can run npm run generate after cloning the sample repository, or use:

openapi-generator-cli generate -i ./api.json -o src/generated -g typescript-fetch
Steps to reproduce
  • git clone git@github.com:frederikprijck/openapi-typescript-fetch-one-of.git
  • cd openapi-typescript-fetch-one-of
  • npm i
  • npm run generate
  • cat src/generated/models/TestUrl.ts

Or using Docker:

docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
    -i https://raw.githubusercontent.com/frederikprijck/openapi-typescript-fetch-one-of/main/api.json \
    -g typescript-fetch \
    -o /local/out/typescript-fetch

Followed by cat out/typescript-fetch/models/TestUrl.ts

Related issues/PRs

There are a lot of issues regarding oneOf and TypeScript, but couldn't really find much that felt related apart from this one:

https://github.com/OpenAPITools/openapi-generator/issues/12510

This looks as if it's exactly the same issue as the first of the issues (so not the generic Array one), but for query parameters instead of schema properties. I also noticed that the issue is closed, has a merged PR linked but I am still seeing the exact same output on the latest master, see my comment on the issue.

Suggest a fix

I don't think I have any idea. I would be happy to help fix this, but will need some pointers.

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 minimal api.json and generation command in the linked reproduction repository, then inspect the generated models/TestUrl.ts output. Compare the oneOf handling with the reported nonexistent functions and invalid generic names. Done means the TypeScript-Fetch generator produces valid model code for this array-or-string schema without unresolved references.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.