openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][typescript-angular] `anyOf` generates empty `interface` model

Open snebjorn opened this issue 2 years ago • 4 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [ ] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

anyOf schema definitions generates an empty interface model.

{
  "components": {
    "schemas": {
      "foo": {
        "type": "object",
        "properties": {
          "bar": {
            "anyOf": [{ "type": "number" }, { "type": "string" }] // <------- this
          }
        }
      }
    }
  }
}

Generates this model

export interface FooBar {  // <--- this doesn't represent type `number | string`
}
import { FooBar } from './fooBar';

export interface Foo { 
    bar?: FooBar;
}
openapi-generator version

This is a regression starting from v6.0.0, it worked in v5.4.0

Output v6.1.0 Output v5.4.0

OpenAPI declaration file content or url

https://github.com/snebjorn/openapi-gen-bug/blob/master/anyof-bug.json

{
  "openapi": "3.0.0",
  "info": {
    "title": "",
    "version": ""
  },
  "paths": {
    "/Foos": {
      "get": {
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/foo"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "foo": {
        "type": "object",
        "properties": {
          "bar": {
            "anyOf": [{ "type": "number" }, { "type": "string" }],
            "description": "This is the description for bar"
          }
        }
      }
    }
  }
}
Generation Details

Generated using only default values, see https://github.com/snebjorn/openapi-gen-bug/blob/59eb4ba55e91f8444add450016fbd5666c04e2ee/openapitools.json#L25-L29

Steps to reproduce
  1. Clone https://github.com/snebjorn/openapi-gen-bug
  2. Run yarn install
  3. Run yarn gen
  4. Observe empty interface in https://github.com/snebjorn/openapi-gen-bug/blob/master/output-6.1.0/anyof-bug/model/fooBar.ts
Related issues/PRs

Couldn't find any related to empty interfaces but a lot of issues popped up regarding anyOf, allOf and oneOf

---- UPDATED ---- This issue was just reported:

  • #13538
Suggest a fix

Not exactly sure why bar?: number | string | null; was changed to bar?: FooBar;. Interfaces can't extend primitive types.

// An interface cannot extend a primitive type like 'string'; an interface can only extend named types and classes ts(2840)
interface FooBar extends string, number {}

It could be turned into a type

export type FooBar = string | number;

However there is also the issue of the description. The description is moved to the FooBar interface/type, but the description is describing the property bar which now looks like this

export interface Foo { 
    bar?: FooBar; // <--- note the missing description. The description is on `FooBar` which I believe is incorrect.
}

It should look like this

export interface Foo { 
    /**
     * This is the description for bar
     */
    bar?: FooBar;
}

snebjorn avatar Sep 15 '22 13:09 snebjorn

@wing328 I've tracked down the issue to being related to the flattening of components which you implemented in #12175

As described in the issue. Flattening of types that use primitive types is troublesome as it cannot be turned into an interface. This is the case for TypeScript, Java and C# (I assume many others as well).

TL;DR; "anyOf": [{ "type": "number" }, { "type": "string" }], cannot be turned into interface FooBar extends string, number.

Why is the components flattened? What problem does it solve? What is the intended behavior in a generator when a flattened component contains primitive types?

snebjorn avatar Sep 26 '22 10:09 snebjorn

Any fix for this issue? We are facing this exact problem. And this is on the latest version 7.3.0.

PabloFNK avatar Feb 20 '24 11:02 PabloFNK

@snebjorn

Hello, any updates? Experiencing the same issue with 7.5.0

egorwow70 avatar May 07 '24 15:05 egorwow70

Sorry, I stopped working on this a while ago. Feel free to pick it up

snebjorn avatar May 07 '24 19:05 snebjorn