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.58k stars 6.52k forks source link

[BUG][typescript-angular] allOf without discriminator results in unnecessary imports #5171

Open amakhrov opened 4 years ago

amakhrov commented 4 years ago

Bug Report Checklist

Description

When a model uses allOf, where some of the referenced models do not have a discriminator (hence, do not denote any hierarchy), the target interface just gets all properties copied from the referenced models, no interface inheritance/extending involved.

However, the base interfaces are still imported into the file with the target model - despite being unused.

openapi-generator version

Latest

OpenAPI declaration file content or url
definitions:
  ExtraFields:
    type: object
    properties:
      extra_field:
        type: string

  ResponseOnePlus:
    type: object
    allOf:
      - $ref: '#/definitions/ExtraFields'
      - type: object
        properties:
          own_field:
            type: string
Command line used for generation
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
    -i /local/spec.yaml \
    -g typescript-angular \
    -o /local/generated
Steps to reproduce

export interface ResponseOnePlus { extraField?: string; ownField?: string; }


Both imports here are not used.

##### Related issues/PRs

Similar issues:
- `typescript-axios`: https://github.com/OpenAPITools/openapi-generator/issues/4804
- `typescript-rxjs`: https://github.com/OpenAPITools/openapi-generator/issues/4629

##### Suggest a fix

The fixes for linked axios and rxjs generators above change the approach for generating a target interface: instead of copying all properties from the source interfaces into the target one, the target type is simply defined as type intersection.

Perhaps we should consider doing the same for typescript-angular (and probably other typescript generators that emit each model in a separate file with imports).

Note that the above mentioned fixes apply the same approach both with or without `discriminator`, thus erasing the semantic difference between them (hierarchy vs composition)
auto-labeler[bot] commented 4 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.