From fa2333717a1e8d837e28be4d1a26c284ec5fd3d4 Mon Sep 17 00:00:00 2001 From: delenius Date: Wed, 10 Feb 2016 13:35:24 -0800 Subject: [PATCH] Use static model factory methods The `constructFromObject` factory methods should be class methods (or "static" methods), not instance methods. With this commit, ApiClient no longer calls the model constructors directly. Instead, it calls the new static factory method to get the new instance. If there is no data on the top level, null is returned. It is still possible for users to call the model constructors directly, of course. --- .../languages/JavascriptClientCodegen.java | 4 +-- .../resources/Javascript/ApiClient.mustache | 3 +-- .../main/resources/Javascript/model.mustache | 9 ++++--- .../javascript-promise/src/ApiClient.js | 3 +-- .../javascript-promise/src/model/Category.js | 13 ++++++---- .../javascript-promise/src/model/Order.js | 21 +++++++++------- .../javascript-promise/src/model/Pet.js | 21 +++++++++------- .../javascript-promise/src/model/Tag.js | 13 ++++++---- .../javascript-promise/src/model/User.js | 25 +++++++++++-------- .../petstore/javascript/src/ApiClient.js | 3 +-- .../petstore/javascript/src/model/Category.js | 13 ++++++---- .../petstore/javascript/src/model/Order.js | 21 +++++++++------- .../petstore/javascript/src/model/Pet.js | 21 +++++++++------- .../petstore/javascript/src/model/Tag.js | 13 ++++++---- .../petstore/javascript/src/model/User.js | 25 +++++++++++-------- 15 files changed, 118 insertions(+), 90 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavascriptClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavascriptClientCodegen.java index 3bd7fe4ac741..f921b02cd783 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavascriptClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavascriptClientCodegen.java @@ -339,10 +339,10 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo @Override public String toDefaultValueWithParam(String name, Property p) { + String type = normalizeType(getTypeDeclaration(p)); if (p instanceof RefProperty) { - return ".constructFromObject(data['" + name + "']);"; + return " = " + type + ".constructFromObject(data['" + name + "']);"; } else { - String type = normalizeType(getTypeDeclaration(p)); return " = ApiClient.convertToType(data['" + name + "'], " + type + ");"; } } diff --git a/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache index 38867a6f341b..14239caf455e 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache @@ -262,8 +262,7 @@ default: if (typeof type === 'function') { // for model type like: User - var model = new type(); - model.constructFromObject(data); + var model = type.constructFromObject(data); return model; } else if (Array.isArray(type)) { // for array type like: ['String'] diff --git a/modules/swagger-codegen/src/main/resources/Javascript/model.mustache b/modules/swagger-codegen/src/main/resources/Javascript/model.mustache index fd64156bb520..5592b328cbe7 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/model.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/model.mustache @@ -35,16 +35,17 @@ {{/vars}} }; - {{classname}}.prototype.constructFromObject = function(data) { + {{classname}}.constructFromObject = function(data) { if (!data) { - return this; + return null; } + var _this = new {{classname}}(); {{#vars}} if (data['{{baseName}}']) { - this['{{baseName}}']{{{defaultValueWithParam}}} + _this['{{baseName}}']{{{defaultValueWithParam}}} } {{/vars}} - return this; + return _this; } {{^omitModelMethods}} diff --git a/samples/client/petstore/javascript-promise/src/ApiClient.js b/samples/client/petstore/javascript-promise/src/ApiClient.js index c6843ed75608..353494ea9418 100644 --- a/samples/client/petstore/javascript-promise/src/ApiClient.js +++ b/samples/client/petstore/javascript-promise/src/ApiClient.js @@ -250,8 +250,7 @@ default: if (typeof type === 'function') { // for model type like: User - var model = new type(); - model.constructFromObject(data); + var model = type.constructFromObject(data); return model; } else if (Array.isArray(type)) { // for array type like: ['String'] diff --git a/samples/client/petstore/javascript-promise/src/model/Category.js b/samples/client/petstore/javascript-promise/src/model/Category.js index 26742d7036b1..0e63bbd4297b 100644 --- a/samples/client/petstore/javascript-promise/src/model/Category.js +++ b/samples/client/petstore/javascript-promise/src/model/Category.js @@ -33,23 +33,25 @@ }; - Category.prototype.constructFromObject = function(data) { + Category.constructFromObject = function(data) { if (!data) { - return this; + return null; } + var _this = new Category(); if (data['id']) { - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + _this['id'] = ApiClient.convertToType(data['id'], 'Integer'); } if (data['name']) { - this['name'] = ApiClient.convertToType(data['name'], 'String'); + _this['name'] = ApiClient.convertToType(data['name'], 'String'); } - return this; + return _this; } + /** * @return {Integer} **/ @@ -78,6 +80,7 @@ this['name'] = name; } + Category.prototype.toJson = function() { return JSON.stringify(this); diff --git a/samples/client/petstore/javascript-promise/src/model/Order.js b/samples/client/petstore/javascript-promise/src/model/Order.js index 0d5b90d319ab..0632c1d29834 100644 --- a/samples/client/petstore/javascript-promise/src/model/Order.js +++ b/samples/client/petstore/javascript-promise/src/model/Order.js @@ -82,39 +82,41 @@ var StatusEnum = function StatusEnum() { }; - Order.prototype.constructFromObject = function(data) { + Order.constructFromObject = function(data) { if (!data) { - return this; + return null; } + var _this = new Order(); if (data['id']) { - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + _this['id'] = ApiClient.convertToType(data['id'], 'Integer'); } if (data['petId']) { - this['petId'] = ApiClient.convertToType(data['petId'], 'Integer'); + _this['petId'] = ApiClient.convertToType(data['petId'], 'Integer'); } if (data['quantity']) { - this['quantity'] = ApiClient.convertToType(data['quantity'], 'Integer'); + _this['quantity'] = ApiClient.convertToType(data['quantity'], 'Integer'); } if (data['shipDate']) { - this['shipDate'] = ApiClient.convertToType(data['shipDate'], 'Date'); + _this['shipDate'] = ApiClient.convertToType(data['shipDate'], 'Date'); } if (data['status']) { - this['status'] = ApiClient.convertToType(data['status'], 'String'); + _this['status'] = ApiClient.convertToType(data['status'], 'String'); } if (data['complete']) { - this['complete'] = ApiClient.convertToType(data['complete'], 'Boolean'); + _this['complete'] = ApiClient.convertToType(data['complete'], 'Boolean'); } - return this; + return _this; } + /** * @return {Integer} **/ @@ -201,6 +203,7 @@ var StatusEnum = function StatusEnum() { this['complete'] = complete; } + Order.prototype.toJson = function() { return JSON.stringify(this); diff --git a/samples/client/petstore/javascript-promise/src/model/Pet.js b/samples/client/petstore/javascript-promise/src/model/Pet.js index 67bacb193908..2d1212f5431d 100644 --- a/samples/client/petstore/javascript-promise/src/model/Pet.js +++ b/samples/client/petstore/javascript-promise/src/model/Pet.js @@ -84,39 +84,41 @@ var StatusEnum = function StatusEnum() { }; - Pet.prototype.constructFromObject = function(data) { + Pet.constructFromObject = function(data) { if (!data) { - return this; + return null; } + var _this = new Pet(); if (data['id']) { - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + _this['id'] = ApiClient.convertToType(data['id'], 'Integer'); } if (data['category']) { - this['category'].constructFromObject(data['category']); + _this['category'] = Category.constructFromObject(data['category']); } if (data['name']) { - this['name'] = ApiClient.convertToType(data['name'], 'String'); + _this['name'] = ApiClient.convertToType(data['name'], 'String'); } if (data['photoUrls']) { - this['photoUrls'] = ApiClient.convertToType(data['photoUrls'], ['String']); + _this['photoUrls'] = ApiClient.convertToType(data['photoUrls'], ['String']); } if (data['tags']) { - this['tags'] = ApiClient.convertToType(data['tags'], [Tag]); + _this['tags'] = ApiClient.convertToType(data['tags'], [Tag]); } if (data['status']) { - this['status'] = ApiClient.convertToType(data['status'], 'String'); + _this['status'] = ApiClient.convertToType(data['status'], 'String'); } - return this; + return _this; } + /** * @return {Integer} **/ @@ -203,6 +205,7 @@ var StatusEnum = function StatusEnum() { this['status'] = status; } + Pet.prototype.toJson = function() { return JSON.stringify(this); diff --git a/samples/client/petstore/javascript-promise/src/model/Tag.js b/samples/client/petstore/javascript-promise/src/model/Tag.js index 563c1ee9df85..f24514931038 100644 --- a/samples/client/petstore/javascript-promise/src/model/Tag.js +++ b/samples/client/petstore/javascript-promise/src/model/Tag.js @@ -33,23 +33,25 @@ }; - Tag.prototype.constructFromObject = function(data) { + Tag.constructFromObject = function(data) { if (!data) { - return this; + return null; } + var _this = new Tag(); if (data['id']) { - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + _this['id'] = ApiClient.convertToType(data['id'], 'Integer'); } if (data['name']) { - this['name'] = ApiClient.convertToType(data['name'], 'String'); + _this['name'] = ApiClient.convertToType(data['name'], 'String'); } - return this; + return _this; } + /** * @return {Integer} **/ @@ -78,6 +80,7 @@ this['name'] = name; } + Tag.prototype.toJson = function() { return JSON.stringify(this); diff --git a/samples/client/petstore/javascript-promise/src/model/User.js b/samples/client/petstore/javascript-promise/src/model/User.js index 67c45561d3c7..68dfc609d556 100644 --- a/samples/client/petstore/javascript-promise/src/model/User.js +++ b/samples/client/petstore/javascript-promise/src/model/User.js @@ -64,47 +64,49 @@ }; - User.prototype.constructFromObject = function(data) { + User.constructFromObject = function(data) { if (!data) { - return this; + return null; } + var _this = new User(); if (data['id']) { - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + _this['id'] = ApiClient.convertToType(data['id'], 'Integer'); } if (data['username']) { - this['username'] = ApiClient.convertToType(data['username'], 'String'); + _this['username'] = ApiClient.convertToType(data['username'], 'String'); } if (data['firstName']) { - this['firstName'] = ApiClient.convertToType(data['firstName'], 'String'); + _this['firstName'] = ApiClient.convertToType(data['firstName'], 'String'); } if (data['lastName']) { - this['lastName'] = ApiClient.convertToType(data['lastName'], 'String'); + _this['lastName'] = ApiClient.convertToType(data['lastName'], 'String'); } if (data['email']) { - this['email'] = ApiClient.convertToType(data['email'], 'String'); + _this['email'] = ApiClient.convertToType(data['email'], 'String'); } if (data['password']) { - this['password'] = ApiClient.convertToType(data['password'], 'String'); + _this['password'] = ApiClient.convertToType(data['password'], 'String'); } if (data['phone']) { - this['phone'] = ApiClient.convertToType(data['phone'], 'String'); + _this['phone'] = ApiClient.convertToType(data['phone'], 'String'); } if (data['userStatus']) { - this['userStatus'] = ApiClient.convertToType(data['userStatus'], 'Integer'); + _this['userStatus'] = ApiClient.convertToType(data['userStatus'], 'Integer'); } - return this; + return _this; } + /** * @return {Integer} **/ @@ -219,6 +221,7 @@ this['userStatus'] = userStatus; } + User.prototype.toJson = function() { return JSON.stringify(this); diff --git a/samples/client/petstore/javascript/src/ApiClient.js b/samples/client/petstore/javascript/src/ApiClient.js index a102b4fb0cb3..1fea5d003a3c 100644 --- a/samples/client/petstore/javascript/src/ApiClient.js +++ b/samples/client/petstore/javascript/src/ApiClient.js @@ -250,8 +250,7 @@ default: if (typeof type === 'function') { // for model type like: User - var model = new type(); - model.constructFromObject(data); + var model = type.constructFromObject(data); return model; } else if (Array.isArray(type)) { // for array type like: ['String'] diff --git a/samples/client/petstore/javascript/src/model/Category.js b/samples/client/petstore/javascript/src/model/Category.js index 26742d7036b1..0e63bbd4297b 100644 --- a/samples/client/petstore/javascript/src/model/Category.js +++ b/samples/client/petstore/javascript/src/model/Category.js @@ -33,23 +33,25 @@ }; - Category.prototype.constructFromObject = function(data) { + Category.constructFromObject = function(data) { if (!data) { - return this; + return null; } + var _this = new Category(); if (data['id']) { - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + _this['id'] = ApiClient.convertToType(data['id'], 'Integer'); } if (data['name']) { - this['name'] = ApiClient.convertToType(data['name'], 'String'); + _this['name'] = ApiClient.convertToType(data['name'], 'String'); } - return this; + return _this; } + /** * @return {Integer} **/ @@ -78,6 +80,7 @@ this['name'] = name; } + Category.prototype.toJson = function() { return JSON.stringify(this); diff --git a/samples/client/petstore/javascript/src/model/Order.js b/samples/client/petstore/javascript/src/model/Order.js index 0d5b90d319ab..0632c1d29834 100644 --- a/samples/client/petstore/javascript/src/model/Order.js +++ b/samples/client/petstore/javascript/src/model/Order.js @@ -82,39 +82,41 @@ var StatusEnum = function StatusEnum() { }; - Order.prototype.constructFromObject = function(data) { + Order.constructFromObject = function(data) { if (!data) { - return this; + return null; } + var _this = new Order(); if (data['id']) { - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + _this['id'] = ApiClient.convertToType(data['id'], 'Integer'); } if (data['petId']) { - this['petId'] = ApiClient.convertToType(data['petId'], 'Integer'); + _this['petId'] = ApiClient.convertToType(data['petId'], 'Integer'); } if (data['quantity']) { - this['quantity'] = ApiClient.convertToType(data['quantity'], 'Integer'); + _this['quantity'] = ApiClient.convertToType(data['quantity'], 'Integer'); } if (data['shipDate']) { - this['shipDate'] = ApiClient.convertToType(data['shipDate'], 'Date'); + _this['shipDate'] = ApiClient.convertToType(data['shipDate'], 'Date'); } if (data['status']) { - this['status'] = ApiClient.convertToType(data['status'], 'String'); + _this['status'] = ApiClient.convertToType(data['status'], 'String'); } if (data['complete']) { - this['complete'] = ApiClient.convertToType(data['complete'], 'Boolean'); + _this['complete'] = ApiClient.convertToType(data['complete'], 'Boolean'); } - return this; + return _this; } + /** * @return {Integer} **/ @@ -201,6 +203,7 @@ var StatusEnum = function StatusEnum() { this['complete'] = complete; } + Order.prototype.toJson = function() { return JSON.stringify(this); diff --git a/samples/client/petstore/javascript/src/model/Pet.js b/samples/client/petstore/javascript/src/model/Pet.js index 67bacb193908..2d1212f5431d 100644 --- a/samples/client/petstore/javascript/src/model/Pet.js +++ b/samples/client/petstore/javascript/src/model/Pet.js @@ -84,39 +84,41 @@ var StatusEnum = function StatusEnum() { }; - Pet.prototype.constructFromObject = function(data) { + Pet.constructFromObject = function(data) { if (!data) { - return this; + return null; } + var _this = new Pet(); if (data['id']) { - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + _this['id'] = ApiClient.convertToType(data['id'], 'Integer'); } if (data['category']) { - this['category'].constructFromObject(data['category']); + _this['category'] = Category.constructFromObject(data['category']); } if (data['name']) { - this['name'] = ApiClient.convertToType(data['name'], 'String'); + _this['name'] = ApiClient.convertToType(data['name'], 'String'); } if (data['photoUrls']) { - this['photoUrls'] = ApiClient.convertToType(data['photoUrls'], ['String']); + _this['photoUrls'] = ApiClient.convertToType(data['photoUrls'], ['String']); } if (data['tags']) { - this['tags'] = ApiClient.convertToType(data['tags'], [Tag]); + _this['tags'] = ApiClient.convertToType(data['tags'], [Tag]); } if (data['status']) { - this['status'] = ApiClient.convertToType(data['status'], 'String'); + _this['status'] = ApiClient.convertToType(data['status'], 'String'); } - return this; + return _this; } + /** * @return {Integer} **/ @@ -203,6 +205,7 @@ var StatusEnum = function StatusEnum() { this['status'] = status; } + Pet.prototype.toJson = function() { return JSON.stringify(this); diff --git a/samples/client/petstore/javascript/src/model/Tag.js b/samples/client/petstore/javascript/src/model/Tag.js index 563c1ee9df85..f24514931038 100644 --- a/samples/client/petstore/javascript/src/model/Tag.js +++ b/samples/client/petstore/javascript/src/model/Tag.js @@ -33,23 +33,25 @@ }; - Tag.prototype.constructFromObject = function(data) { + Tag.constructFromObject = function(data) { if (!data) { - return this; + return null; } + var _this = new Tag(); if (data['id']) { - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + _this['id'] = ApiClient.convertToType(data['id'], 'Integer'); } if (data['name']) { - this['name'] = ApiClient.convertToType(data['name'], 'String'); + _this['name'] = ApiClient.convertToType(data['name'], 'String'); } - return this; + return _this; } + /** * @return {Integer} **/ @@ -78,6 +80,7 @@ this['name'] = name; } + Tag.prototype.toJson = function() { return JSON.stringify(this); diff --git a/samples/client/petstore/javascript/src/model/User.js b/samples/client/petstore/javascript/src/model/User.js index 67c45561d3c7..68dfc609d556 100644 --- a/samples/client/petstore/javascript/src/model/User.js +++ b/samples/client/petstore/javascript/src/model/User.js @@ -64,47 +64,49 @@ }; - User.prototype.constructFromObject = function(data) { + User.constructFromObject = function(data) { if (!data) { - return this; + return null; } + var _this = new User(); if (data['id']) { - this['id'] = ApiClient.convertToType(data['id'], 'Integer'); + _this['id'] = ApiClient.convertToType(data['id'], 'Integer'); } if (data['username']) { - this['username'] = ApiClient.convertToType(data['username'], 'String'); + _this['username'] = ApiClient.convertToType(data['username'], 'String'); } if (data['firstName']) { - this['firstName'] = ApiClient.convertToType(data['firstName'], 'String'); + _this['firstName'] = ApiClient.convertToType(data['firstName'], 'String'); } if (data['lastName']) { - this['lastName'] = ApiClient.convertToType(data['lastName'], 'String'); + _this['lastName'] = ApiClient.convertToType(data['lastName'], 'String'); } if (data['email']) { - this['email'] = ApiClient.convertToType(data['email'], 'String'); + _this['email'] = ApiClient.convertToType(data['email'], 'String'); } if (data['password']) { - this['password'] = ApiClient.convertToType(data['password'], 'String'); + _this['password'] = ApiClient.convertToType(data['password'], 'String'); } if (data['phone']) { - this['phone'] = ApiClient.convertToType(data['phone'], 'String'); + _this['phone'] = ApiClient.convertToType(data['phone'], 'String'); } if (data['userStatus']) { - this['userStatus'] = ApiClient.convertToType(data['userStatus'], 'Integer'); + _this['userStatus'] = ApiClient.convertToType(data['userStatus'], 'Integer'); } - return this; + return _this; } + /** * @return {Integer} **/ @@ -219,6 +221,7 @@ this['userStatus'] = userStatus; } + User.prototype.toJson = function() { return JSON.stringify(this);