OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
22.02k stars 6.61k forks source link

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

Open frederikprijck opened 1 year ago

frederikprijck commented 1 year ago

Bug Report Checklist

Description

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

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:

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

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.

Linus-Boehm commented 1 year ago

I can confirm, we have the same issue with v6.4.0 (since v6.1.0), for polymorphic models it is even worse, as it tries to use instanceOf<ChildModel>. With version v6.0.0 everything is working for us.

We use the following options: --additional-properties=withInterfaces=true,stringEnums=true

frederikprijck commented 1 year ago

Thanks for chiming in @Linus-Boehm, to avoid confusion, I want to mention that the issue being reported here is not entirly fixed when pinning to 6.0.0.

The instanceOf part of the issue is, but the FromJSON / ToJSON are not.

Nevertheless, that's good information to know.

jackkinsella commented 1 year ago

I think some related issues are:

frederikprijck commented 1 year ago

Thanks @jackkinsella.

There's definetly one (ts-fetch) related to the instanceof issue, but not the FromJson/ToJson. Regardless, Great to get these connected! Thanks

The other two seems to be unrelated, one is even about golang, which is an entirly different generator. The other is about incorrect import path.

Just mentioning to avoid confusion.

eliw00d commented 1 year ago

I had both instanceOf and spread types errors and rolling back to 5.4.0 resolved both of them. Obviously, it's not ideal, but if you roll back to 5.4.0 does it solve both issues for you?

balloman commented 11 months ago

Are there any updates with this?

devgertschi commented 10 months ago

Any updates here?

renzoafable commented 10 months ago

Getting the same issue. Any updates?

austinhulak commented 9 months ago

I am also running into this - any updates?

pelhage commented 8 months ago

Running into the same issue, any updates or plans to address this?

jackkinsella commented 8 months ago

We ended up adding a post processing layer using handlebars to parse for problematic output and convert it into correct output.

On Thu, Mar 7, 2024 at 10:36 PM Patrick El-Hage @.***> wrote:

Running into the same issue, any updates or plans to address this?

— Reply to this email directly, view it on GitHub https://github.com/OpenAPITools/openapi-generator/issues/14763#issuecomment-1984670074, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABE5JX6IGQI7P2XQVZOBCLYXDTWZAVCNFSM6AAAAAAVB4CO7SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOBUGY3TAMBXGQ . You are receiving this because you were mentioned.Message ID: @.***>

6feferonka9 commented 8 months ago

Any news???

denisvanmorgan commented 8 months ago

hey any news on this?

anthonytamez commented 8 months ago

Same issue here. Any updates?

giovannicimolin commented 8 months ago

@frederikprijck I think I've created a fix for a similar issue: https://github.com/OpenAPITools/openapi-generator/pull/18154

Can you check if this fixes your issue? It solved mine.

welschmoor commented 4 months ago

Two temporary solutions:

  1. Add mapping to opanapi spec. Example:
            "SomeType": {
                "oneOf": [ {
                    "$ref": "#/components/schemas/Blah1"
                }, {
                    "$ref": "#/components/schemas/Blah2"
                } ],
                "type": "object",
                "properties": {
                    "objectType": {
                        "type": "string"
                    }
                },
                "required": [ "objectType" ],
                "discriminator": {
                    "propertyName": "objectType",
                    //add this, which is the same as oneOf above:
                    "mapping": {
                        "Blah1": "#/components/schemas/Blah1",
                        "Blah2": "#/components/schemas/Blah2"
                    }
                }
            },
  2. use typescript-axios generator, it generates everything without problems: npx @openapitools/openapi-generator-cli generate -i ./openapi.json -g typescript-axios -o packages/openapi/out -c ./openapiconfig.json --global-property models,supportingFiles
SL-RU commented 3 months ago

Same issue on 7.7.0.

My solution is to add ts-nocheck to files with oneOf:

diff --git a/src/api/models/JobPostArgs.ts b/src/api/models/JobPostArgs.ts
--- a/src/api/models/JobPostArgs.ts
+++ b/src/api/models/JobPostArgs.ts
@@ -1,3 +1,4 @@
+// @ts-nocheck
 /* tslint:disable */
 /* eslint-disable */
 /**
patch -p1 < ./api-hotfix.patch