OpenAPITools / OpenAPITools/openapi-generator
[BUG][typescript-node] invalid oneOff class or import
Nobody has claimed this yet.
- 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:
- Add
components/schemas/TestResponseasoneOfwith referencing the two schemas. This:- generates both
TestResponseandTestErrorclasses correctly - generates a
TestResponseclass returned by the API class - the
TestResponseclass has all the properties of bothTestSuccessandTestErroras required -- not actually useful... - the
TestResponseclass imports bothTestSuccessandTestError, but doesn't use them anywhere
- generates both
// UNUSED
import { TestError } from './testError';
import { TestSuccess } from './testSuccess';
export class TestResponse {
'success': boolean;
'result': number;
'error': string;
static discriminator: string | undefined = undefined; // ???
// ...
- Add the
oneOfschema inline under.../responses/'200'/content/'application/json'/schema. This:- generates both
TestResponseandTestErrorclasses correctly - generates an API class returning correctly
TestResponse | TestError - BUT: the import for these classes is utterly invalid 😆
- generates both
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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