From c7766457bd93dfbcf5956e9dea0323456809a0ce Mon Sep 17 00:00:00 2001 From: delenius Date: Tue, 9 Feb 2016 10:58:10 -0800 Subject: [PATCH] Don't set missing optional params in models Fixes #2103. In the constructFromObject methods, we were always setting all the parameters, including optional ones that were missing in the `data` object. Because of the `convertToType` calls, this had the effect of producing the *strings* `"undefined"` or `"null"` as property values on the constructed object! This obviously leads to difficult bugs. This commit makes sure that we first check that the data field exists in the data object. Note that both `null` and `undefined` values will be skipped. --- .../main/resources/Javascript/model.mustache | 4 ++- .../javascript-promise/src/model/Category.js | 8 +++-- .../javascript-promise/src/model/Order.js | 24 ++++++++++---- .../javascript-promise/src/model/Pet.js | 24 ++++++++++---- .../javascript-promise/src/model/Tag.js | 8 +++-- .../javascript-promise/src/model/User.js | 32 ++++++++++++++----- .../petstore/javascript/src/model/Category.js | 8 +++-- .../petstore/javascript/src/model/Order.js | 24 ++++++++++---- .../petstore/javascript/src/model/Pet.js | 24 ++++++++++---- .../petstore/javascript/src/model/Tag.js | 8 +++-- .../petstore/javascript/src/model/User.js | 32 ++++++++++++++----- 11 files changed, 147 insertions(+), 49 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/Javascript/model.mustache b/modules/swagger-codegen/src/main/resources/Javascript/model.mustache index 634648d68c..fd64156bb5 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/model.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/model.mustache @@ -40,7 +40,9 @@ return this; } {{#vars}} - this['{{baseName}}']{{{defaultValueWithParam}}} + if (data['{{baseName}}']) { + this['{{baseName}}']{{{defaultValueWithParam}}} + } {{/vars}} return this; } diff --git a/samples/client/petstore/javascript-promise/src/model/Category.js b/samples/client/petstore/javascript-promise/src/model/Category.js index 4b58f1e7c4..26742d7036 100644 --- a/samples/client/petstore/javascript-promise/src/model/Category.js +++ b/samples/client/petstore/javascript-promise/src/model/Category.js @@ -38,9 +38,13 @@ return this; } - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + if (data['id']) { + this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + } - this['name'] = ApiClient.convertToType(data['name'], 'String'); + if (data['name']) { + this['name'] = ApiClient.convertToType(data['name'], 'String'); + } return this; } diff --git a/samples/client/petstore/javascript-promise/src/model/Order.js b/samples/client/petstore/javascript-promise/src/model/Order.js index c9dd8403a4..0d5b90d319 100644 --- a/samples/client/petstore/javascript-promise/src/model/Order.js +++ b/samples/client/petstore/javascript-promise/src/model/Order.js @@ -87,17 +87,29 @@ var StatusEnum = function StatusEnum() { return this; } - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + if (data['id']) { + this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + } - this['petId'] = ApiClient.convertToType(data['petId'], 'Integer'); + if (data['petId']) { + this['petId'] = ApiClient.convertToType(data['petId'], 'Integer'); + } - this['quantity'] = ApiClient.convertToType(data['quantity'], 'Integer'); + if (data['quantity']) { + this['quantity'] = ApiClient.convertToType(data['quantity'], 'Integer'); + } - this['shipDate'] = ApiClient.convertToType(data['shipDate'], 'Date'); + if (data['shipDate']) { + this['shipDate'] = ApiClient.convertToType(data['shipDate'], 'Date'); + } - this['status'] = ApiClient.convertToType(data['status'], 'String'); + if (data['status']) { + this['status'] = ApiClient.convertToType(data['status'], 'String'); + } - this['complete'] = ApiClient.convertToType(data['complete'], 'Boolean'); + if (data['complete']) { + this['complete'] = ApiClient.convertToType(data['complete'], 'Boolean'); + } return this; } diff --git a/samples/client/petstore/javascript-promise/src/model/Pet.js b/samples/client/petstore/javascript-promise/src/model/Pet.js index 60b24a3f8e..67bacb1939 100644 --- a/samples/client/petstore/javascript-promise/src/model/Pet.js +++ b/samples/client/petstore/javascript-promise/src/model/Pet.js @@ -89,17 +89,29 @@ var StatusEnum = function StatusEnum() { return this; } - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + if (data['id']) { + this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + } - this['category'].constructFromObject(data['category']); + if (data['category']) { + this['category'].constructFromObject(data['category']); + } - this['name'] = ApiClient.convertToType(data['name'], 'String'); + if (data['name']) { + this['name'] = ApiClient.convertToType(data['name'], 'String'); + } - this['photoUrls'] = ApiClient.convertToType(data['photoUrls'], ['String']); + if (data['photoUrls']) { + this['photoUrls'] = ApiClient.convertToType(data['photoUrls'], ['String']); + } - this['tags'] = ApiClient.convertToType(data['tags'], [Tag]); + if (data['tags']) { + this['tags'] = ApiClient.convertToType(data['tags'], [Tag]); + } - this['status'] = ApiClient.convertToType(data['status'], 'String'); + if (data['status']) { + this['status'] = ApiClient.convertToType(data['status'], 'String'); + } return this; } diff --git a/samples/client/petstore/javascript-promise/src/model/Tag.js b/samples/client/petstore/javascript-promise/src/model/Tag.js index ac3d537349..563c1ee9df 100644 --- a/samples/client/petstore/javascript-promise/src/model/Tag.js +++ b/samples/client/petstore/javascript-promise/src/model/Tag.js @@ -38,9 +38,13 @@ return this; } - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + if (data['id']) { + this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + } - this['name'] = ApiClient.convertToType(data['name'], 'String'); + if (data['name']) { + this['name'] = ApiClient.convertToType(data['name'], 'String'); + } return this; } diff --git a/samples/client/petstore/javascript-promise/src/model/User.js b/samples/client/petstore/javascript-promise/src/model/User.js index 2ee006e0ac..67c45561d3 100644 --- a/samples/client/petstore/javascript-promise/src/model/User.js +++ b/samples/client/petstore/javascript-promise/src/model/User.js @@ -69,21 +69,37 @@ return this; } - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + if (data['id']) { + this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + } - this['username'] = ApiClient.convertToType(data['username'], 'String'); + if (data['username']) { + this['username'] = ApiClient.convertToType(data['username'], 'String'); + } - this['firstName'] = ApiClient.convertToType(data['firstName'], 'String'); + if (data['firstName']) { + this['firstName'] = ApiClient.convertToType(data['firstName'], 'String'); + } - this['lastName'] = ApiClient.convertToType(data['lastName'], 'String'); + if (data['lastName']) { + this['lastName'] = ApiClient.convertToType(data['lastName'], 'String'); + } - this['email'] = ApiClient.convertToType(data['email'], 'String'); + if (data['email']) { + this['email'] = ApiClient.convertToType(data['email'], 'String'); + } - this['password'] = ApiClient.convertToType(data['password'], 'String'); + if (data['password']) { + this['password'] = ApiClient.convertToType(data['password'], 'String'); + } - this['phone'] = ApiClient.convertToType(data['phone'], 'String'); + if (data['phone']) { + this['phone'] = ApiClient.convertToType(data['phone'], 'String'); + } - this['userStatus'] = ApiClient.convertToType(data['userStatus'], 'Integer'); + if (data['userStatus']) { + this['userStatus'] = ApiClient.convertToType(data['userStatus'], 'Integer'); + } return this; } diff --git a/samples/client/petstore/javascript/src/model/Category.js b/samples/client/petstore/javascript/src/model/Category.js index 4b58f1e7c4..26742d7036 100644 --- a/samples/client/petstore/javascript/src/model/Category.js +++ b/samples/client/petstore/javascript/src/model/Category.js @@ -38,9 +38,13 @@ return this; } - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + if (data['id']) { + this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + } - this['name'] = ApiClient.convertToType(data['name'], 'String'); + if (data['name']) { + this['name'] = ApiClient.convertToType(data['name'], 'String'); + } return this; } diff --git a/samples/client/petstore/javascript/src/model/Order.js b/samples/client/petstore/javascript/src/model/Order.js index c9dd8403a4..0d5b90d319 100644 --- a/samples/client/petstore/javascript/src/model/Order.js +++ b/samples/client/petstore/javascript/src/model/Order.js @@ -87,17 +87,29 @@ var StatusEnum = function StatusEnum() { return this; } - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + if (data['id']) { + this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + } - this['petId'] = ApiClient.convertToType(data['petId'], 'Integer'); + if (data['petId']) { + this['petId'] = ApiClient.convertToType(data['petId'], 'Integer'); + } - this['quantity'] = ApiClient.convertToType(data['quantity'], 'Integer'); + if (data['quantity']) { + this['quantity'] = ApiClient.convertToType(data['quantity'], 'Integer'); + } - this['shipDate'] = ApiClient.convertToType(data['shipDate'], 'Date'); + if (data['shipDate']) { + this['shipDate'] = ApiClient.convertToType(data['shipDate'], 'Date'); + } - this['status'] = ApiClient.convertToType(data['status'], 'String'); + if (data['status']) { + this['status'] = ApiClient.convertToType(data['status'], 'String'); + } - this['complete'] = ApiClient.convertToType(data['complete'], 'Boolean'); + if (data['complete']) { + this['complete'] = ApiClient.convertToType(data['complete'], 'Boolean'); + } return this; } diff --git a/samples/client/petstore/javascript/src/model/Pet.js b/samples/client/petstore/javascript/src/model/Pet.js index 60b24a3f8e..67bacb1939 100644 --- a/samples/client/petstore/javascript/src/model/Pet.js +++ b/samples/client/petstore/javascript/src/model/Pet.js @@ -89,17 +89,29 @@ var StatusEnum = function StatusEnum() { return this; } - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + if (data['id']) { + this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + } - this['category'].constructFromObject(data['category']); + if (data['category']) { + this['category'].constructFromObject(data['category']); + } - this['name'] = ApiClient.convertToType(data['name'], 'String'); + if (data['name']) { + this['name'] = ApiClient.convertToType(data['name'], 'String'); + } - this['photoUrls'] = ApiClient.convertToType(data['photoUrls'], ['String']); + if (data['photoUrls']) { + this['photoUrls'] = ApiClient.convertToType(data['photoUrls'], ['String']); + } - this['tags'] = ApiClient.convertToType(data['tags'], [Tag]); + if (data['tags']) { + this['tags'] = ApiClient.convertToType(data['tags'], [Tag]); + } - this['status'] = ApiClient.convertToType(data['status'], 'String'); + if (data['status']) { + this['status'] = ApiClient.convertToType(data['status'], 'String'); + } return this; } diff --git a/samples/client/petstore/javascript/src/model/Tag.js b/samples/client/petstore/javascript/src/model/Tag.js index ac3d537349..563c1ee9df 100644 --- a/samples/client/petstore/javascript/src/model/Tag.js +++ b/samples/client/petstore/javascript/src/model/Tag.js @@ -38,9 +38,13 @@ return this; } - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + if (data['id']) { + this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + } - this['name'] = ApiClient.convertToType(data['name'], 'String'); + if (data['name']) { + this['name'] = ApiClient.convertToType(data['name'], 'String'); + } return this; } diff --git a/samples/client/petstore/javascript/src/model/User.js b/samples/client/petstore/javascript/src/model/User.js index 2ee006e0ac..67c45561d3 100644 --- a/samples/client/petstore/javascript/src/model/User.js +++ b/samples/client/petstore/javascript/src/model/User.js @@ -69,21 +69,37 @@ return this; } - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + if (data['id']) { + this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + } - this['username'] = ApiClient.convertToType(data['username'], 'String'); + if (data['username']) { + this['username'] = ApiClient.convertToType(data['username'], 'String'); + } - this['firstName'] = ApiClient.convertToType(data['firstName'], 'String'); + if (data['firstName']) { + this['firstName'] = ApiClient.convertToType(data['firstName'], 'String'); + } - this['lastName'] = ApiClient.convertToType(data['lastName'], 'String'); + if (data['lastName']) { + this['lastName'] = ApiClient.convertToType(data['lastName'], 'String'); + } - this['email'] = ApiClient.convertToType(data['email'], 'String'); + if (data['email']) { + this['email'] = ApiClient.convertToType(data['email'], 'String'); + } - this['password'] = ApiClient.convertToType(data['password'], 'String'); + if (data['password']) { + this['password'] = ApiClient.convertToType(data['password'], 'String'); + } - this['phone'] = ApiClient.convertToType(data['phone'], 'String'); + if (data['phone']) { + this['phone'] = ApiClient.convertToType(data['phone'], 'String'); + } - this['userStatus'] = ApiClient.convertToType(data['userStatus'], 'Integer'); + if (data['userStatus']) { + this['userStatus'] = ApiClient.convertToType(data['userStatus'], 'Integer'); + } return this; }