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
21.72k stars 6.55k forks source link

[BUG] [typescript-fetch] oneOf with primitive type gives compilation errors #14831

Open sammy-da opened 1 year ago

sammy-da commented 1 year ago
Description

a oneOf schema that has a primitive type, such as string, as one of the options generates invalid typescript code that doesn't compile, (but can be fixed with a couple small manual tweaks)

(I think this also applies to anyOf)

openapi-generator version

typescript-fetch, using openapi-generator version 6.2.1

OpenAPI declaration file content or url
{
   "components": {
      "schemas": {
         "StringSchema": {
            "description": "meep ent",
            "type": "string"
         },
         "FooEntity": {
            "oneOf": [
               {
                  "$ref": "#/components/schemas/ObjectA"
               },
               {
                  "$ref": "#/components/schemas/StringSchema"
               }
            ]
         }
      }
   }
}

this generates code that looks like:

import {
  ObjectA,
  instanceOfObjectA,
  ObjectAFromJSON,
  ObjectAFromJSONTyped,
  ObjectAToJSON,
} from './ObjectA';
import {
  string,
  instanceOfstring,
  stringFromJSON,
  stringFromJSONTyped,
  stringToJSON,
} from './string'; // <-- THIS IS INVALID

/**
* @type FooEntity
* 
* @export
*/
export type FooEntity = ObjectA | string;

Also, the JSON-serialization function that gets generated (FooEntityToJSON) is invalid too.

Steps to reproduce

generate a typescript client using from the given example schemas above ☝️

Related issues/PRs
Suggest a fix
jennydaman commented 3 weeks ago

Duplicate of https://github.com/OpenAPITools/openapi-generator/issues/14763