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 8, 2016
1 parent 12ff5cc commit ebabc40
Show file tree
Hide file tree
Showing 9 changed files with 532 additions and 473 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,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 @@ -65,10 +66,6 @@
}
{{/vars}}

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

if (module) {
module.{{classname}} = {{classname}};
}
Expand Down
29 changes: 29 additions & 0 deletions samples/client/petstore/javascript/src/ApiModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(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';

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

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

return ApiModel;

}));
91 changes: 44 additions & 47 deletions samples/client/petstore/javascript/src/model/Category.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,81 @@
(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';





var Category = function Category() {
var self = this;

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

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

};

self.constructFromObject = function(data) {
if (!data) {
return this;
}

self['id'] = ApiClient.convertToType(data['id'], 'Integer');

self['name'] = ApiClient.convertToType(data['name'], 'String');

Category.prototype.constructFromObject = function(data) {
if (!data) {
return this;
}


/**
* @return {Integer}
**/
self.getId = function() {
return self['id'];
}

/**
* @param {Integer} id
**/
self.setId = function(id) {
self['id'] = id;
}
this['id'] = ApiClient.convertToType(data['id'], 'Integer');

/**
* @return {String}
**/
self.getName = function() {
return self['name'];
}

/**
* @param {String} name
**/
self.setName = function(name) {
self['name'] = name;
}
this['name'] = ApiClient.convertToType(data['name'], 'String');

return this;
}

self.toJson = function() {
return JSON.stringify(self);
}
};

/**
* @return {Integer}
**/
Category.prototype.getId = function() {
return this['id'];
}

/**
* @param {Integer} id
**/
Category.prototype.setId = function(id) {
this['id'] = id;
}

/**
* @return {String}
**/
Category.prototype.getName = function() {
return this['name'];
}

/**
* @param {String} name
**/
Category.prototype.setName = function(name) {
this['name'] = name;
}


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

0 comments on commit ebabc40

Please sign in to comment.