Skip to content

Commit a07c078

Browse files
committed
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.
1 parent b54947d commit a07c078

File tree

15 files changed

+118
-90
lines changed

15 files changed

+118
-90
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavascriptClientCodegen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,10 @@ public String toDefaultValue(Property p) {
339339

340340
@Override
341341
public String toDefaultValueWithParam(String name, Property p) {
342+
String type = normalizeType(getTypeDeclaration(p));
342343
if (p instanceof RefProperty) {
343-
return ".constructFromObject(data['" + name + "']);";
344+
return " = " + type + ".constructFromObject(data['" + name + "']);";
344345
} else {
345-
String type = normalizeType(getTypeDeclaration(p));
346346
return " = ApiClient.convertToType(data['" + name + "'], " + type + ");";
347347
}
348348
}

modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,7 @@
262262
default:
263263
if (typeof type === 'function') {
264264
// for model type like: User
265-
var model = new type();
266-
model.constructFromObject(data);
265+
var model = type.constructFromObject(data);
267266
return model;
268267
} else if (Array.isArray(type)) {
269268
// for array type like: ['String']

modules/swagger-codegen/src/main/resources/Javascript/model.mustache

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@
3535
{{/vars}}
3636
};
3737

38-
{{classname}}.prototype.constructFromObject = function(data) {
38+
{{classname}}.constructFromObject = function(data) {
3939
if (!data) {
40-
return this;
40+
return null;
4141
}
42+
var _this = new {{classname}}();
4243
{{#vars}}
4344
if (data['{{baseName}}']) {
44-
this['{{baseName}}']{{{defaultValueWithParam}}}
45+
_this['{{baseName}}']{{{defaultValueWithParam}}}
4546
}
4647
{{/vars}}
47-
return this;
48+
return _this;
4849
}
4950

5051
{{^omitModelMethods}}

samples/client/petstore/javascript-promise/src/ApiClient.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@
250250
default:
251251
if (typeof type === 'function') {
252252
// for model type like: User
253-
var model = new type();
254-
model.constructFromObject(data);
253+
var model = type.constructFromObject(data);
255254
return model;
256255
} else if (Array.isArray(type)) {
257256
// for array type like: ['String']

samples/client/petstore/javascript-promise/src/model/Category.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,25 @@
3333

3434
};
3535

36-
Category.prototype.constructFromObject = function(data) {
36+
Category.constructFromObject = function(data) {
3737
if (!data) {
38-
return this;
38+
return null;
3939
}
40+
var _this = new Category();
4041

4142
if (data['id']) {
42-
this['id'] = ApiClient.convertToType(data['id'], 'Integer');
43+
_this['id'] = ApiClient.convertToType(data['id'], 'Integer');
4344
}
4445

4546
if (data['name']) {
46-
this['name'] = ApiClient.convertToType(data['name'], 'String');
47+
_this['name'] = ApiClient.convertToType(data['name'], 'String');
4748
}
4849

49-
return this;
50+
return _this;
5051
}
5152

5253

54+
5355
/**
5456
* @return {Integer}
5557
**/
@@ -78,6 +80,7 @@
7880
this['name'] = name;
7981
}
8082

83+
8184

8285
Category.prototype.toJson = function() {
8386
return JSON.stringify(this);

samples/client/petstore/javascript-promise/src/model/Order.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,39 +82,41 @@ var StatusEnum = function StatusEnum() {
8282

8383
};
8484

85-
Order.prototype.constructFromObject = function(data) {
85+
Order.constructFromObject = function(data) {
8686
if (!data) {
87-
return this;
87+
return null;
8888
}
89+
var _this = new Order();
8990

9091
if (data['id']) {
91-
this['id'] = ApiClient.convertToType(data['id'], 'Integer');
92+
_this['id'] = ApiClient.convertToType(data['id'], 'Integer');
9293
}
9394

9495
if (data['petId']) {
95-
this['petId'] = ApiClient.convertToType(data['petId'], 'Integer');
96+
_this['petId'] = ApiClient.convertToType(data['petId'], 'Integer');
9697
}
9798

9899
if (data['quantity']) {
99-
this['quantity'] = ApiClient.convertToType(data['quantity'], 'Integer');
100+
_this['quantity'] = ApiClient.convertToType(data['quantity'], 'Integer');
100101
}
101102

102103
if (data['shipDate']) {
103-
this['shipDate'] = ApiClient.convertToType(data['shipDate'], 'Date');
104+
_this['shipDate'] = ApiClient.convertToType(data['shipDate'], 'Date');
104105
}
105106

106107
if (data['status']) {
107-
this['status'] = ApiClient.convertToType(data['status'], 'String');
108+
_this['status'] = ApiClient.convertToType(data['status'], 'String');
108109
}
109110

110111
if (data['complete']) {
111-
this['complete'] = ApiClient.convertToType(data['complete'], 'Boolean');
112+
_this['complete'] = ApiClient.convertToType(data['complete'], 'Boolean');
112113
}
113114

114-
return this;
115+
return _this;
115116
}
116117

117118

119+
118120
/**
119121
* @return {Integer}
120122
**/
@@ -201,6 +203,7 @@ var StatusEnum = function StatusEnum() {
201203
this['complete'] = complete;
202204
}
203205

206+
204207

205208
Order.prototype.toJson = function() {
206209
return JSON.stringify(this);

samples/client/petstore/javascript-promise/src/model/Pet.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,39 +84,41 @@ var StatusEnum = function StatusEnum() {
8484

8585
};
8686

87-
Pet.prototype.constructFromObject = function(data) {
87+
Pet.constructFromObject = function(data) {
8888
if (!data) {
89-
return this;
89+
return null;
9090
}
91+
var _this = new Pet();
9192

9293
if (data['id']) {
93-
this['id'] = ApiClient.convertToType(data['id'], 'Integer');
94+
_this['id'] = ApiClient.convertToType(data['id'], 'Integer');
9495
}
9596

9697
if (data['category']) {
97-
this['category'].constructFromObject(data['category']);
98+
_this['category'] = Category.constructFromObject(data['category']);
9899
}
99100

100101
if (data['name']) {
101-
this['name'] = ApiClient.convertToType(data['name'], 'String');
102+
_this['name'] = ApiClient.convertToType(data['name'], 'String');
102103
}
103104

104105
if (data['photoUrls']) {
105-
this['photoUrls'] = ApiClient.convertToType(data['photoUrls'], ['String']);
106+
_this['photoUrls'] = ApiClient.convertToType(data['photoUrls'], ['String']);
106107
}
107108

108109
if (data['tags']) {
109-
this['tags'] = ApiClient.convertToType(data['tags'], [Tag]);
110+
_this['tags'] = ApiClient.convertToType(data['tags'], [Tag]);
110111
}
111112

112113
if (data['status']) {
113-
this['status'] = ApiClient.convertToType(data['status'], 'String');
114+
_this['status'] = ApiClient.convertToType(data['status'], 'String');
114115
}
115116

116-
return this;
117+
return _this;
117118
}
118119

119120

121+
120122
/**
121123
* @return {Integer}
122124
**/
@@ -203,6 +205,7 @@ var StatusEnum = function StatusEnum() {
203205
this['status'] = status;
204206
}
205207

208+
206209

207210
Pet.prototype.toJson = function() {
208211
return JSON.stringify(this);

samples/client/petstore/javascript-promise/src/model/Tag.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,25 @@
3333

3434
};
3535

36-
Tag.prototype.constructFromObject = function(data) {
36+
Tag.constructFromObject = function(data) {
3737
if (!data) {
38-
return this;
38+
return null;
3939
}
40+
var _this = new Tag();
4041

4142
if (data['id']) {
42-
this['id'] = ApiClient.convertToType(data['id'], 'Integer');
43+
_this['id'] = ApiClient.convertToType(data['id'], 'Integer');
4344
}
4445

4546
if (data['name']) {
46-
this['name'] = ApiClient.convertToType(data['name'], 'String');
47+
_this['name'] = ApiClient.convertToType(data['name'], 'String');
4748
}
4849

49-
return this;
50+
return _this;
5051
}
5152

5253

54+
5355
/**
5456
* @return {Integer}
5557
**/
@@ -78,6 +80,7 @@
7880
this['name'] = name;
7981
}
8082

83+
8184

8285
Tag.prototype.toJson = function() {
8386
return JSON.stringify(this);

samples/client/petstore/javascript-promise/src/model/User.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,47 +64,49 @@
6464

6565
};
6666

67-
User.prototype.constructFromObject = function(data) {
67+
User.constructFromObject = function(data) {
6868
if (!data) {
69-
return this;
69+
return null;
7070
}
71+
var _this = new User();
7172

7273
if (data['id']) {
73-
this['id'] = ApiClient.convertToType(data['id'], 'Integer');
74+
_this['id'] = ApiClient.convertToType(data['id'], 'Integer');
7475
}
7576

7677
if (data['username']) {
77-
this['username'] = ApiClient.convertToType(data['username'], 'String');
78+
_this['username'] = ApiClient.convertToType(data['username'], 'String');
7879
}
7980

8081
if (data['firstName']) {
81-
this['firstName'] = ApiClient.convertToType(data['firstName'], 'String');
82+
_this['firstName'] = ApiClient.convertToType(data['firstName'], 'String');
8283
}
8384

8485
if (data['lastName']) {
85-
this['lastName'] = ApiClient.convertToType(data['lastName'], 'String');
86+
_this['lastName'] = ApiClient.convertToType(data['lastName'], 'String');
8687
}
8788

8889
if (data['email']) {
89-
this['email'] = ApiClient.convertToType(data['email'], 'String');
90+
_this['email'] = ApiClient.convertToType(data['email'], 'String');
9091
}
9192

9293
if (data['password']) {
93-
this['password'] = ApiClient.convertToType(data['password'], 'String');
94+
_this['password'] = ApiClient.convertToType(data['password'], 'String');
9495
}
9596

9697
if (data['phone']) {
97-
this['phone'] = ApiClient.convertToType(data['phone'], 'String');
98+
_this['phone'] = ApiClient.convertToType(data['phone'], 'String');
9899
}
99100

100101
if (data['userStatus']) {
101-
this['userStatus'] = ApiClient.convertToType(data['userStatus'], 'Integer');
102+
_this['userStatus'] = ApiClient.convertToType(data['userStatus'], 'Integer');
102103
}
103104

104-
return this;
105+
return _this;
105106
}
106107

107108

109+
108110
/**
109111
* @return {Integer}
110112
**/
@@ -219,6 +221,7 @@
219221
this['userStatus'] = userStatus;
220222
}
221223

224+
222225

223226
User.prototype.toJson = function() {
224227
return JSON.stringify(this);

samples/client/petstore/javascript/src/ApiClient.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@
250250
default:
251251
if (typeof type === 'function') {
252252
// for model type like: User
253-
var model = new type();
254-
model.constructFromObject(data);
253+
var model = type.constructFromObject(data);
255254
return model;
256255
} else if (Array.isArray(type)) {
257256
// for array type like: ['String']

samples/client/petstore/javascript/src/model/Category.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,25 @@
3333

3434
};
3535

36-
Category.prototype.constructFromObject = function(data) {
36+
Category.constructFromObject = function(data) {
3737
if (!data) {
38-
return this;
38+
return null;
3939
}
40+
var _this = new Category();
4041

4142
if (data['id']) {
42-
this['id'] = ApiClient.convertToType(data['id'], 'Integer');
43+
_this['id'] = ApiClient.convertToType(data['id'], 'Integer');
4344
}
4445

4546
if (data['name']) {
46-
this['name'] = ApiClient.convertToType(data['name'], 'String');
47+
_this['name'] = ApiClient.convertToType(data['name'], 'String');
4748
}
4849

49-
return this;
50+
return _this;
5051
}
5152

5253

54+
5355
/**
5456
* @return {Integer}
5557
**/
@@ -78,6 +80,7 @@
7880
this['name'] = name;
7981
}
8082

83+
8184

8285
Category.prototype.toJson = function() {
8386
return JSON.stringify(this);

0 commit comments

Comments
 (0)