Skip to content

Commit 2f54a2f

Browse files
authored
Fix for #15736 [TYPESCRIPT-FETCH] Subclassing components using discri… (#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.
1 parent e914c40 commit 2f54a2f

File tree

185 files changed

+1213
-202
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+1213
-202
lines changed

modules/openapi-generator/src/main/resources/typescript-fetch/modelEnum.mustache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
2222
export function {{classname}}ToJSON(value?: {{classname}} | null): any {
2323
return value as any;
2424
}
25+
26+
export function {{classname}}ToJSONTyped(value: any, ignoreDiscriminator: boolean): {{classname}} {
27+
return value as {{classname}};
28+
}

modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import {
66
{{classname}}FromJSON,
77
{{classname}}FromJSONTyped,
88
{{classname}}ToJSON,
9+
{{classname}}ToJSONTyped,
910
} from './{{filename}}{{importFileExtension}}';
1011
{{/tsImports}}
1112

1213
{{/hasImports}}
1314
{{#discriminator}}
1415
{{#discriminator.mappedModels}}
15-
import { {{modelName}}FromJSONTyped } from './{{modelName}}{{importFileExtension}}';
16+
import { {{modelName}}, {{modelName}}FromJSONTyped, {{modelName}}ToJSON, {{modelName}}ToJSONTyped } from './{{modelName}}{{importFileExtension}}';
1617
{{/discriminator.mappedModels}}
1718
{{/discriminator}}
1819
{{>modelGenericInterfaces}}
@@ -42,13 +43,13 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
4243
if (!ignoreDiscriminator) {
4344
{{#discriminator.mappedModels}}
4445
if (json['{{discriminator.propertyBaseName}}'] === '{{mappingName}}') {
45-
return {{modelName}}FromJSONTyped(json, true);
46+
return {{modelName}}FromJSONTyped(json, ignoreDiscriminator);
4647
}
4748
{{/discriminator.mappedModels}}
4849
}
4950
{{/discriminator}}
5051
return {
51-
{{#parent}}...{{{.}}}FromJSONTyped(json, ignoreDiscriminator),{{/parent}}
52+
{{#parent}}...{{{.}}}FromJSONTyped(json, true),{{/parent}}
5253
{{#additionalPropertiesType}}
5354
...json,
5455
{{/additionalPropertiesType}}
@@ -97,13 +98,31 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
9798
{{/hasVars}}
9899
}
99100

100-
export function {{classname}}ToJSON(value?: {{#hasReadOnly}}Omit<{{classname}}, {{#readOnlyVars}}'{{baseName}}'{{^-last}}|{{/-last}}{{/readOnlyVars}}>{{/hasReadOnly}}{{^hasReadOnly}}{{classname}}{{/hasReadOnly}} | null): any {
101+
export function {{classname}}ToJSON(json: any): {{classname}} {
102+
return {{classname}}ToJSONTyped(json, false);
103+
}
104+
105+
export function {{classname}}ToJSONTyped(value?: {{#hasReadOnly}}Omit<{{classname}}, {{#readOnlyVars}}'{{baseName}}'{{^-last}}|{{/-last}}{{/readOnlyVars}}>{{/hasReadOnly}}{{^hasReadOnly}}{{classname}}{{/hasReadOnly}} | null, ignoreDiscriminator: boolean = false): any {
101106
{{#hasVars}}
102107
if (value == null) {
103108
return value;
104109
}
110+
{{#discriminator}}
111+
112+
if (!ignoreDiscriminator) {
113+
switch (value['{{discriminator.propertyName}}']) {
114+
{{#discriminator.mappedModels}}
115+
case '{{mappingName}}':
116+
return {{modelName}}ToJSONTyped(value as {{modelName}}, ignoreDiscriminator);
117+
{{/discriminator.mappedModels}}
118+
default:
119+
throw new Error(`No variant of {{classname}} exists with '{{discriminator.propertyName}}=${value['{{discriminator.propertyName}}']}'`);
120+
}
121+
}
122+
{{/discriminator}}
123+
105124
return {
106-
{{#parent}}...{{{.}}}ToJSON(value),{{/parent}}
125+
{{#parent}}...{{{.}}}ToJSONTyped(value, true),{{/parent}}
107126
{{#additionalPropertiesType}}
108127
...value,
109128
{{/additionalPropertiesType}}

samples/client/others/typescript-fetch/self-import-issue/models/AbstractUserDto.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ import {
1818
BranchDtoFromJSON,
1919
BranchDtoFromJSONTyped,
2020
BranchDtoToJSON,
21+
BranchDtoToJSONTyped,
2122
} from './BranchDto';
2223

23-
import { InternalAuthenticatedUserDtoFromJSONTyped } from './InternalAuthenticatedUserDto';
24-
import { RemoteAuthenticatedUserDtoFromJSONTyped } from './RemoteAuthenticatedUserDto';
24+
import { InternalAuthenticatedUserDto, InternalAuthenticatedUserDtoFromJSONTyped, InternalAuthenticatedUserDtoToJSON, InternalAuthenticatedUserDtoToJSONTyped } from './InternalAuthenticatedUserDto';
25+
import { RemoteAuthenticatedUserDto, RemoteAuthenticatedUserDtoFromJSONTyped, RemoteAuthenticatedUserDtoToJSON, RemoteAuthenticatedUserDtoToJSONTyped } from './RemoteAuthenticatedUserDto';
2526
/**
2627
*
2728
* @export
@@ -65,10 +66,10 @@ export function AbstractUserDtoFromJSONTyped(json: any, ignoreDiscriminator: boo
6566
}
6667
if (!ignoreDiscriminator) {
6768
if (json['type'] === 'internal-authenticated') {
68-
return InternalAuthenticatedUserDtoFromJSONTyped(json, true);
69+
return InternalAuthenticatedUserDtoFromJSONTyped(json, ignoreDiscriminator);
6970
}
7071
if (json['type'] === 'remote-authenticated') {
71-
return RemoteAuthenticatedUserDtoFromJSONTyped(json, true);
72+
return RemoteAuthenticatedUserDtoFromJSONTyped(json, ignoreDiscriminator);
7273
}
7374
}
7475
return {
@@ -79,10 +80,26 @@ export function AbstractUserDtoFromJSONTyped(json: any, ignoreDiscriminator: boo
7980
};
8081
}
8182

82-
export function AbstractUserDtoToJSON(value?: AbstractUserDto | null): any {
83+
export function AbstractUserDtoToJSON(json: any): AbstractUserDto {
84+
return AbstractUserDtoToJSONTyped(json, false);
85+
}
86+
87+
export function AbstractUserDtoToJSONTyped(value?: AbstractUserDto | null, ignoreDiscriminator: boolean = false): any {
8388
if (value == null) {
8489
return value;
8590
}
91+
92+
if (!ignoreDiscriminator) {
93+
switch (value['type']) {
94+
case 'internal-authenticated':
95+
return InternalAuthenticatedUserDtoToJSONTyped(value as InternalAuthenticatedUserDto, ignoreDiscriminator);
96+
case 'remote-authenticated':
97+
return RemoteAuthenticatedUserDtoToJSONTyped(value as RemoteAuthenticatedUserDto, ignoreDiscriminator);
98+
default:
99+
throw new Error(`No variant of AbstractUserDto exists with 'type=${value['type']}'`);
100+
}
101+
}
102+
86103
return {
87104

88105
'username': value['username'],

samples/client/others/typescript-fetch/self-import-issue/models/BranchDto.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ export function BranchDtoFromJSONTyped(json: any, ignoreDiscriminator: boolean):
4848
};
4949
}
5050

51-
export function BranchDtoToJSON(value?: BranchDto | null): any {
51+
export function BranchDtoToJSON(json: any): BranchDto {
52+
return BranchDtoToJSONTyped(json, false);
53+
}
54+
55+
export function BranchDtoToJSONTyped(value?: BranchDto | null, ignoreDiscriminator: boolean = false): any {
5256
if (value == null) {
5357
return value;
5458
}
59+
5560
return {
5661

5762
'name': value['name'],

samples/client/others/typescript-fetch/self-import-issue/models/InternalAuthenticatedUserDto.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ import {
1818
BranchDtoFromJSON,
1919
BranchDtoFromJSONTyped,
2020
BranchDtoToJSON,
21+
BranchDtoToJSONTyped,
2122
} from './BranchDto';
2223
import type { AbstractUserDto } from './AbstractUserDto';
2324
import {
2425
AbstractUserDtoFromJSON,
2526
AbstractUserDtoFromJSONTyped,
2627
AbstractUserDtoToJSON,
28+
AbstractUserDtoToJSONTyped,
2729
} from './AbstractUserDto';
2830

2931
/**
@@ -49,7 +51,11 @@ export function InternalAuthenticatedUserDtoFromJSONTyped(json: any, ignoreDiscr
4951
return json;
5052
}
5153

52-
export function InternalAuthenticatedUserDtoToJSON(value?: InternalAuthenticatedUserDto | null): any {
54+
export function InternalAuthenticatedUserDtoToJSON(json: any): InternalAuthenticatedUserDto {
55+
return InternalAuthenticatedUserDtoToJSONTyped(json, false);
56+
}
57+
58+
export function InternalAuthenticatedUserDtoToJSONTyped(value?: InternalAuthenticatedUserDto | null, ignoreDiscriminator: boolean = false): any {
5359
return value;
5460
}
5561

samples/client/others/typescript-fetch/self-import-issue/models/RemoteAuthenticatedUserDto.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ import {
1818
BranchDtoFromJSON,
1919
BranchDtoFromJSONTyped,
2020
BranchDtoToJSON,
21+
BranchDtoToJSONTyped,
2122
} from './BranchDto';
2223
import type { AbstractUserDto } from './AbstractUserDto';
2324
import {
2425
AbstractUserDtoFromJSON,
2526
AbstractUserDtoFromJSONTyped,
2627
AbstractUserDtoToJSON,
28+
AbstractUserDtoToJSONTyped,
2729
} from './AbstractUserDto';
2830

2931
/**
@@ -49,7 +51,11 @@ export function RemoteAuthenticatedUserDtoFromJSONTyped(json: any, ignoreDiscrim
4951
return json;
5052
}
5153

52-
export function RemoteAuthenticatedUserDtoToJSON(value?: RemoteAuthenticatedUserDto | null): any {
54+
export function RemoteAuthenticatedUserDtoToJSON(json: any): RemoteAuthenticatedUserDto {
55+
return RemoteAuthenticatedUserDtoToJSONTyped(json, false);
56+
}
57+
58+
export function RemoteAuthenticatedUserDtoToJSONTyped(value?: RemoteAuthenticatedUserDto | null, ignoreDiscriminator: boolean = false): any {
5359
return value;
5460
}
5561

samples/client/petstore/typescript-fetch/builds/allOf-nullable/models/Club.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
OwnerFromJSON,
1919
OwnerFromJSONTyped,
2020
OwnerToJSON,
21+
OwnerToJSONTyped,
2122
} from './Owner';
2223

2324
/**
@@ -55,10 +56,15 @@ export function ClubFromJSONTyped(json: any, ignoreDiscriminator: boolean): Club
5556
};
5657
}
5758

58-
export function ClubToJSON(value?: Club | null): any {
59+
export function ClubToJSON(json: any): Club {
60+
return ClubToJSONTyped(json, false);
61+
}
62+
63+
export function ClubToJSONTyped(value?: Club | null, ignoreDiscriminator: boolean = false): any {
5964
if (value == null) {
6065
return value;
6166
}
67+
6268
return {
6369

6470
'owner': OwnerToJSON(value['owner']),

samples/client/petstore/typescript-fetch/builds/allOf-nullable/models/Owner.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ export function OwnerFromJSONTyped(json: any, ignoreDiscriminator: boolean): Own
4848
};
4949
}
5050

51-
export function OwnerToJSON(value?: Owner | null): any {
51+
export function OwnerToJSON(json: any): Owner {
52+
return OwnerToJSONTyped(json, false);
53+
}
54+
55+
export function OwnerToJSONTyped(value?: Owner | null, ignoreDiscriminator: boolean = false): any {
5256
if (value == null) {
5357
return value;
5458
}
59+
5560
return {
5661

5762
'name': value['name'],

samples/client/petstore/typescript-fetch/builds/allOf-readonly/models/Club.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
OwnerFromJSON,
1919
OwnerFromJSONTyped,
2020
OwnerToJSON,
21+
OwnerToJSONTyped,
2122
} from './Owner';
2223

2324
/**
@@ -55,10 +56,15 @@ export function ClubFromJSONTyped(json: any, ignoreDiscriminator: boolean): Club
5556
};
5657
}
5758

58-
export function ClubToJSON(value?: Omit<Club, 'owner'> | null): any {
59+
export function ClubToJSON(json: any): Club {
60+
return ClubToJSONTyped(json, false);
61+
}
62+
63+
export function ClubToJSONTyped(value?: Omit<Club, 'owner'> | null, ignoreDiscriminator: boolean = false): any {
5964
if (value == null) {
6065
return value;
6166
}
67+
6268
return {
6369

6470
};

samples/client/petstore/typescript-fetch/builds/allOf-readonly/models/Owner.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ export function OwnerFromJSONTyped(json: any, ignoreDiscriminator: boolean): Own
4848
};
4949
}
5050

51-
export function OwnerToJSON(value?: Owner | null): any {
51+
export function OwnerToJSON(json: any): Owner {
52+
return OwnerToJSONTyped(json, false);
53+
}
54+
55+
export function OwnerToJSONTyped(value?: Owner | null, ignoreDiscriminator: boolean = false): any {
5256
if (value == null) {
5357
return value;
5458
}
59+
5560
return {
5661

5762
'name': value['name'],

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/AdditionalPropertiesClass.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,15 @@ export function AdditionalPropertiesClassFromJSONTyped(json: any, ignoreDiscrimi
5555
};
5656
}
5757

58-
export function AdditionalPropertiesClassToJSON(value?: AdditionalPropertiesClass | null): any {
58+
export function AdditionalPropertiesClassToJSON(json: any): AdditionalPropertiesClass {
59+
return AdditionalPropertiesClassToJSONTyped(json, false);
60+
}
61+
62+
export function AdditionalPropertiesClassToJSONTyped(value?: AdditionalPropertiesClass | null, ignoreDiscriminator: boolean = false): any {
5963
if (value == null) {
6064
return value;
6165
}
66+
6267
return {
6368

6469
'map_property': value['mapProperty'],

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/AllOfWithSingleRef.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
SingleRefTypeFromJSON,
1919
SingleRefTypeFromJSONTyped,
2020
SingleRefTypeToJSON,
21+
SingleRefTypeToJSONTyped,
2122
} from './SingleRefType';
2223

2324
/**
@@ -64,10 +65,15 @@ export function AllOfWithSingleRefFromJSONTyped(json: any, ignoreDiscriminator:
6465
};
6566
}
6667

67-
export function AllOfWithSingleRefToJSON(value?: AllOfWithSingleRef | null): any {
68+
export function AllOfWithSingleRefToJSON(json: any): AllOfWithSingleRef {
69+
return AllOfWithSingleRefToJSONTyped(json, false);
70+
}
71+
72+
export function AllOfWithSingleRefToJSONTyped(value?: AllOfWithSingleRef | null, ignoreDiscriminator: boolean = false): any {
6873
if (value == null) {
6974
return value;
7075
}
76+
7177
return {
7278

7379
'username': value['username'],

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/Animal.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
*/
1414

1515
import { mapValues } from '../runtime';
16-
import { CatFromJSONTyped } from './Cat';
17-
import { DogFromJSONTyped } from './Dog';
16+
import { Cat, CatFromJSONTyped, CatToJSON, CatToJSONTyped } from './Cat';
17+
import { Dog, DogFromJSONTyped, DogToJSON, DogToJSONTyped } from './Dog';
1818
/**
1919
*
2020
* @export
@@ -53,10 +53,10 @@ export function AnimalFromJSONTyped(json: any, ignoreDiscriminator: boolean): An
5353
}
5454
if (!ignoreDiscriminator) {
5555
if (json['className'] === 'CAT') {
56-
return CatFromJSONTyped(json, true);
56+
return CatFromJSONTyped(json, ignoreDiscriminator);
5757
}
5858
if (json['className'] === 'DOG') {
59-
return DogFromJSONTyped(json, true);
59+
return DogFromJSONTyped(json, ignoreDiscriminator);
6060
}
6161
}
6262
return {
@@ -66,10 +66,26 @@ export function AnimalFromJSONTyped(json: any, ignoreDiscriminator: boolean): An
6666
};
6767
}
6868

69-
export function AnimalToJSON(value?: Animal | null): any {
69+
export function AnimalToJSON(json: any): Animal {
70+
return AnimalToJSONTyped(json, false);
71+
}
72+
73+
export function AnimalToJSONTyped(value?: Animal | null, ignoreDiscriminator: boolean = false): any {
7074
if (value == null) {
7175
return value;
7276
}
77+
78+
if (!ignoreDiscriminator) {
79+
switch (value['className']) {
80+
case 'CAT':
81+
return CatToJSONTyped(value as Cat, ignoreDiscriminator);
82+
case 'DOG':
83+
return DogToJSONTyped(value as Dog, ignoreDiscriminator);
84+
default:
85+
throw new Error(`No variant of Animal exists with 'className=${value['className']}'`);
86+
}
87+
}
88+
7389
return {
7490

7591
'className': value['className'],

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/ArrayOfArrayOfNumberOnly.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ export function ArrayOfArrayOfNumberOnlyFromJSONTyped(json: any, ignoreDiscrimin
4848
};
4949
}
5050

51-
export function ArrayOfArrayOfNumberOnlyToJSON(value?: ArrayOfArrayOfNumberOnly | null): any {
51+
export function ArrayOfArrayOfNumberOnlyToJSON(json: any): ArrayOfArrayOfNumberOnly {
52+
return ArrayOfArrayOfNumberOnlyToJSONTyped(json, false);
53+
}
54+
55+
export function ArrayOfArrayOfNumberOnlyToJSONTyped(value?: ArrayOfArrayOfNumberOnly | null, ignoreDiscriminator: boolean = false): any {
5256
if (value == null) {
5357
return value;
5458
}
59+
5560
return {
5661

5762
'ArrayArrayNumber': value['arrayArrayNumber'],

0 commit comments

Comments
 (0)