mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-10 09:56:08 +00:00
[typescript-fetch] Fix ToJSON for non-descriminator oneOf constructs (#12513)
* [typescript-fetch] Fix ToJSON for non-descriminator oneOf constructs * [typescript-fetch] Update samples for oneOf fix
This commit is contained in:
@@ -20,6 +20,20 @@ import {
|
||||
{{/discriminator}}
|
||||
{{>modelGenericInterfaces}}
|
||||
|
||||
/**
|
||||
* Check if a given object implements the {{classname}} interface.
|
||||
*/
|
||||
export function instanceOf{{classname}}(value: object): boolean {
|
||||
let isInstance = true;
|
||||
{{#vars}}
|
||||
{{#required}}
|
||||
isInstance = isInstance && "{{name}}" in value;
|
||||
{{/required}}
|
||||
{{/vars}}
|
||||
|
||||
return isInstance;
|
||||
}
|
||||
|
||||
export function {{classname}}FromJSON(json: any): {{classname}} {
|
||||
return {{classname}}FromJSONTyped(json, false);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
{{#oneOf}}
|
||||
import {
|
||||
{{{.}}},
|
||||
instanceOf{{{.}}},
|
||||
{{{.}}}FromJSON,
|
||||
{{{.}}}FromJSONTyped,
|
||||
{{{.}}}ToJSON,
|
||||
@@ -51,7 +52,14 @@ export function {{classname}}ToJSON(value?: {{classname}} | null): any {
|
||||
throw new Error(`No variant of {{classname}} exists with '{{discriminator.propertyName}}=${value['{{discriminator.propertyName}}']}'`);
|
||||
}
|
||||
{{/discriminator}}
|
||||
|
||||
{{^discriminator}}
|
||||
return { {{#oneOf}}...{{{.}}}ToJSON(value){{^-last}}, {{/-last}}{{/oneOf}} };
|
||||
{{#oneOf}}
|
||||
if (instanceOf{{{.}}}(value)) {
|
||||
return {{{.}}}ToJSON(value as {{{.}}});
|
||||
}
|
||||
{{/oneOf}}
|
||||
|
||||
return {};
|
||||
{{/discriminator}}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user