Skip to content

Commit 28cf0b2

Browse files
stephanpelikanmacjohnny
authored andcommitted
[typescript-fetch] fix serialization/deserialization with inheritance (#3767)
* #3646 - fix inheritence * #3646: Fix imports * Update modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache Co-Authored-By: Esteban Gehring <[email protected]> * generate typeescript-fetch samples
1 parent 31d7bf9 commit 28cf0b2

File tree

38 files changed

+478
-0
lines changed

38 files changed

+478
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export enum {{classname}} {
1212
}
1313

1414
export function {{classname}}FromJSON(json: any): {{classname}} {
15+
return {{classname}}FromJSONTyped(json, false);
16+
}
17+
18+
export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boolean): {{classname}} {
1519
return json as {{classname}};
1620
}
1721

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

+30
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ import {
44
{{#imports}}
55
{{{.}}},
66
{{.}}FromJSON,
7+
{{.}}FromJSONTyped,
78
{{.}}ToJSON,
89
{{/imports}}
910
} from './';
1011

1112
{{/hasImports}}
13+
{{#discriminator}}
14+
import {
15+
{{#discriminator.mappedModels}}
16+
{{modelName}}FromJSONTyped
17+
{{/discriminator.mappedModels}}
18+
} from './';
19+
20+
{{/discriminator}}
1221
/**
1322
* {{{description}}}
1423
* @export
@@ -29,8 +38,25 @@ export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
2938
}
3039

3140
export function {{classname}}FromJSON(json: any): {{classname}} {
41+
return {{classname}}FromJSONTyped(json, false);
42+
}
43+
44+
export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boolean): {{classname}} {
3245
{{#hasVars}}
46+
if ((json === undefined) || (json === null)) {
47+
return json;
48+
}
49+
{{#discriminator}}
50+
if (!ignoreDiscriminator) {
51+
{{#discriminator.mappedModels}}
52+
if (json['{{discriminator.propertyName}}'] === '{{modelName}}') {
53+
return {{modelName}}FromJSONTyped(json, true);
54+
}
55+
{{/discriminator.mappedModels}}
56+
}
57+
{{/discriminator}}
3358
return {
59+
{{#parent}}...{{{parent}}}FromJSONTyped(json, ignoreDiscriminator),{{/parent}}
3460
{{#additionalPropertiesType}}
3561
...json,
3662
{{/additionalPropertiesType}}
@@ -79,7 +105,11 @@ export function {{classname}}ToJSON(value?: {{classname}}): any {
79105
if (value === undefined) {
80106
return undefined;
81107
}
108+
if (value === null) {
109+
return null;
110+
}
82111
return {
112+
{{#parent}}...{{{parent}}}ToJSON(value),{{/parent}}
83113
{{#additionalPropertiesType}}
84114
...value,
85115
{{/additionalPropertiesType}}

samples/client/petstore/typescript-fetch/builds/default/src/models/Category.ts

+12
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ export interface Category {
3333
}
3434

3535
export function CategoryFromJSON(json: any): Category {
36+
return CategoryFromJSONTyped(json, false);
37+
}
38+
39+
export function CategoryFromJSONTyped(json: any, ignoreDiscriminator: boolean): Category {
40+
if ((json === undefined) || (json === null)) {
41+
return json;
42+
}
3643
return {
44+
3745
'id': !exists(json, 'id') ? undefined : json['id'],
3846
'name': !exists(json, 'name') ? undefined : json['name'],
3947
};
@@ -43,7 +51,11 @@ export function CategoryToJSON(value?: Category): any {
4351
if (value === undefined) {
4452
return undefined;
4553
}
54+
if (value === null) {
55+
return null;
56+
}
4657
return {
58+
4759
'id': value.id,
4860
'name': value.name,
4961
};

samples/client/petstore/typescript-fetch/builds/default/src/models/ModelApiResponse.ts

+12
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@ export interface ModelApiResponse {
3939
}
4040

4141
export function ModelApiResponseFromJSON(json: any): ModelApiResponse {
42+
return ModelApiResponseFromJSONTyped(json, false);
43+
}
44+
45+
export function ModelApiResponseFromJSONTyped(json: any, ignoreDiscriminator: boolean): ModelApiResponse {
46+
if ((json === undefined) || (json === null)) {
47+
return json;
48+
}
4249
return {
50+
4351
'code': !exists(json, 'code') ? undefined : json['code'],
4452
'type': !exists(json, 'type') ? undefined : json['type'],
4553
'message': !exists(json, 'message') ? undefined : json['message'],
@@ -50,7 +58,11 @@ export function ModelApiResponseToJSON(value?: ModelApiResponse): any {
5058
if (value === undefined) {
5159
return undefined;
5260
}
61+
if (value === null) {
62+
return null;
63+
}
5364
return {
65+
5466
'code': value.code,
5567
'type': value.type,
5668
'message': value.message,

samples/client/petstore/typescript-fetch/builds/default/src/models/Order.ts

+12
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ export interface Order {
5757
}
5858

5959
export function OrderFromJSON(json: any): Order {
60+
return OrderFromJSONTyped(json, false);
61+
}
62+
63+
export function OrderFromJSONTyped(json: any, ignoreDiscriminator: boolean): Order {
64+
if ((json === undefined) || (json === null)) {
65+
return json;
66+
}
6067
return {
68+
6169
'id': !exists(json, 'id') ? undefined : json['id'],
6270
'petId': !exists(json, 'petId') ? undefined : json['petId'],
6371
'quantity': !exists(json, 'quantity') ? undefined : json['quantity'],
@@ -71,7 +79,11 @@ export function OrderToJSON(value?: Order): any {
7179
if (value === undefined) {
7280
return undefined;
7381
}
82+
if (value === null) {
83+
return null;
84+
}
7485
return {
86+
7587
'id': value.id,
7688
'petId': value.petId,
7789
'quantity': value.quantity,

samples/client/petstore/typescript-fetch/builds/default/src/models/Pet.ts

+14
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import { exists, mapValues } from '../runtime';
1515
import {
1616
Category,
1717
CategoryFromJSON,
18+
CategoryFromJSONTyped,
1819
CategoryToJSON,
1920
Tag,
2021
TagFromJSON,
22+
TagFromJSONTyped,
2123
TagToJSON,
2224
} from './';
2325

@@ -66,7 +68,15 @@ export interface Pet {
6668
}
6769

6870
export function PetFromJSON(json: any): Pet {
71+
return PetFromJSONTyped(json, false);
72+
}
73+
74+
export function PetFromJSONTyped(json: any, ignoreDiscriminator: boolean): Pet {
75+
if ((json === undefined) || (json === null)) {
76+
return json;
77+
}
6978
return {
79+
7080
'id': !exists(json, 'id') ? undefined : json['id'],
7181
'category': !exists(json, 'category') ? undefined : CategoryFromJSON(json['category']),
7282
'name': json['name'],
@@ -80,7 +90,11 @@ export function PetToJSON(value?: Pet): any {
8090
if (value === undefined) {
8191
return undefined;
8292
}
93+
if (value === null) {
94+
return null;
95+
}
8396
return {
97+
8498
'id': value.id,
8599
'category': CategoryToJSON(value.category),
86100
'name': value.name,

samples/client/petstore/typescript-fetch/builds/default/src/models/Tag.ts

+12
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ export interface Tag {
3333
}
3434

3535
export function TagFromJSON(json: any): Tag {
36+
return TagFromJSONTyped(json, false);
37+
}
38+
39+
export function TagFromJSONTyped(json: any, ignoreDiscriminator: boolean): Tag {
40+
if ((json === undefined) || (json === null)) {
41+
return json;
42+
}
3643
return {
44+
3745
'id': !exists(json, 'id') ? undefined : json['id'],
3846
'name': !exists(json, 'name') ? undefined : json['name'],
3947
};
@@ -43,7 +51,11 @@ export function TagToJSON(value?: Tag): any {
4351
if (value === undefined) {
4452
return undefined;
4553
}
54+
if (value === null) {
55+
return null;
56+
}
4657
return {
58+
4759
'id': value.id,
4860
'name': value.name,
4961
};

samples/client/petstore/typescript-fetch/builds/default/src/models/User.ts

+12
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,15 @@ export interface User {
6969
}
7070

7171
export function UserFromJSON(json: any): User {
72+
return UserFromJSONTyped(json, false);
73+
}
74+
75+
export function UserFromJSONTyped(json: any, ignoreDiscriminator: boolean): User {
76+
if ((json === undefined) || (json === null)) {
77+
return json;
78+
}
7279
return {
80+
7381
'id': !exists(json, 'id') ? undefined : json['id'],
7482
'username': !exists(json, 'username') ? undefined : json['username'],
7583
'firstName': !exists(json, 'firstName') ? undefined : json['firstName'],
@@ -85,7 +93,11 @@ export function UserToJSON(value?: User): any {
8593
if (value === undefined) {
8694
return undefined;
8795
}
96+
if (value === null) {
97+
return null;
98+
}
8899
return {
100+
89101
'id': value.id,
90102
'username': value.username,
91103
'firstName': value.firstName,

samples/client/petstore/typescript-fetch/builds/es6-target/src/models/Category.ts

+12
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ export interface Category {
3333
}
3434

3535
export function CategoryFromJSON(json: any): Category {
36+
return CategoryFromJSONTyped(json, false);
37+
}
38+
39+
export function CategoryFromJSONTyped(json: any, ignoreDiscriminator: boolean): Category {
40+
if ((json === undefined) || (json === null)) {
41+
return json;
42+
}
3643
return {
44+
3745
'id': !exists(json, 'id') ? undefined : json['id'],
3846
'name': !exists(json, 'name') ? undefined : json['name'],
3947
};
@@ -43,7 +51,11 @@ export function CategoryToJSON(value?: Category): any {
4351
if (value === undefined) {
4452
return undefined;
4553
}
54+
if (value === null) {
55+
return null;
56+
}
4657
return {
58+
4759
'id': value.id,
4860
'name': value.name,
4961
};

samples/client/petstore/typescript-fetch/builds/es6-target/src/models/ModelApiResponse.ts

+12
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@ export interface ModelApiResponse {
3939
}
4040

4141
export function ModelApiResponseFromJSON(json: any): ModelApiResponse {
42+
return ModelApiResponseFromJSONTyped(json, false);
43+
}
44+
45+
export function ModelApiResponseFromJSONTyped(json: any, ignoreDiscriminator: boolean): ModelApiResponse {
46+
if ((json === undefined) || (json === null)) {
47+
return json;
48+
}
4249
return {
50+
4351
'code': !exists(json, 'code') ? undefined : json['code'],
4452
'type': !exists(json, 'type') ? undefined : json['type'],
4553
'message': !exists(json, 'message') ? undefined : json['message'],
@@ -50,7 +58,11 @@ export function ModelApiResponseToJSON(value?: ModelApiResponse): any {
5058
if (value === undefined) {
5159
return undefined;
5260
}
61+
if (value === null) {
62+
return null;
63+
}
5364
return {
65+
5466
'code': value.code,
5567
'type': value.type,
5668
'message': value.message,

samples/client/petstore/typescript-fetch/builds/es6-target/src/models/Order.ts

+12
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ export interface Order {
5757
}
5858

5959
export function OrderFromJSON(json: any): Order {
60+
return OrderFromJSONTyped(json, false);
61+
}
62+
63+
export function OrderFromJSONTyped(json: any, ignoreDiscriminator: boolean): Order {
64+
if ((json === undefined) || (json === null)) {
65+
return json;
66+
}
6067
return {
68+
6169
'id': !exists(json, 'id') ? undefined : json['id'],
6270
'petId': !exists(json, 'petId') ? undefined : json['petId'],
6371
'quantity': !exists(json, 'quantity') ? undefined : json['quantity'],
@@ -71,7 +79,11 @@ export function OrderToJSON(value?: Order): any {
7179
if (value === undefined) {
7280
return undefined;
7381
}
82+
if (value === null) {
83+
return null;
84+
}
7485
return {
86+
7587
'id': value.id,
7688
'petId': value.petId,
7789
'quantity': value.quantity,

samples/client/petstore/typescript-fetch/builds/es6-target/src/models/Pet.ts

+14
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import { exists, mapValues } from '../runtime';
1515
import {
1616
Category,
1717
CategoryFromJSON,
18+
CategoryFromJSONTyped,
1819
CategoryToJSON,
1920
Tag,
2021
TagFromJSON,
22+
TagFromJSONTyped,
2123
TagToJSON,
2224
} from './';
2325

@@ -66,7 +68,15 @@ export interface Pet {
6668
}
6769

6870
export function PetFromJSON(json: any): Pet {
71+
return PetFromJSONTyped(json, false);
72+
}
73+
74+
export function PetFromJSONTyped(json: any, ignoreDiscriminator: boolean): Pet {
75+
if ((json === undefined) || (json === null)) {
76+
return json;
77+
}
6978
return {
79+
7080
'id': !exists(json, 'id') ? undefined : json['id'],
7181
'category': !exists(json, 'category') ? undefined : CategoryFromJSON(json['category']),
7282
'name': json['name'],
@@ -80,7 +90,11 @@ export function PetToJSON(value?: Pet): any {
8090
if (value === undefined) {
8191
return undefined;
8292
}
93+
if (value === null) {
94+
return null;
95+
}
8396
return {
97+
8498
'id': value.id,
8599
'category': CategoryToJSON(value.category),
86100
'name': value.name,

0 commit comments

Comments
 (0)