-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG][Typescript] Generated code with oneOf
does not properly check the dto type
#18713
Comments
How about using Like: Dto:
type: object
properties:
payload:
oneOf:
- $ref: '#/components/schemas/Payload1'
- $ref: '#/components/schemas/Payload2'
- $ref: '#/components/schemas/Payload3'
discriminator:
propertyName: type |
This does not work. With the provided schema in this issue, using /**
* @type DtoPayload
* Payload for the assistance request
* @export
*/
export type DtoPayload = ; And if I set /**
* @type DtoPayload
* Payload for the assistance request
* @export
*/
export type DtoPayload = { type: 'Payload1' } & Payload1 | { type: 'Payload2' } & Payload2 | { type: 'Payload3' } & Payload3;
// ^^^^^^^^^^ The type will be `never`
export function DtoPayloadFromJSON(json: any): DtoPayload {
return DtoPayloadFromJSONTyped(json, false);
}
export function DtoPayloadFromJSONTyped(json: any, ignoreDiscriminator: boolean): DtoPayload {
if (json == null) {
return json;
}
switch (json['type']) {
case 'Payload1':
return Object.assign({}, Payload1FromJSONTyped(json, true), { type: 'Payload1' });
// ^^^^^^ Cannot assign type "Payload1 & { type: string; }" to type "never".
case 'Payload2':
return Object.assign({}, Payload2FromJSONTyped(json, true), { type: 'Payload2' });
case 'Payload3':
return Object.assign({}, Payload3FromJSONTyped(json, true), { type: 'Payload3' });
default:
throw new Error(`No variant of DtoPayload exists with 'type=${json['type']}'`);
}
} |
Hmm, there's room for improvement in the generated code. |
Maybe we can fix it like this: Line 26 in 1c787ba
- if (!('{{name}}' in value){{#isEnum}} || value['{{name}}'] !== {{enumType}}[0]{{/isEnum}}) return false;
+ if (!('{{name}}' in value){{#isEnum}} || value['{{name}}'] !== Object.values({{datatypeWithEnum}})[0]{{/isEnum}}) return false; |
Bug Report Checklist
Description
Given the OpenApi schema below, we have
Dto
withpayload
field, which has a type ofoneOf
ofPayload1
,Payload2
andPayload3
. Each of payload types has a propertytype
with type of enum with only one value.When we generate client code, we get this code in
models/DtoPayload.ts
file:and code for each if check:
models/Payload1.ts
models/Payload2.ts
models/Payload3.ts
since functions
instanceOfPayload1
andinstanceOfPayload3
are identical, generated code treatsPayload3
asPayload1
, which causes problems when converting plain objects to dto's.openapi-generator version
7.5.0
OpenAPI declaration file content or url
Generation Details
npx -y @openapitools/openapi-generator-cli generate -g typescript-fetch
--additional-properties=importFileExtension=".js" --skip-validate-spec
-i ./test.json
-o src/api-client
Steps to reproduce
test
test
test.json
and copy and paste provided api schema above.Generation Details
command in cliRelated issues/PRs
Suggest a fix
We want the generator to also take into account types of fields, when checking if object is instance of another object.
for example:
The text was updated successfully, but these errors were encountered: