Skip to content

Commit

Permalink
Move common functionality to new model base class
Browse files Browse the repository at this point in the history
Fixes "additional comment swagger-api#2" in swagger-api#2044.

Saves memory by not repeating the `toJson` method in every model
class.

The new `ApiModel` base class may also be useful for other purposes
in the future.
  • Loading branch information
delenius committed Feb 12, 2016
1 parent 847a8e1 commit 2607f49
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ public void preprocessSwagger(Swagger swagger) {
supportingFiles.add(new SupportingFile("package.mustache", "", "package.json"));
supportingFiles.add(new SupportingFile("index.mustache", sourceFolder, "index.js"));
supportingFiles.add(new SupportingFile("ApiClient.mustache", sourceFolder, "ApiClient.js"));
supportingFiles.add(new SupportingFile("ApiModel.mustache", sourceFolder, "ApiModel.js"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
if (!root.{{moduleName}}) {
root.{{moduleName}} = {};
}
root.{{moduleName}}.ApiModel = factory();
}
}(this, function() {
'use strict';
/**
Base class for all generated model classes,
containing common functionality.
**/
var ApiModel = function(){}
ApiModel.prototype.toJson = function() {
return JSON.stringify(this);
}

if (module) {
module.ApiModel = ApiModel;
}

return ApiModel;

}));
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([undefined, '../ApiClient'{{#imports}}, './{{import}}'{{/imports}}], factory);
define([undefined, '../ApiClient', '../ApiModel'{{#imports}}, './{{import}}'{{/imports}}], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(undefined, require('../ApiClient'){{#imports}}, require('./{{import}}'){{/imports}});
module.exports = factory(undefined, require('../ApiClient'), require('../ApiModel'){{#imports}}, require('./{{import}}'){{/imports}});
} else {
// Browser globals (root is window)
if (!root.{{moduleName}}) {
root.{{moduleName}} = {};
}
factory(root.{{moduleName}}, root.{{moduleName}}.ApiClient{{#imports}}, root.{{moduleName}}.{{import}}{{/imports}});
factory(root.{{moduleName}}, root.{{moduleName}}.ApiClient, root.{{moduleName}}.ApiModel{{#imports}}, root.{{moduleName}}.{{import}}{{/imports}});
}
}(this, function(module, ApiClient{{#imports}}, {{import}}{{/imports}}) {
}(this, function(module, ApiClient, ApiModel{{#imports}}, {{import}}{{/imports}}) {
'use strict';
{{#models}}{{#model}}
Expand All @@ -31,6 +31,7 @@
* minimum: {{minimum}}{{/minimum}}{{#maximum}}
* maximum: {{maximum}}{{/maximum}}
**/
ApiModel.call(this);
this['{{baseName}}'] = {{#required}}{{name}}{{/required}}{{^required}}{{{defaultValue}}}{{/required}};
{{/vars}}
};
Expand Down Expand Up @@ -69,10 +70,6 @@
{{/vars}}
{{/omitModelMethods}}

{{classname}}.prototype.toJson = function() {
return JSON.stringify(this);
}

if (module) {
module.{{classname}} = {{classname}};
}
Expand Down
33 changes: 33 additions & 0 deletions samples/client/petstore/javascript-promise/src/ApiModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
if (!root.SwaggerPetstore) {
root.SwaggerPetstore = {};
}
root.SwaggerPetstore.ApiModel = factory();
}
}(this, function() {
'use strict';

/**
Base class for all generated model classes,
containing common functionality.
**/
var ApiModel = function(){}
ApiModel.prototype.toJson = function() {
return JSON.stringify(this);
}

if (module) {
module.ApiModel = ApiModel;
}

return ApiModel;

}));
16 changes: 8 additions & 8 deletions samples/client/petstore/javascript-promise/src/model/Category.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([undefined, '../ApiClient'], factory);
define([undefined, '../ApiClient', '../ApiModel'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(undefined, require('../ApiClient'));
module.exports = factory(undefined, require('../ApiClient'), require('../ApiModel'));
} else {
// Browser globals (root is window)
if (!root.SwaggerPetstore) {
root.SwaggerPetstore = {};
}
factory(root.SwaggerPetstore, root.SwaggerPetstore.ApiClient);
factory(root.SwaggerPetstore, root.SwaggerPetstore.ApiClient, root.SwaggerPetstore.ApiModel);
}
}(this, function(module, ApiClient) {
}(this, function(module, ApiClient, ApiModel) {
'use strict';


Expand All @@ -24,11 +24,13 @@
/**
* datatype: Integer
**/
ApiModel.call(this);
this['id'] = null;

/**
* datatype: String
**/
ApiModel.call(this);
this['name'] = null;

};
Expand All @@ -50,6 +52,7 @@
}



/**
* @return {Integer}
**/
Expand Down Expand Up @@ -78,10 +81,7 @@
this['name'] = name;
}


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


if (module) {
module.Category = Category;
Expand Down
20 changes: 12 additions & 8 deletions samples/client/petstore/javascript-promise/src/model/Order.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([undefined, '../ApiClient'], factory);
define([undefined, '../ApiClient', '../ApiModel'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(undefined, require('../ApiClient'));
module.exports = factory(undefined, require('../ApiClient'), require('../ApiModel'));
} else {
// Browser globals (root is window)
if (!root.SwaggerPetstore) {
root.SwaggerPetstore = {};
}
factory(root.SwaggerPetstore, root.SwaggerPetstore.ApiClient);
factory(root.SwaggerPetstore, root.SwaggerPetstore.ApiClient, root.SwaggerPetstore.ApiModel);
}
}(this, function(module, ApiClient) {
}(this, function(module, ApiClient, ApiModel) {
'use strict';


Expand Down Expand Up @@ -52,32 +52,38 @@ var StatusEnum = function StatusEnum() {
/**
* datatype: Integer
**/
ApiModel.call(this);
this['id'] = null;

/**
* datatype: Integer
**/
ApiModel.call(this);
this['petId'] = null;

/**
* datatype: Integer
**/
ApiModel.call(this);
this['quantity'] = null;

/**
* datatype: Date
**/
ApiModel.call(this);
this['shipDate'] = null;

/**
* Order Status
* datatype: StatusEnum
**/
ApiModel.call(this);
this['status'] = null;

/**
* datatype: Boolean
**/
ApiModel.call(this);
this['complete'] = null;

};
Expand Down Expand Up @@ -115,6 +121,7 @@ var StatusEnum = function StatusEnum() {
}



/**
* @return {Integer}
**/
Expand Down Expand Up @@ -201,10 +208,7 @@ var StatusEnum = function StatusEnum() {
this['complete'] = complete;
}


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


if (module) {
module.Order = Order;
Expand Down
20 changes: 12 additions & 8 deletions samples/client/petstore/javascript-promise/src/model/Pet.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([undefined, '../ApiClient', './Category', './Tag'], factory);
define([undefined, '../ApiClient', '../ApiModel', './Category', './Tag'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(undefined, require('../ApiClient'), require('./Category'), require('./Tag'));
module.exports = factory(undefined, require('../ApiClient'), require('../ApiModel'), require('./Category'), require('./Tag'));
} else {
// Browser globals (root is window)
if (!root.SwaggerPetstore) {
root.SwaggerPetstore = {};
}
factory(root.SwaggerPetstore, root.SwaggerPetstore.ApiClient, root.SwaggerPetstore.Category, root.SwaggerPetstore.Tag);
factory(root.SwaggerPetstore, root.SwaggerPetstore.ApiClient, root.SwaggerPetstore.ApiModel, root.SwaggerPetstore.Category, root.SwaggerPetstore.Tag);
}
}(this, function(module, ApiClient, Category, Tag) {
}(this, function(module, ApiClient, ApiModel, Category, Tag) {
'use strict';


Expand Down Expand Up @@ -52,34 +52,40 @@ var StatusEnum = function StatusEnum() {
/**
* datatype: Integer
**/
ApiModel.call(this);
this['id'] = null;

/**
* datatype: Category
**/
ApiModel.call(this);
this['category'] = new Category();

/**
* datatype: String
* required
**/
ApiModel.call(this);
this['name'] = name;

/**
* datatype: [String]
* required
**/
ApiModel.call(this);
this['photoUrls'] = photoUrls;

/**
* datatype: [Tag]
**/
ApiModel.call(this);
this['tags'] = [];

/**
* pet status in the store
* datatype: StatusEnum
**/
ApiModel.call(this);
this['status'] = null;

};
Expand Down Expand Up @@ -117,6 +123,7 @@ var StatusEnum = function StatusEnum() {
}



/**
* @return {Integer}
**/
Expand Down Expand Up @@ -203,10 +210,7 @@ var StatusEnum = function StatusEnum() {
this['status'] = status;
}


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


if (module) {
module.Pet = Pet;
Expand Down
16 changes: 8 additions & 8 deletions samples/client/petstore/javascript-promise/src/model/Tag.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([undefined, '../ApiClient'], factory);
define([undefined, '../ApiClient', '../ApiModel'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(undefined, require('../ApiClient'));
module.exports = factory(undefined, require('../ApiClient'), require('../ApiModel'));
} else {
// Browser globals (root is window)
if (!root.SwaggerPetstore) {
root.SwaggerPetstore = {};
}
factory(root.SwaggerPetstore, root.SwaggerPetstore.ApiClient);
factory(root.SwaggerPetstore, root.SwaggerPetstore.ApiClient, root.SwaggerPetstore.ApiModel);
}
}(this, function(module, ApiClient) {
}(this, function(module, ApiClient, ApiModel) {
'use strict';


Expand All @@ -24,11 +24,13 @@
/**
* datatype: Integer
**/
ApiModel.call(this);
this['id'] = null;

/**
* datatype: String
**/
ApiModel.call(this);
this['name'] = null;

};
Expand All @@ -50,6 +52,7 @@
}



/**
* @return {Integer}
**/
Expand Down Expand Up @@ -78,10 +81,7 @@
this['name'] = name;
}


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


if (module) {
module.Tag = Tag;
Expand Down
Loading

0 comments on commit 2607f49

Please sign in to comment.