Skip to content

Commit

Permalink
Fix for #15736 [TYPESCRIPT-FETCH] Subclassing components using discri… (
Browse files Browse the repository at this point in the history
#19524)

* Fix for #15736 [TYPESCRIPT-FETCH] Subclassing components using discriminators fails to convert subclasses to JSON.
Added similar discriminator handling to ToJSON as was already in place for FromJSON.
The actual files changed are typescript-fetch/modelGeneric.mustache and typescript-fetch/apis.mustache.
Also, adjusted FromJSON a bit in an attempt to support multiple hierarchical levels of discriminators.
And fixed an issue with calling FromJSON from the map() function, which caused the index parameter getting inadvertently passed as the ignoreDiscriminator parameter.
Additionally, fixed failing "mvn integration-test -f samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces/pom.xml"
Moreover, added forceConsistentCasingInFileNames:false into tsconfig.json to make tests compile on OsX.

* Rolled back the changes related to the map() function calls in favor of using ToJSONTyped instead, as that is in line with how FromJSON is already implemented.
  • Loading branch information
hagis authored Sep 5, 2024
1 parent e914c40 commit 2f54a2f
Show file tree
Hide file tree
Showing 185 changed files with 1,213 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
export function {{classname}}ToJSON(value?: {{classname}} | null): any {
return value as any;
}

