OpenAPITools / OpenAPITools/openapi-generator

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

Open
#5,171 1 comment 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: TypeScript Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
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
  • Run a command above, providing a spec file with allOf
  • Check model/responseOnePlus.ts.It will looks like this:
import { ExtraFields } from './extraFields';
import { ResponseOnePlusAllOf } from './responseOnePlusAllOf';

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

Both imports here are not used.

Related issues/PRs

Similar issues:

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the documented openapi-generator-cli command with the provided YAML and inspect generated/model/responseOnePlus.ts. Compare its imports with the interface body and related typescript-axios and typescript-rxjs fixes; done means the generated TypeScript Angular model no longer contains unused imports for non-discriminator allOf references.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.