Skip to content

[BUG][typescript-node] invalid oneOff class or import #4374

@quezak

Description

@quezak

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?
  • [Optional] Bounty to sponsor the fix (example)
Description

I'm spec'ing a weird API that returns either { success: true, ... } or { success: false, error: ... }. I've defined these as TestSuccess and TestError. I wanted to use the oneOf schema to make the client return a union type. I've tried this two ways, both produce invalid code:

  1. Add components/schemas/TestResponse as oneOf with referencing the two schemas. This:
    • generates both TestResponse and TestError classes correctly
    • generates a TestResponse class returned by the API class
    • the TestResponse class has all the properties of both TestSuccess and TestError as required -- not actually useful...
    • the TestResponse class imports both TestSuccess and TestError, but doesn't use them anywhere
// UNUSED
import { TestError } from './testError';
import { TestSuccess } from './testSuccess';

export class TestResponse {
    'success': boolean;
    'result': number;
    'error': string;

    static discriminator: string | undefined = undefined; // ???
// ...
  1. Add the oneOf schema inline under .../responses/'200'/content/'application/json'/schema. This:
    • generates both TestResponse and TestError classes correctly
    • generates an API class returning correctly TestResponse | TestError
    • BUT: the import for these classes is utterly invalid 😆
import { TestSuccess | TestError } from '../model/testSuccess | TestError';
openapi-generator version

5.0 snapshot (5.0.0-20191104.022738-17)

OpenAPI declaration file content or url

Case 1:

openapi: '3.0.2'

info:
  version: 0.0.1
  title: test

paths:
  /test:
    get:
      summary: test
      operationId: test
      responses:
        '200':
          description: test result
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TestResponse'

components:
  schemas:
    TestResponse:
      oneOf:
        - $ref: '#/components/schemas/TestSuccess'
        - $ref: '#/components/schemas/TestError'

    TestSuccess:
      type: object
      required:
        - success
        - result
      properties:
        success:
          type: boolean
        result:
          type: integer

    TestError:
      type: object
      required:
        - success
        - error
      properties:
        success:
          type: boolean
        error:
          type: string

Case 2: same, only TestResponse is pasted inline, not as a component:

...
        '200':
          description: test result
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/TestSuccess'
                  - $ref: '#/components/schemas/TestError'
...
Command line used for generation
java -DskipFormModel=true -jar $GENERATOR_JAR generate -i spec/test-api.2.yml -g typescript-node -p supportsES6=true -o .
Steps to reproduce

Generate the clients and inspect the code :)

Related issues/PRs

I've read #3092 and #2154, but it seems like this is a different case.

Suggest a fix

Case 1: the third generated schema should be just export type TestResponse = TestSuccess | TestError. Or make some use of the discriminator field? (and don't mark all the union fields as non-null)

Case 2: the two classes should be imported correctly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions