Skip to content

Use static model factory methods #2114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,10 @@ public String toDefaultValue(Property p) {

@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 + ");";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
3 changes: 1 addition & 2 deletions samples/client/petstore/javascript-promise/src/ApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
13 changes: 8 additions & 5 deletions samples/client/petstore/javascript-promise/src/model/Category.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
**/
Expand Down Expand Up @@ -78,6 +80,7 @@
this['name'] = name;
}



Category.prototype.toJson = function() {
return JSON.stringify(this);
Expand Down
21 changes: 12 additions & 9 deletions samples/client/petstore/javascript-promise/src/model/Order.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
**/
Expand Down Expand Up @@ -201,6 +203,7 @@ var StatusEnum = function StatusEnum() {
this['complete'] = complete;
}



Order.prototype.toJson = function() {
return JSON.stringify(this);
Expand Down
21 changes: 12 additions & 9 deletions samples/client/petstore/javascript-promise/src/model/Pet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
**/
Expand Down Expand Up @@ -203,6 +205,7 @@ var StatusEnum = function StatusEnum() {
this['status'] = status;
}



Pet.prototype.toJson = function() {
return JSON.stringify(this);
Expand Down
13 changes: 8 additions & 5 deletions samples/client/petstore/javascript-promise/src/model/Tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
**/
Expand Down Expand Up @@ -78,6 +80,7 @@
this['name'] = name;
}



Tag.prototype.toJson = function() {
return JSON.stringify(this);
Expand Down
25 changes: 14 additions & 11 deletions samples/client/petstore/javascript-promise/src/model/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
**/
Expand Down Expand Up @@ -219,6 +221,7 @@
this['userStatus'] = userStatus;
}



User.prototype.toJson = function() {
return JSON.stringify(this);
Expand Down
3 changes: 1 addition & 2 deletions samples/client/petstore/javascript/src/ApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
13 changes: 8 additions & 5 deletions samples/client/petstore/javascript/src/model/Category.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
**/
Expand Down Expand Up @@ -78,6 +80,7 @@
this['name'] = name;
}



Category.prototype.toJson = function() {
return JSON.stringify(this);
Expand Down
Loading