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

[BUG][typescript-angular] Missing type import when using `$ref` with `oneOf` or `anyOf`

Open ksierpien opened this issue 3 years ago • 1 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)?
  • [x] 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

When there is an inline schema with $ref used together with oneOf or allOf, the generator creates a new type for it, but does not generate the import statement for that type in *.service.ts. This causes a TypeScript compilation error.

In versions before 6.0.0 it didn't create a new type that required an import. It just used a union of types referenced by those refs.

openapi-generator version

6.0.0 and later. It used to work in 5.4.0 and 6.0.0-beta.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: API
  description: Description
  version: 0.1.0
paths:
  /api/Sample/GetValue:
    get:
      tags:
      - Sample
      operationId: Sample_GetValue
      parameters:
      - name: input
        in: query
        schema:
          oneOf:
          - $ref: '#/components/schemas/MyType'
      responses:
        200:
          description: ''
          content:
            application/json:
              schema:
                type: integer
components:
  schemas:
    MyType:
      type: integer
Generation Details
{
	"$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
	"generator-cli": {
		"version": "6.1.0",
		"generators": {
			"angular": {
				"generatorName": "typescript-angular",
				"output": "./generated",
				"glob": "./spec.yaml"
			}
		}
	}
}
Steps to reproduce

Just generate the above with ^6.0.0 and typescript-angular. There will be a compilation error in sample.service.ts in function:

public sampleGetValue(input?: SampleGetValueInputParameter, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext}): Observable<number>;

The type SampleGetValueInputParameter is not imported from '../model/sampleGetValueInputParameter'.

Related issues/PRs

There is a similar, but slightly different issue #12256 that might be related to it.

Suggest a fix

ksierpien avatar Sep 13 '22 15:09 ksierpien

I'm also facing this exact issue with Typescript-Axios v6.1.0

Very frustrating as we're still stuck on v5.3.1 due to other regressions that were introduced in v5.4 and fixed in v6

jjbell150 avatar Sep 19 '22 01:09 jjbell150

Same issue with the typescript-axios, hoping in a fix.

emajo avatar Sep 22 '22 06:09 emajo

Just tried this in 6.5.0 and the issue seems fixed, at least in my scenario with typescript-angular. I'm closing it.

ksierpien avatar Apr 26 '23 10:04 ksierpien