export function {{classname}}ToJSONTyped(value: any, ignoreDiscriminator: boolean): {{classname}} {
return value as {{classname}};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import {
{{classname}}FromJSON,
{{classname}}FromJSONTyped,
{{classname}}ToJSON,
{{classname}}ToJSONTyped,
} from './{{filename}}{{importFileExtension}}';
{{/tsImports}}

{{/hasImports}}
{{#discriminator}}
{{#discriminator.mappedModels}}
import { {{modelName}}FromJSONTyped } from './{{modelName}}{{importFileExtension}}';
import { {{modelName}}, {{modelName}}FromJSONTyped, {{modelName}}ToJSON, {{modelName}}ToJSONTyped } from './{{modelName}}{{importFileExtension}}';
{{/discriminator.mappedModels}}
{{/discriminator}}
{{>modelGenericInterfaces}}
Expand Down Expand Up @@ -42,13 +43,13 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
if (!ignoreDiscriminator) {
{{#discriminator.mappedModels}}
if (json['{{discriminator.propertyBaseName}}'] === '{{mappingName}}') {
return {{modelName}}FromJSONTyped(json, true);
return {{modelName}}FromJSONTyped(json, ignoreDiscriminator);
}
{{/discriminator.mappedModels}}
}
{{/discriminator}}
return {
{{#parent}}...{{{.}}}FromJSONTyped(json, ignoreDiscriminator),{{/parent}}
{{#parent}}...{{{.}}}FromJSONTyped(json, true),{{/parent}}
{{#additionalPropertiesType}}
...json,
{{/additionalPropertiesType}}
Expand Down Expand Up @@ -97,13 +98,31 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
{{/hasVars}}
}

export function {{classname}}ToJSON(value?: {{#hasReadOnly}}Omit<{{classname}}, {{#readOnlyVars}}'{{baseName}}'{{^-last}}|{{/-last}}{{/readOnlyVars}}>{{/hasReadOnly}}{{^hasReadOnly}}{{classname}}{{/hasReadOnly}} | null): any {
export function {{classname}}ToJSON(json: any): {{classname}} {
return {{classname}}ToJSONTyped(json, false);
}

export function {{classname}}ToJSONTyped(value?: {{#hasReadOnly}}Omit<{{classname}}, {{#readOnlyVars}}'{{baseName}}'{{^-last}}|{{/-last}}{{/readOnlyVars}}>{{/hasReadOnly}}{{^hasReadOnly}}{{classname}}{{/hasReadOnly}} | null, ignoreDiscriminator: boolean = false): any {
{{#hasVars}}
if (value == null) {
return value;
}
{{#discriminator}}

if (!ignoreDiscriminator) {
switch (value['{{discriminator.propertyName}}']) {
{{#discriminator.mappedModels}}
case '{{mappingName}}':
return {{modelName}}ToJSONTyped(value as {{modelName}}, ignoreDiscriminator);
{{/discriminator.mappedModels}}
default:
throw new Error(`No variant of {{classname}} exists with '{{discriminator.propertyName}}=${value['{{discriminator.propertyName}}']}'`);
}
}
{{/discriminator}}

return {
{{#parent}}...{{{.}}}ToJSON(value),{{/parent}}
{{#parent}}...{{{.}}}ToJSONTyped(value, true),{{/parent}}
{{#additionalPropertiesType}}
...value,
{{/additionalPropertiesType}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import {
BranchDtoFromJSON,
BranchDtoFromJSONTyped,
BranchDtoToJSON,
BranchDtoToJSONTyped,
} from './BranchDto';

import { InternalAuthenticatedUserDtoFromJSONTyped } from './InternalAuthenticatedUserDto';
import { RemoteAuthenticatedUserDtoFromJSONTyped } from './RemoteAuthenticatedUserDto';
import { InternalAuthenticatedUserDto, InternalAuthenticatedUserDtoFromJSONTyped, InternalAuthenticatedUserDtoToJSON, InternalAuthenticatedUserDtoToJSONTyped } from './InternalAuthenticatedUserDto';
import { RemoteAuthenticatedUserDto, RemoteAuthenticatedUserDtoFromJSONTyped, RemoteAuthenticatedUserDtoToJSON, RemoteAuthenticatedUserDtoToJSONTyped } from './RemoteAuthenticatedUserDto';
/**
*
* @export
Expand Down Expand Up @@ -65,10 +66,10 @@ export function AbstractUserDtoFromJSONTyped(json: any, ignoreDiscriminator: boo
}
if (!ignoreDiscriminator) {
if (json['type'] === 'internal-authenticated') {
return InternalAuthenticatedUserDtoFromJSONTyped(json, true);
return InternalAuthenticatedUserDtoFromJSONTyped(json, ignoreDiscriminator);
}
if (json['type'] === 'remote-authenticated') {
return RemoteAuthenticatedUserDtoFromJSONTyped(json, true);
return RemoteAuthenticatedUserDtoFromJSONTyped(json, ignoreDiscriminator);
}
}
return {
Expand All @@ -79,10 +80,26 @@ export function AbstractUserDtoFromJSONTyped(json: any, ignoreDiscriminator: boo
};
}

export function AbstractUserDtoToJSON(value?: AbstractUserDto | null): any {
export function AbstractUserDtoToJSON(json: any): AbstractUserDto {
return AbstractUserDtoToJSONTyped(json, false);
}

export function AbstractUserDtoToJSONTyped(value?: AbstractUserDto | null, ignoreDiscriminator: boolean = false): any {
if (value == null) {
return value;
}

if (!ignoreDiscriminator) {
switch (value['type']) {
case 'internal-authenticated':
return InternalAuthenticatedUserDtoToJSONTyped(value as InternalAuthenticatedUserDto, ignoreDiscriminator);
case 'remote-authenticated':
return RemoteAuthenticatedUserDtoToJSONTyped(value as RemoteAuthenticatedUserDto, ignoreDiscriminator);
default:
throw new Error(`No variant of AbstractUserDto exists with 'type=${value['type']}'`);
}
}

return {

'username': value['username'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ export function BranchDtoFromJSONTyped(json: any, ignoreDiscriminator: boolean):
};
}

export function BranchDtoToJSON(value?: BranchDto | null): any {
export function BranchDtoToJSON(json: any): BranchDto {
return BranchDtoToJSONTyped(json, false);
}

export function BranchDtoToJSONTyped(value?: BranchDto | null, ignoreDiscriminator: boolean = false): any {
if (value == null) {
return value;
}

return {

'name': value['name'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import {
BranchDtoFromJSON,
BranchDtoFromJSONTyped,
BranchDtoToJSON,
BranchDtoToJSONTyped,
} from './BranchDto';
import type { AbstractUserDto } from './AbstractUserDto';
import {
AbstractUserDtoFromJSON,
AbstractUserDtoFromJSONTyped,
AbstractUserDtoToJSON,
AbstractUserDtoToJSONTyped,
} from './AbstractUserDto';

/**
Expand All @@ -49,7 +51,11 @@ export function InternalAuthenticatedUserDtoFromJSONTyped(json: any, ignoreDiscr
return json;
}

export function InternalAuthenticatedUserDtoToJSON(value?: InternalAuthenticatedUserDto | null): any {
export function InternalAuthenticatedUserDtoToJSON(json: any): InternalAuthenticatedUserDto {
return InternalAuthenticatedUserDtoToJSONTyped(json, false);
}

export function InternalAuthenticatedUserDtoToJSONTyped(value?: InternalAuthenticatedUserDto | null, ignoreDiscriminator: boolean = false): any {
return value;
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import {
BranchDtoFromJSON,
BranchDtoFromJSONTyped,
BranchDtoToJSON,
BranchDtoToJSONTyped,
} from './BranchDto';
import type { AbstractUserDto } from './AbstractUserDto';
import {
AbstractUserDtoFromJSON,
AbstractUserDtoFromJSONTyped,
AbstractUserDtoToJSON,
AbstractUserDtoToJSONTyped,
} from './AbstractUserDto';

/**
Expand All @@ -49,7 +51,11 @@ export function RemoteAuthenticatedUserDtoFromJSONTyped(json: any, ignoreDiscrim
return json;
}

export function RemoteAuthenticatedUserDtoToJSON(value?: RemoteAuthenticatedUserDto | null): any {
export function RemoteAuthenticatedUserDtoToJSON(json: any): RemoteAuthenticatedUserDto {
return RemoteAuthenticatedUserDtoToJSONTyped(json, false);
}

export function RemoteAuthenticatedUserDtoToJSONTyped(value?: RemoteAuthenticatedUserDto | null, ignoreDiscriminator: boolean = false): any {
return value;
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
OwnerFromJSON,
OwnerFromJSONTyped,
OwnerToJSON,
OwnerToJSONTyped,
} from './Owner';

/**
Expand Down Expand Up @@ -55,10 +56,15 @@ export function ClubFromJSONTyped(json: any, ignoreDiscriminator: boolean): Club
};
}

export function ClubToJSON(value?: Club | null): any {
export function ClubToJSON(json: any): Club {
return ClubToJSONTyped(json, false);
}

export function ClubToJSONTyped(value?: Club | null, ignoreDiscriminator: boolean = false): any {
if (value == null) {
return value;
}

return {

'owner': OwnerToJSON(value['owner']),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ export function OwnerFromJSONTyped(json: any, ignoreDiscriminator: boolean): Own
};
}

export function OwnerToJSON(value?: Owner | null): any {
export function OwnerToJSON(json: any): Owner {
return OwnerToJSONTyped(json, false);
}

export function OwnerToJSONTyped(value?: Owner | null, ignoreDiscriminator: boolean = false): any {
if (value == null) {
return value;
}

return {

'name': value['name'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
OwnerFromJSON,
OwnerFromJSONTyped,
OwnerToJSON,
OwnerToJSONTyped,
} from './Owner';

/**
Expand Down Expand Up @@ -55,10 +56,15 @@ export function ClubFromJSONTyped(json: any, ignoreDiscriminator: boolean): Club
};
}

export function ClubToJSON(value?: Omit<Club, 'owner'> | null): any {
export function ClubToJSON(json: any): Club {
return ClubToJSONTyped(json, false);
}

export function ClubToJSONTyped(value?: Omit<Club, 'owner'> | null, ignoreDiscriminator: boolean = false): any {
if (value == null) {
return value;
}

return {

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ export function OwnerFromJSONTyped(json: any, ignoreDiscriminator: boolean): Own
};
}

export function OwnerToJSON(value?: Owner | null): any {
export function OwnerToJSON(json: any): Owner {
return OwnerToJSONTyped(json, false);
}

export function OwnerToJSONTyped(value?: Owner | null, ignoreDiscriminator: boolean = false): any {
if (value == null) {
return value;
}

return {

'name': value['name'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ export function AdditionalPropertiesClassFromJSONTyped(json: any, ignoreDiscrimi
};
}

export function AdditionalPropertiesClassToJSON(value?: AdditionalPropertiesClass | null): any {
export function AdditionalPropertiesClassToJSON(json: any): AdditionalPropertiesClass {
return AdditionalPropertiesClassToJSONTyped(json, false);
}

export function AdditionalPropertiesClassToJSONTyped(value?: AdditionalPropertiesClass | null, ignoreDiscriminator: boolean = false): any {
if (value == null) {
return value;
}

return {

'map_property': value['mapProperty'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
SingleRefTypeFromJSON,
SingleRefTypeFromJSONTyped,
SingleRefTypeToJSON,
SingleRefTypeToJSONTyped,
} from './SingleRefType';

/**
Expand Down Expand Up @@ -64,10 +65,15 @@ export function AllOfWithSingleRefFromJSONTyped(json: any, ignoreDiscriminator:
};
}

export function AllOfWithSingleRefToJSON(value?: AllOfWithSingleRef | null): any {
export function AllOfWithSingleRefToJSON(json: any): AllOfWithSingleRef {
return AllOfWithSingleRefToJSONTyped(json, false);
}

export function AllOfWithSingleRefToJSONTyped(value?: AllOfWithSingleRef | null, ignoreDiscriminator: boolean = false): any {
if (value == null) {
return value;
}

return {

'username': value['username'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*/

import { mapValues } from '../runtime';
import { CatFromJSONTyped } from './Cat';
import { DogFromJSONTyped } from './Dog';
import { Cat, CatFromJSONTyped, CatToJSON, CatToJSONTyped } from './Cat';
import { Dog, DogFromJSONTyped, DogToJSON, DogToJSONTyped } from './Dog';
/**
*
* @export
Expand Down Expand Up @@ -53,10 +53,10 @@ export function AnimalFromJSONTyped(json: any, ignoreDiscriminator: boolean): An
}
if (!ignoreDiscriminator) {
if (json['className'] === 'CAT') {
return CatFromJSONTyped(json, true);
return CatFromJSONTyped(json, ignoreDiscriminator);
}
if (json['className'] === 'DOG') {
return DogFromJSONTyped(json, true);
return DogFromJSONTyped(json, ignoreDiscriminator);
}
}
return {
Expand All @@ -66,10 +66,26 @@ export function AnimalFromJSONTyped(json: any, ignoreDiscriminator: boolean): An
};
}

export function AnimalToJSON(value?: Animal | null): any {
export function AnimalToJSON(json: any): Animal {
return AnimalToJSONTyped(json, false);
}

export function AnimalToJSONTyped(value?: Animal | null, ignoreDiscriminator: boolean = false): any {
if (value == null) {
return value;
}

if (!ignoreDiscriminator) {
switch (value['className']) {
case 'CAT':
return CatToJSONTyped(value as Cat, ignoreDiscriminator);
case 'DOG':
return DogToJSONTyped(value as Dog, ignoreDiscriminator);
default:
throw new Error(`No variant of Animal exists with 'className=${value['className']}'`);
}
}

return {

'className': value['className'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ export function ArrayOfArrayOfNumberOnlyFromJSONTyped(json: any, ignoreDiscrimin
};
}

export function ArrayOfArrayOfNumberOnlyToJSON(value?: ArrayOfArrayOfNumberOnly | null): any {
export function ArrayOfArrayOfNumberOnlyToJSON(json: any): ArrayOfArrayOfNumberOnly {
return ArrayOfArrayOfNumberOnlyToJSONTyped(json, false);
}

export function ArrayOfArrayOfNumberOnlyToJSONTyped(value?: ArrayOfArrayOfNumberOnly | null, ignoreDiscriminator: boolean = false): any {
if (value == null) {
return value;
}

return {

'ArrayArrayNumber': value['arrayArrayNumber'],
Expand Down
Loading

0 comments on commit 2f54a2f

Please sign in to comment.