From 9ea88db1dba6ac6ae69e34cc370093aec3058c2a Mon Sep 17 00:00:00 2001 From: Geoff Brown Date: Fri, 18 May 2018 11:16:47 -0400 Subject: [PATCH 1/2] 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. --- .../resources/typescript-node/api.mustache | 10 ++++++++- .../petstore/typescript-node/default/api.ts | 22 +++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/typescript-node/api.mustache b/modules/openapi-generator/src/main/resources/typescript-node/api.mustache index c12d04a4ddce..098c2feb11c1 100644 --- a/modules/openapi-generator/src/main/resources/typescript-node/api.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-node/api.mustache @@ -47,7 +47,12 @@ class ObjectSerializer { return expectedType; // the type does not have a discriminator. use it. } else { if (data[discriminatorProperty]) { - return data[discriminatorProperty]; // use the type given in the discriminator + var discriminatorType = data[discriminatorProperty]; + if(typeMap[discriminatorType]){ + return discriminatorType; // use the type given in the discriminator + } else { + return expectedType; // discriminator did not map to a type + } } else { return expectedType; // discriminator was not present (or an empty string) } @@ -78,6 +83,9 @@ class ObjectSerializer { if (!typeMap[type]) { // in case we dont know the type return data; } + + // Get the actual type of this object + type = this.findCorrectType(data, type); // get the map for the correct type. let attributeTypes = typeMap[type].getAttributeTypeMap(); diff --git a/samples/client/petstore/typescript-node/default/api.ts b/samples/client/petstore/typescript-node/default/api.ts index 13b8db92be6f..d534f24414cf 100644 --- a/samples/client/petstore/typescript-node/default/api.ts +++ b/samples/client/petstore/typescript-node/default/api.ts @@ -56,7 +56,12 @@ class ObjectSerializer { return expectedType; // the type does not have a discriminator. use it. } else { if (data[discriminatorProperty]) { - return data[discriminatorProperty]; // use the type given in the discriminator + var discriminatorType = data[discriminatorProperty]; + if(typeMap[discriminatorType]){ + return discriminatorType; // use the type given in the discriminator + } else { + return expectedType; // discriminator did not map to a type + } } else { return expectedType; // discriminator was not present (or an empty string) } @@ -87,6 +92,9 @@ class ObjectSerializer { if (!typeMap[type]) { // in case we dont know the type return data; } + + // Get the actual type of this object + type = this.findCorrectType(data, type); // get the map for the correct type. let attributeTypes = typeMap[type].getAttributeTypeMap(); @@ -144,7 +152,7 @@ export class ApiResponse { 'type'?: string; 'message'?: string; - static discriminator = undefined; + static discriminator: string | undefined = undefined; static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [ { @@ -175,7 +183,7 @@ export class Category { 'id'?: number; 'name'?: string; - static discriminator = undefined; + static discriminator: string | undefined = undefined; static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [ { @@ -208,7 +216,7 @@ export class Order { 'status'?: Order.StatusEnum; 'complete'?: boolean; - static discriminator = undefined; + static discriminator: string | undefined = undefined; static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [ { @@ -268,7 +276,7 @@ export class Pet { */ 'status'?: Pet.StatusEnum; - static discriminator = undefined; + static discriminator: string | undefined = undefined; static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [ { @@ -321,7 +329,7 @@ export class Tag { 'id'?: number; 'name'?: string; - static discriminator = undefined; + static discriminator: string | undefined = undefined; static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [ { @@ -356,7 +364,7 @@ export class User { */ 'userStatus'?: number; - static discriminator = undefined; + static discriminator: string | undefined = undefined; static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [ { From 1b9925c9e0f9c7a298840c5ce268af4b4c685305 Mon Sep 17 00:00:00 2001 From: Geoff Brown Date: Fri, 18 May 2018 11:30:29 -0400 Subject: [PATCH 2/2] Fix improper whitespace in mustache template (PHNX-859) (#2) Motivation ---- OpenAPI Generator upstream requested whitespace fixes (from tabs to 4 spaces) Modifications ---- Fixed whitespace --- .../src/main/resources/typescript-node/api.mustache | 4 ++-- samples/client/petstore/typescript-node/default/api.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/typescript-node/api.mustache b/modules/openapi-generator/src/main/resources/typescript-node/api.mustache index 098c2feb11c1..8916679189fc 100644 --- a/modules/openapi-generator/src/main/resources/typescript-node/api.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-node/api.mustache @@ -83,8 +83,8 @@ class ObjectSerializer { if (!typeMap[type]) { // in case we dont know the type return data; } - - // Get the actual type of this object + + // Get the actual type of this object type = this.findCorrectType(data, type); // get the map for the correct type. diff --git a/samples/client/petstore/typescript-node/default/api.ts b/samples/client/petstore/typescript-node/default/api.ts index d534f24414cf..a6e6c97eb340 100644 --- a/samples/client/petstore/typescript-node/default/api.ts +++ b/samples/client/petstore/typescript-node/default/api.ts @@ -92,8 +92,8 @@ class ObjectSerializer { if (!typeMap[type]) { // in case we dont know the type return data; } - - // Get the actual type of this object + + // Get the actual type of this object type = this.findCorrectType(data, type); // get the map for the correct type.