OpenAPITools / OpenAPITools/openapi-generator

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

Open
#4,374 9 comments 1 reaction 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?
  • [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.

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 with the typescript-node generator entry point and reproduce both OpenAPI examples using the shown Java command. Compare the generated TestResponse output and imports for the component and inline oneOf cases. Done means the component case represents a usable TestSuccess | TestError union and the inline case emits valid imports, with generator tests covering both outputs.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.