-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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: stringCase 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.