Skip to content

Commit 9b86023

Browse files
gbrown-cewing328
authored andcommitted
[TypeScript] Make OpenAPI Generator serialize subclasses properly (OpenAPITools#102)
* Make SwaggerCodeGen serialize subclasses properly (PHNX-859) (#1) Motivation ---- Previously, when serializing as subclass of a property, generated swagger clients would only serialize properties of the parent class causing some values to not be pass through Modifications ---- Before serializing attributes of a given type, we check to see if there is a specific type to be serialized so that we don't miss any properties. * Fix improper whitespace in mustache template (PHNX-859) (#2) Motivation ---- OpenAPI Generator upstream requested whitespace fixes (from tabs to 4 spaces) Modifications ---- Fixed whitespace
1 parent 71b5de3 commit 9b86023

File tree

2 files changed

+24
-8
lines changed
  • modules/openapi-generator/src/main/resources/typescript-node
  • samples/client/petstore/typescript-node/default

2 files changed

+24
-8
lines changed

modules/openapi-generator/src/main/resources/typescript-node/api.mustache

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ class ObjectSerializer {
4747
return expectedType; // the type does not have a discriminator. use it.
4848
} else {
4949
if (data[discriminatorProperty]) {
50-
return data[discriminatorProperty]; // use the type given in the discriminator
50+
var discriminatorType = data[discriminatorProperty];
51+
if(typeMap[discriminatorType]){
52+
return discriminatorType; // use the type given in the discriminator
53+
} else {
54+
return expectedType; // discriminator did not map to a type
55+
}
5156
} else {
5257
return expectedType; // discriminator was not present (or an empty string)
5358
}
@@ -78,6 +83,9 @@ class ObjectSerializer {
7883
if (!typeMap[type]) { // in case we dont know the type
7984
return data;
8085
}
86+
87+
// Get the actual type of this object
88+
type = this.findCorrectType(data, type);
8189
8290
// get the map for the correct type.
8391
let attributeTypes = typeMap[type].getAttributeTypeMap();

samples/client/petstore/typescript-node/default/api.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ class ObjectSerializer {
5656
return expectedType; // the type does not have a discriminator. use it.
5757
} else {
5858
if (data[discriminatorProperty]) {
59-
return data[discriminatorProperty]; // use the type given in the discriminator
59+
var discriminatorType = data[discriminatorProperty];
60+
if(typeMap[discriminatorType]){
61+
return discriminatorType; // use the type given in the discriminator
62+
} else {
63+
return expectedType; // discriminator did not map to a type
64+
}
6065
} else {
6166
return expectedType; // discriminator was not present (or an empty string)
6267
}
@@ -87,6 +92,9 @@ class ObjectSerializer {
8792
if (!typeMap[type]) { // in case we dont know the type
8893
return data;
8994
}
95+
96+
// Get the actual type of this object
97+
type = this.findCorrectType(data, type);
9098

9199
// get the map for the correct type.
92100
let attributeTypes = typeMap[type].getAttributeTypeMap();
@@ -144,7 +152,7 @@ export class ApiResponse {
144152
'type'?: string;
145153
'message'?: string;
146154

147-
static discriminator = undefined;
155+
static discriminator: string | undefined = undefined;
148156

149157
static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [
150158
{
@@ -175,7 +183,7 @@ export class Category {
175183
'id'?: number;
176184
'name'?: string;
177185

178-
static discriminator = undefined;
186+
static discriminator: string | undefined = undefined;
179187

180188
static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [
181189
{
@@ -208,7 +216,7 @@ export class Order {
208216
'status'?: Order.StatusEnum;
209217
'complete'?: boolean;
210218

211-
static discriminator = undefined;
219+
static discriminator: string | undefined = undefined;
212220

213221
static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [
214222
{
@@ -268,7 +276,7 @@ export class Pet {
268276
*/
269277
'status'?: Pet.StatusEnum;
270278

271-
static discriminator = undefined;
279+
static discriminator: string | undefined = undefined;
272280

273281
static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [
274282
{
@@ -321,7 +329,7 @@ export class Tag {
321329
'id'?: number;
322330
'name'?: string;
323331

324-
static discriminator = undefined;
332+
static discriminator: string | undefined = undefined;
325333

326334
static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [
327335
{
@@ -356,7 +364,7 @@ export class User {
356364
*/
357365
'userStatus'?: number;
358366

359-
static discriminator = undefined;
367+
static discriminator: string | undefined = undefined;
360368

361369
static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [
362370
{

0 commit comments

Comments
 (0)