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.8k stars 6.58k forks source link

[BUG] Imports missing in nested models for `anyOf` properties #11867

Open nmerigaud opened 2 years ago

nmerigaud commented 2 years ago

Bug Report Checklist

Description

https://github.com/OpenAPITools/openapi-generator/commit/6430aaf3b1b2a6bafc67d5342f9e3703ada1a671 introduced deeply nested imports. More specifically the method that recursively collects imports handles allOf and oneOf composed schemas but seems to ignore anyOf schemas.

Having the following Open API schemas, the generated model AnyFooBar does not include the required Foo and Bar imports

edit: Removed TypeScript from title as from what I can tell, looking at where the issue lies, this is likely affecting all languages.

openapi-generator version

This is in 5.4.0 as well as latest of master as far as I can tell.

OpenAPI declaration file content or url
components:
  schemas:
    Foo:
      type: object
      properties:
        name:
          type: string
        foo:
          type: string
    Bar:
      type: object
      properties:
        name:
          type: string
        bar:
          type: string
    AnyFooBar:
      type: object
      properties:
        name:
          type: string
        fooOrBar:
          anyOf:
            - $ref: '#/components/schemas/Foo'
            - $ref: '#/components/schemas/Bar'
Generation Details
java -jar openapi-generator-cli.jar generate -g typescript -i api.yml -o generated/
import { HttpFile } from '../http/http';

export class AnyFooBar {
    'name'?: string;
    'fooOrBar'?: Foo | Bar;

    static readonly discriminator: string | undefined = undefined;

    static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
        {
            "name": "name",
            "baseName": "name",
            "type": "string",
            "format": ""
        },
        {
            "name": "fooOrBar",
            "baseName": "fooOrBar",
            "type": "Foo | Bar",
            "format": ""
        }    ];

    static getAttributeTypeMap() {
        return AnyFooBar.attributeTypeMap;
    }

    public constructor() {
    }
}
Related issues/PRs

The following issue does look somewhat similar though language and models do differ.

Suggest a fix

Opened a pull request for candidate fix: #11879

ikopysov commented 2 years ago

It seems that it's better to leave [TypeScript] in the title, and add related tag, because for some languages it works. For other languages there are multiple tickets, and without tags it makes search for corresponding ticket much harder.

Additionally, I believe, they should be implemented "per generator" so it will make it harder to identify and work for some concrete features.

Other option is to create separate issue which will reference all related issues with different generators so it will be easier to work on specific feature and identify generators that do not support those specs.

upd: This may be linked to #10514