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 f3d61872c089..2a60c7fc1f0c 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 @@ -164,11 +164,11 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo if (swagger.getInfo() != null) { Info info = swagger.getInfo(); - if (projectName == null && info.getTitle() != null) { + if (StringUtils.isBlank(projectName) && info.getTitle() != null) { // when projectName is not specified, generate it from info.title projectName = dashize(info.getTitle()); } - if (projectVersion == null) { + if (StringUtils.isBlank(projectVersion)) { // when projectVersion is not specified, use info.version projectVersion = info.getVersion(); } @@ -185,13 +185,13 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo } // default values - if (projectName == null) { + if (StringUtils.isBlank(projectName)) { projectName = "swagger-js-client"; } - if (moduleName == null) { + if (StringUtils.isBlank(moduleName)) { moduleName = camelize(underscore(projectName)); } - if (projectVersion == null) { + if (StringUtils.isBlank(projectVersion)) { projectVersion = "1.0.0"; } if (projectDescription == null) { @@ -290,14 +290,13 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo @Override public String getTypeDeclaration(Property p) { if (p instanceof ArrayProperty) { - // ArrayProperty ap = (ArrayProperty) p; - // Property inner = ap.getItems(); - return getSwaggerType(p); // TODO: + "/* <" + getTypeDeclaration(inner) + "> */"; + ArrayProperty ap = (ArrayProperty) p; + Property inner = ap.getItems(); + return "[" + getTypeDeclaration(inner) + "]"; } else if (p instanceof MapProperty) { MapProperty mp = (MapProperty) p; Property inner = mp.getAdditionalProperties(); - - return getSwaggerType(p) + ""; + return "{String: " + getTypeDeclaration(inner) + "}"; } return super.getTypeDeclaration(p); } @@ -318,10 +317,18 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo @Override public String toDefaultValueWithParam(String name, Property p) { if (p instanceof RefProperty) { - return ".constructFromObject(data." + name + ");"; + return ".constructFromObject(data['" + name + "']);"; + } else { + String type = normalizeType(getTypeDeclaration(p)); + return " = ApiClient.convertToType(data['" + name + "'], " + type + ");"; } + } - return super.toDefaultValueWithParam(name, p); + /** + * Normalize type by wrapping primitive types with single quotes. + */ + public String normalizeType(String type) { + return type.replaceAll("\\b(Boolean|Integer|Number|String|Date)\\b", "'$1'"); } @Override @@ -357,6 +364,15 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo return camelize(sanitizeName(operationId), true); } + @Override + public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map definitions, Swagger swagger) { + CodegenOperation op = super.fromOperation(path, httpMethod, operation, definitions, swagger); + if (op.returnType != null) { + op.returnType = normalizeType(op.returnType); + } + return op; + } + @Override public CodegenModel fromModel(String name, Model model, Map allDefinitions) { CodegenModel codegenModel = super.fromModel(name, model, allDefinitions); @@ -417,11 +433,6 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo return objs; } - @Override - public Map postProcessOperations(Map objs) { - return objs; - } - @Override protected boolean needToImport(String type) { return !defaultIncludes.contains(type) diff --git a/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache index 006d8d50eb76..222ade010a76 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache @@ -88,9 +88,15 @@ * Check if the given parameter value is like file content. */ ApiClient.prototype.isFileParam = function isFileParam(param) { - // Buffer or fs.ReadStream in Node.js - if (typeof module === 'object' && module.exports && - (param instanceof Buffer || param instanceof require('fs').ReadStream)) { + // fs.ReadStream in Node.js (but not in runtime like browserify) + if (typeof window === 'undefined' && + typeof require === 'function' && + require('fs') && + param instanceof require('fs').ReadStream) { + return true; + } + // Buffer in Node.js + if (typeof Buffer === 'function' && param instanceof Buffer) { return true; } // Blob in browser @@ -125,9 +131,23 @@ return newParams; }; + ApiClient.prototype.deserialize = function deserialize(response, returnType) { + if (response == null || returnType == null) { + return null; + } + // Rely on Superagent for parsing response body. + // See http://visionmedia.github.io/superagent/#parsing-response-bodies + var data = response.body; + if (data == null) { + return null; + } + return ApiClient.convertToType(data, returnType); + }; + ApiClient.prototype.callApi = function callApi(path, httpMethod, pathParams, queryParams, headerParams, formParams, bodyParam, contentTypes, accepts, - callback) { + returnType, callback) { + var _this = this; var url = this.buildUrl(path, pathParams); var request = superagent(httpMethod, url); @@ -169,7 +189,10 @@ request.end(function(error, response) { if (callback) { - var data = response && response.body; + var data = null; + if (!error) { + data = _this.deserialize(response, returnType); + } callback(error, data, response); } }); @@ -177,6 +200,61 @@ return request; }; + ApiClient.parseDate = function parseDate(str) { + str = str.replace(/T/i, ' '); + return new Date(str); + }; + + ApiClient.convertToType = function convertToType(data, type) { + switch (type) { + case 'Boolean': + return Boolean(data); + case 'Integer': + return parseInt(data, 10); + case 'Number': + return parseFloat(data); + case 'String': + return String(data); + case 'Date': + return this.parseDate(String(data)); + default: + if (typeof type === 'function') { + // for model type like: User + var model = new type(); + model.constructFromObject(data); + return model; + } else if (Array.isArray(type)) { + // for array type like: ['String'] + var itemType = type[0]; + return data.map(function(item) { + return ApiClient.convertToType(item, itemType); + }); + } else if (typeof type === 'object') { + // for plain object type like: {'String': 'Integer'} + var keyType, valueType; + for (var k in type) { + if (type.hasOwnProperty(k)) { + keyType = k; + valueType = type[k]; + break; + } + } + var result = {}; + for (var k in data) { + if (data.hasOwnProperty(k)) { + var key = ApiClient.convertToType(k, keyType); + var value = ApiClient.convertToType(data[k], valueType); + result[key] = value; + } + } + return result; + } else { + // for unknown type, return the data directly + return data; + } + } + }; + ApiClient.default = new ApiClient(); return ApiClient; diff --git a/modules/swagger-codegen/src/main/resources/Javascript/api.mustache b/modules/swagger-codegen/src/main/resources/Javascript/api.mustache index a0ac34c48dde..f60c421eda7a 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/api.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/api.mustache @@ -53,28 +53,12 @@ var contentTypes = [<#consumes>''<#hasMore>, ]; var accepts = [<#produces>''<#hasMore>, ]; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) {<#returnType><#returnTypeIsPrimitive> - callback(error, data, response);<^returnTypeIsPrimitive><#isListContainer> - // TODO: support deserializing array of models - callback(error, data, response);<^isListContainer> - if (!error && data) { - var result = new <&returnType>(); - result.constructFromObject(data); - callback(error, result, response); - } else { - callback(error, data, response); - }<^returnType> - callback(error, data, response); - }; - } + var returnType = <#returnType><&returnType><^returnType>null; return this.apiClient.callApi( '<&path>', '', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); <={{ }}=> } diff --git a/modules/swagger-codegen/src/main/resources/Javascript/enumClass.mustache b/modules/swagger-codegen/src/main/resources/Javascript/enumClass.mustache index 648708c2d7ed..aa7d8bf50fd3 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/enumClass.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/enumClass.mustache @@ -1,18 +1,18 @@ //export module -if ( typeof define === "function" && define.amd ) { - define('{{datatypeWithEnum}}', ['jquery'], function($) { +if ( typeof define === "function" && define.amd ) { + define('{{datatypeWithEnum}}', [], function() { return {{datatypeWithEnum}}; }); } var {{datatypeWithEnum}} = function {{datatypeWithEnum}}() { var self = this; - + {{#allowableValues}}{{#enumVars}} /** - * @const - */ + * @const + */ self.{{name}} = "{{value}}"{{^-last}}, {{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}} diff --git a/modules/swagger-codegen/src/main/resources/Javascript/model.mustache b/modules/swagger-codegen/src/main/resources/Javascript/model.mustache index a5778003c94b..ee30a99bb024 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/model.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/model.mustache @@ -1,18 +1,18 @@ (function(root, factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. - define([undefined{{#imports}}, './{{import}}'{{/imports}}], factory); + define([undefined, '../ApiClient'{{#imports}}, './{{import}}'{{/imports}}], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(undefined{{#imports}}, require('./{{import}}'){{/imports}}); + module.exports = factory(undefined, require('../ApiClient'){{#imports}}, require('./{{import}}'){{/imports}}); } else { // Browser globals (root is window) if (!root.{{moduleName}}) { root.{{moduleName}} = {}; } - factory(root.{{moduleName}}{{#imports}}, root.{{moduleName}}.{{import}}{{/imports}}); + factory(root.{{moduleName}}, root.{{moduleName}}.ApiClient{{#imports}}, root.{{moduleName}}.{{import}}{{/imports}}); } -}(this, function(module{{#imports}}, {{import}}{{/imports}}) { +}(this, function(module, ApiClient{{#imports}}, {{import}}{{/imports}}) { 'use strict'; {{#models}}{{#model}} @@ -32,16 +32,17 @@ * minimum: {{minimum}}{{/minimum}}{{#maximum}} * maximum: {{maximum}}{{/maximum}} **/ - self.{{name}} = {{#required}}{{name}}{{/required}}{{^required}}{{{defaultValue}}}{{/required}}; + self['{{baseName}}'] = {{#required}}{{name}}{{/required}}{{^required}}{{{defaultValue}}}{{/required}}; {{/vars}} self.constructFromObject = function(data) { if (!data) { - return; + return this; } {{#vars}} - self.{{name}}{{{defaultValueWithParam}}} + self['{{baseName}}']{{{defaultValueWithParam}}} {{/vars}} + return this; } {{#vars}} @@ -52,19 +53,19 @@ * @return {{=<% %>=}}{<% datatypeWithEnum %>}<%={{ }}=%> **/ self.{{getter}} = function() { - return self.{{name}}; + return self['{{baseName}}']; } /**{{#description}} * set {{{description}}}{{/description}} * @param {{=<% %>=}}{<% datatypeWithEnum %>}<%={{ }}=%> {{name}} **/ - self.{{setter}} = function ({{name}}) { - self.{{name}} = {{name}}; + self.{{setter}} = function({{name}}) { + self['{{baseName}}'] = {{name}}; } {{/vars}} - self.toJson = function () { + self.toJson = function() { return JSON.stringify(self); } }; diff --git a/samples/client/petstore/javascript/src/ApiClient.js b/samples/client/petstore/javascript/src/ApiClient.js index 7fdc0ca81dc0..17b271bc5ecc 100644 --- a/samples/client/petstore/javascript/src/ApiClient.js +++ b/samples/client/petstore/javascript/src/ApiClient.js @@ -88,9 +88,15 @@ * Check if the given parameter value is like file content. */ ApiClient.prototype.isFileParam = function isFileParam(param) { - // Buffer or fs.ReadStream in Node.js - if (typeof module === 'object' && module.exports && - (param instanceof Buffer || param instanceof require('fs').ReadStream)) { + // fs.ReadStream in Node.js (but not in runtime like browserify) + if (typeof window === 'undefined' && + typeof require === 'function' && + require('fs') && + param instanceof require('fs').ReadStream) { + return true; + } + // Buffer in Node.js + if (typeof Buffer === 'function' && param instanceof Buffer) { return true; } // Blob in browser @@ -125,9 +131,23 @@ return newParams; }; + ApiClient.prototype.deserialize = function deserialize(response, returnType) { + if (response == null || returnType == null) { + return null; + } + // Rely on Superagent for parsing response body. + // See http://visionmedia.github.io/superagent/#parsing-response-bodies + var data = response.body; + if (data == null) { + return null; + } + return ApiClient.convertToType(data, returnType); + }; + ApiClient.prototype.callApi = function callApi(path, httpMethod, pathParams, queryParams, headerParams, formParams, bodyParam, contentTypes, accepts, - callback) { + returnType, callback) { + var _this = this; var url = this.buildUrl(path, pathParams); var request = superagent(httpMethod, url); @@ -169,7 +189,10 @@ request.end(function(error, response) { if (callback) { - var data = response && response.body; + var data = null; + if (!error) { + data = _this.deserialize(response, returnType); + } callback(error, data, response); } }); @@ -177,6 +200,61 @@ return request; }; + ApiClient.parseDate = function parseDate(str) { + str = str.replace(/T/i, ' '); + return new Date(str); + }; + + ApiClient.convertToType = function convertToType(data, type) { + switch (type) { + case 'Boolean': + return Boolean(data); + case 'Integer': + return parseInt(data, 10); + case 'Number': + return parseFloat(data); + case 'String': + return String(data); + case 'Date': + return this.parseDate(String(data)); + default: + if (typeof type === 'function') { + // for model type like: User + var model = new type(); + model.constructFromObject(data); + return model; + } else if (Array.isArray(type)) { + // for array type like: ['String'] + var itemType = type[0]; + return data.map(function(item) { + return ApiClient.convertToType(item, itemType); + }); + } else if (typeof type === 'object') { + // for plain object type like: {'String': 'Integer'} + var keyType, valueType; + for (var k in type) { + if (type.hasOwnProperty(k)) { + keyType = k; + valueType = type[k]; + break; + } + } + var result = {}; + for (var k in data) { + if (data.hasOwnProperty(k)) { + var key = ApiClient.convertToType(k, keyType); + var value = ApiClient.convertToType(data[k], valueType); + result[key] = value; + } + } + return result; + } else { + // for unknown type, return the data directly + return data; + } + } + }; + ApiClient.default = new ApiClient(); return ApiClient; diff --git a/samples/client/petstore/javascript/src/api/PetApi.js b/samples/client/petstore/javascript/src/api/PetApi.js index 1746e343abb7..227ab40acc99 100644 --- a/samples/client/petstore/javascript/src/api/PetApi.js +++ b/samples/client/petstore/javascript/src/api/PetApi.js @@ -43,18 +43,12 @@ var contentTypes = ['application/json', 'application/xml']; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = null; return this.apiClient.callApi( '/pet', 'PUT', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -81,18 +75,12 @@ var contentTypes = ['application/json', 'application/xml']; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = null; return this.apiClient.callApi( '/pet', 'POST', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -100,9 +88,9 @@ /** * Finds Pets by status * Multiple status values can be provided with comma seperated strings - * @param {Array} status Status values that need to be considered for filter + * @param {[String]} status Status values that need to be considered for filter * @param {function} callback the callback function, accepting three arguments: error, data, response - * data is of type: Array + * data is of type: [Pet] */ self.findPetsByStatus = function(status, callback) { var postBody = null; @@ -121,19 +109,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - // TODO: support deserializing array of models - callback(error, data, response); - }; - } + var returnType = [Pet]; return this.apiClient.callApi( '/pet/findByStatus', 'GET', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -141,9 +122,9 @@ /** * Finds Pets by tags * Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing. - * @param {Array} tags Tags to filter by + * @param {[String]} tags Tags to filter by * @param {function} callback the callback function, accepting three arguments: error, data, response - * data is of type: Array + * data is of type: [Pet] */ self.findPetsByTags = function(tags, callback) { var postBody = null; @@ -162,19 +143,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - // TODO: support deserializing array of models - callback(error, data, response); - }; - } + var returnType = [Pet]; return this.apiClient.callApi( '/pet/findByTags', 'GET', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -208,24 +182,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - if (!error && data) { - var result = new Pet(); - result.constructFromObject(data); - callback(error, result, response); - } else { - callback(error, data, response); - } - }; - } + var returnType = Pet; return this.apiClient.callApi( '/pet/{petId}', 'GET', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -262,18 +224,12 @@ var contentTypes = ['application/x-www-form-urlencoded']; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = null; return this.apiClient.callApi( '/pet/{petId}', 'POST', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -308,18 +264,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = null; return this.apiClient.callApi( '/pet/{petId}', 'DELETE', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -356,18 +306,12 @@ var contentTypes = ['multipart/form-data']; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = null; return this.apiClient.callApi( '/pet/{petId}/uploadImage', 'POST', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -377,7 +321,7 @@ * Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions * @param {Integer} petId ID of pet that needs to be fetched * @param {function} callback the callback function, accepting three arguments: error, data, response - * data is of type: String + * data is of type: 'String' */ self.getPetByIdWithByteArray = function(petId, callback) { var postBody = null; @@ -401,18 +345,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = 'String'; return this.apiClient.callApi( '/pet/{petId}?testing_byte_array=true', 'GET', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -439,18 +377,12 @@ var contentTypes = ['application/json', 'application/xml']; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = null; return this.apiClient.callApi( '/pet?testing_byte_array=true', 'POST', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } diff --git a/samples/client/petstore/javascript/src/api/StoreApi.js b/samples/client/petstore/javascript/src/api/StoreApi.js index 7ef38358e9c5..632d5b836314 100644 --- a/samples/client/petstore/javascript/src/api/StoreApi.js +++ b/samples/client/petstore/javascript/src/api/StoreApi.js @@ -25,7 +25,7 @@ * Returns pet inventories by status * Returns a map of status codes to quantities * @param {function} callback the callback function, accepting three arguments: error, data, response - * data is of type: Object + * data is of type: {'String': 'Integer'} */ self.getInventory = function(callback) { var postBody = null; @@ -43,18 +43,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = {'String': 'Integer'}; return this.apiClient.callApi( '/store/inventory', 'GET', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -82,24 +76,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - if (!error && data) { - var result = new Order(); - result.constructFromObject(data); - callback(error, result, response); - } else { - callback(error, data, response); - } - }; - } + var returnType = Order; return this.apiClient.callApi( '/store/order', 'POST', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -133,24 +115,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - if (!error && data) { - var result = new Order(); - result.constructFromObject(data); - callback(error, result, response); - } else { - callback(error, data, response); - } - }; - } + var returnType = Order; return this.apiClient.callApi( '/store/order/{orderId}', 'GET', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -183,18 +153,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = null; return this.apiClient.callApi( '/store/order/{orderId}', 'DELETE', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } diff --git a/samples/client/petstore/javascript/src/api/UserApi.js b/samples/client/petstore/javascript/src/api/UserApi.js index 528214b2f8b2..2ab1f1f3abb7 100644 --- a/samples/client/petstore/javascript/src/api/UserApi.js +++ b/samples/client/petstore/javascript/src/api/UserApi.js @@ -43,18 +43,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = null; return this.apiClient.callApi( '/user', 'POST', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -62,7 +56,7 @@ /** * Creates list of users with given input array * - * @param {Array} body List of user object + * @param {[User]} body List of user object * @param {function} callback the callback function, accepting three arguments: error, data, response */ self.createUsersWithArrayInput = function(body, callback) { @@ -81,18 +75,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = null; return this.apiClient.callApi( '/user/createWithArray', 'POST', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -100,7 +88,7 @@ /** * Creates list of users with given input array * - * @param {Array} body List of user object + * @param {[User]} body List of user object * @param {function} callback the callback function, accepting three arguments: error, data, response */ self.createUsersWithListInput = function(body, callback) { @@ -119,18 +107,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = null; return this.apiClient.callApi( '/user/createWithList', 'POST', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -141,7 +123,7 @@ * @param {String} username The user name for login * @param {String} password The password for login in clear text * @param {function} callback the callback function, accepting three arguments: error, data, response - * data is of type: String + * data is of type: 'String' */ self.loginUser = function(username, password, callback) { var postBody = null; @@ -161,18 +143,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = 'String'; return this.apiClient.callApi( '/user/login', 'GET', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -198,18 +174,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = null; return this.apiClient.callApi( '/user/logout', 'GET', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -243,24 +213,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - if (!error && data) { - var result = new User(); - result.constructFromObject(data); - callback(error, result, response); - } else { - callback(error, data, response); - } - }; - } + var returnType = User; return this.apiClient.callApi( '/user/{username}', 'GET', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -294,18 +252,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = null; return this.apiClient.callApi( '/user/{username}', 'PUT', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } @@ -338,18 +290,12 @@ var contentTypes = []; var accepts = ['application/json', 'application/xml']; - - var handleResponse = null; - if (callback) { - handleResponse = function(error, data, response) { - callback(error, data, response); - }; - } + var returnType = null; return this.apiClient.callApi( '/user/{username}', 'DELETE', pathParams, queryParams, headerParams, formParams, postBody, - contentTypes, accepts, handleResponse + contentTypes, accepts, returnType, callback ); } diff --git a/samples/client/petstore/javascript/src/model/Category.js b/samples/client/petstore/javascript/src/model/Category.js index 6b302a32f302..9f7c53a2240a 100644 --- a/samples/client/petstore/javascript/src/model/Category.js +++ b/samples/client/petstore/javascript/src/model/Category.js @@ -1,18 +1,18 @@ (function(root, factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. - define([undefined], factory); + define([undefined, '../ApiClient'], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(undefined); + module.exports = factory(undefined, require('../ApiClient')); } else { // Browser globals (root is window) if (!root.SwaggerPetstore) { root.SwaggerPetstore = {}; } - factory(root.SwaggerPetstore); + factory(root.SwaggerPetstore, root.SwaggerPetstore.ApiClient); } -}(this, function(module) { +}(this, function(module, ApiClient) { 'use strict'; @@ -25,23 +25,24 @@ /** * datatype: Integer **/ - self.id = null; + self['id'] = null; /** * datatype: String **/ - self.name = null; + self['name'] = null; self.constructFromObject = function(data) { if (!data) { - return; + return this; } - self.id = data.id; + self['id'] = ApiClient.convertToType(data['id'], 'Integer'); - self.name = data.name; + self['name'] = ApiClient.convertToType(data['name'], 'String'); + return this; } @@ -49,32 +50,32 @@ * @return {Integer} **/ self.getId = function() { - return self.id; + return self['id']; } /** * @param {Integer} id **/ - self.setId = function (id) { - self.id = id; + self.setId = function(id) { + self['id'] = id; } /** * @return {String} **/ self.getName = function() { - return self.name; + return self['name']; } /** * @param {String} name **/ - self.setName = function (name) { - self.name = name; + self.setName = function(name) { + self['name'] = name; } - self.toJson = function () { + self.toJson = function() { return JSON.stringify(self); } }; diff --git a/samples/client/petstore/javascript/src/model/Order.js b/samples/client/petstore/javascript/src/model/Order.js index 9f641242bf86..1ac5ff706af6 100644 --- a/samples/client/petstore/javascript/src/model/Order.js +++ b/samples/client/petstore/javascript/src/model/Order.js @@ -1,46 +1,46 @@ (function(root, factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. - define([undefined], factory); + define([undefined, '../ApiClient'], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(undefined); + module.exports = factory(undefined, require('../ApiClient')); } else { // Browser globals (root is window) if (!root.SwaggerPetstore) { root.SwaggerPetstore = {}; } - factory(root.SwaggerPetstore); + factory(root.SwaggerPetstore, root.SwaggerPetstore.ApiClient); } -}(this, function(module) { +}(this, function(module, ApiClient) { 'use strict'; //export module -if ( typeof define === "function" && define.amd ) { - define('StatusEnum', ['jquery'], function($) { +if ( typeof define === "function" && define.amd ) { + define('StatusEnum', [], function() { return StatusEnum; }); } var StatusEnum = function StatusEnum() { var self = this; - + /** - * @const - */ + * @const + */ self.PLACED = "placed", /** - * @const - */ + * @const + */ self.APPROVED = "approved", /** - * @const - */ + * @const + */ self.DELIVERED = "delivered"; } @@ -53,52 +53,53 @@ var StatusEnum = function StatusEnum() { /** * datatype: Integer **/ - self.id = null; + self['id'] = null; /** * datatype: Integer **/ - self.petId = null; + self['petId'] = null; /** * datatype: Integer **/ - self.quantity = null; + self['quantity'] = null; /** * datatype: Date **/ - self.shipDate = null; + self['shipDate'] = null; /** * Order Status * datatype: StatusEnum **/ - self.status = null; + self['status'] = null; /** * datatype: Boolean **/ - self.complete = null; + self['complete'] = null; self.constructFromObject = function(data) { if (!data) { - return; + return this; } - self.id = data.id; + self['id'] = ApiClient.convertToType(data['id'], 'Integer'); - self.petId = data.petId; + self['petId'] = ApiClient.convertToType(data['petId'], 'Integer'); - self.quantity = data.quantity; + self['quantity'] = ApiClient.convertToType(data['quantity'], 'Integer'); - self.shipDate = data.shipDate; + self['shipDate'] = ApiClient.convertToType(data['shipDate'], 'Date'); - self.status = data.status; + self['status'] = ApiClient.convertToType(data['status'], 'String'); - self.complete = data.complete; + self['complete'] = ApiClient.convertToType(data['complete'], 'Boolean'); + return this; } @@ -106,56 +107,56 @@ var StatusEnum = function StatusEnum() { * @return {Integer} **/ self.getId = function() { - return self.id; + return self['id']; } /** * @param {Integer} id **/ - self.setId = function (id) { - self.id = id; + self.setId = function(id) { + self['id'] = id; } /** * @return {Integer} **/ self.getPetId = function() { - return self.petId; + return self['petId']; } /** * @param {Integer} petId **/ - self.setPetId = function (petId) { - self.petId = petId; + self.setPetId = function(petId) { + self['petId'] = petId; } /** * @return {Integer} **/ self.getQuantity = function() { - return self.quantity; + return self['quantity']; } /** * @param {Integer} quantity **/ - self.setQuantity = function (quantity) { - self.quantity = quantity; + self.setQuantity = function(quantity) { + self['quantity'] = quantity; } /** * @return {Date} **/ self.getShipDate = function() { - return self.shipDate; + return self['shipDate']; } /** * @param {Date} shipDate **/ - self.setShipDate = function (shipDate) { - self.shipDate = shipDate; + self.setShipDate = function(shipDate) { + self['shipDate'] = shipDate; } /** @@ -163,33 +164,33 @@ var StatusEnum = function StatusEnum() { * @return {StatusEnum} **/ self.getStatus = function() { - return self.status; + return self['status']; } /** * set Order Status * @param {StatusEnum} status **/ - self.setStatus = function (status) { - self.status = status; + self.setStatus = function(status) { + self['status'] = status; } /** * @return {Boolean} **/ self.getComplete = function() { - return self.complete; + return self['complete']; } /** * @param {Boolean} complete **/ - self.setComplete = function (complete) { - self.complete = complete; + self.setComplete = function(complete) { + self['complete'] = complete; } - self.toJson = function () { + self.toJson = function() { return JSON.stringify(self); } }; diff --git a/samples/client/petstore/javascript/src/model/Pet.js b/samples/client/petstore/javascript/src/model/Pet.js index 392007d6a54b..d50823edefb7 100644 --- a/samples/client/petstore/javascript/src/model/Pet.js +++ b/samples/client/petstore/javascript/src/model/Pet.js @@ -1,46 +1,46 @@ (function(root, factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. - define([undefined, './Category', './Tag'], factory); + define([undefined, '../ApiClient', './Category', './Tag'], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(undefined, require('./Category'), require('./Tag')); + module.exports = factory(undefined, require('../ApiClient'), require('./Category'), require('./Tag')); } else { // Browser globals (root is window) if (!root.SwaggerPetstore) { root.SwaggerPetstore = {}; } - factory(root.SwaggerPetstore, root.SwaggerPetstore.Category, root.SwaggerPetstore.Tag); + factory(root.SwaggerPetstore, root.SwaggerPetstore.ApiClient, root.SwaggerPetstore.Category, root.SwaggerPetstore.Tag); } -}(this, function(module, Category, Tag) { +}(this, function(module, ApiClient, Category, Tag) { 'use strict'; //export module -if ( typeof define === "function" && define.amd ) { - define('StatusEnum', ['jquery'], function($) { +if ( typeof define === "function" && define.amd ) { + define('StatusEnum', [], function() { return StatusEnum; }); } var StatusEnum = function StatusEnum() { var self = this; - + /** - * @const - */ + * @const + */ self.AVAILABLE = "available", /** - * @const - */ + * @const + */ self.PENDING = "pending", /** - * @const - */ + * @const + */ self.SOLD = "sold"; } @@ -53,54 +53,55 @@ var StatusEnum = function StatusEnum() { /** * datatype: Integer **/ - self.id = null; + self['id'] = null; /** * datatype: Category **/ - self.category = new Category(); + self['category'] = new Category(); /** * datatype: String * required **/ - self.name = name; + self['name'] = name; /** - * datatype: Array + * datatype: [String] * required **/ - self.photoUrls = photoUrls; + self['photoUrls'] = photoUrls; /** - * datatype: Array + * datatype: [Tag] **/ - self.tags = []; + self['tags'] = []; /** * pet status in the store * datatype: StatusEnum **/ - self.status = null; + self['status'] = null; self.constructFromObject = function(data) { if (!data) { - return; + return this; } - self.id = data.id; + self['id'] = ApiClient.convertToType(data['id'], 'Integer'); - self.category.constructFromObject(data.category); + self['category'].constructFromObject(data['category']); - self.name = data.name; + self['name'] = ApiClient.convertToType(data['name'], 'String'); - self.photoUrls = data.photoUrls; + self['photoUrls'] = ApiClient.convertToType(data['photoUrls'], ['String']); - self.tags = data.tags; + self['tags'] = ApiClient.convertToType(data['tags'], [Tag]); - self.status = data.status; + self['status'] = ApiClient.convertToType(data['status'], 'String'); + return this; } @@ -108,70 +109,70 @@ var StatusEnum = function StatusEnum() { * @return {Integer} **/ self.getId = function() { - return self.id; + return self['id']; } /** * @param {Integer} id **/ - self.setId = function (id) { - self.id = id; + self.setId = function(id) { + self['id'] = id; } /** * @return {Category} **/ self.getCategory = function() { - return self.category; + return self['category']; } /** * @param {Category} category **/ - self.setCategory = function (category) { - self.category = category; + self.setCategory = function(category) { + self['category'] = category; } /** * @return {String} **/ self.getName = function() { - return self.name; + return self['name']; } /** * @param {String} name **/ - self.setName = function (name) { - self.name = name; + self.setName = function(name) { + self['name'] = name; } /** - * @return {Array} + * @return {[String]} **/ self.getPhotoUrls = function() { - return self.photoUrls; + return self['photoUrls']; } /** - * @param {Array} photoUrls + * @param {[String]} photoUrls **/ - self.setPhotoUrls = function (photoUrls) { - self.photoUrls = photoUrls; + self.setPhotoUrls = function(photoUrls) { + self['photoUrls'] = photoUrls; } /** - * @return {Array} + * @return {[Tag]} **/ self.getTags = function() { - return self.tags; + return self['tags']; } /** - * @param {Array} tags + * @param {[Tag]} tags **/ - self.setTags = function (tags) { - self.tags = tags; + self.setTags = function(tags) { + self['tags'] = tags; } /** @@ -179,19 +180,19 @@ var StatusEnum = function StatusEnum() { * @return {StatusEnum} **/ self.getStatus = function() { - return self.status; + return self['status']; } /** * set pet status in the store * @param {StatusEnum} status **/ - self.setStatus = function (status) { - self.status = status; + self.setStatus = function(status) { + self['status'] = status; } - self.toJson = function () { + self.toJson = function() { return JSON.stringify(self); } }; diff --git a/samples/client/petstore/javascript/src/model/Tag.js b/samples/client/petstore/javascript/src/model/Tag.js index 2dbf932f300a..3180975e9764 100644 --- a/samples/client/petstore/javascript/src/model/Tag.js +++ b/samples/client/petstore/javascript/src/model/Tag.js @@ -1,18 +1,18 @@ (function(root, factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. - define([undefined], factory); + define([undefined, '../ApiClient'], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(undefined); + module.exports = factory(undefined, require('../ApiClient')); } else { // Browser globals (root is window) if (!root.SwaggerPetstore) { root.SwaggerPetstore = {}; } - factory(root.SwaggerPetstore); + factory(root.SwaggerPetstore, root.SwaggerPetstore.ApiClient); } -}(this, function(module) { +}(this, function(module, ApiClient) { 'use strict'; @@ -25,23 +25,24 @@ /** * datatype: Integer **/ - self.id = null; + self['id'] = null; /** * datatype: String **/ - self.name = null; + self['name'] = null; self.constructFromObject = function(data) { if (!data) { - return; + return this; } - self.id = data.id; + self['id'] = ApiClient.convertToType(data['id'], 'Integer'); - self.name = data.name; + self['name'] = ApiClient.convertToType(data['name'], 'String'); + return this; } @@ -49,32 +50,32 @@ * @return {Integer} **/ self.getId = function() { - return self.id; + return self['id']; } /** * @param {Integer} id **/ - self.setId = function (id) { - self.id = id; + self.setId = function(id) { + self['id'] = id; } /** * @return {String} **/ self.getName = function() { - return self.name; + return self['name']; } /** * @param {String} name **/ - self.setName = function (name) { - self.name = name; + self.setName = function(name) { + self['name'] = name; } - self.toJson = function () { + self.toJson = function() { return JSON.stringify(self); } }; diff --git a/samples/client/petstore/javascript/src/model/User.js b/samples/client/petstore/javascript/src/model/User.js index a76ccf19013b..dab5d3791b5a 100644 --- a/samples/client/petstore/javascript/src/model/User.js +++ b/samples/client/petstore/javascript/src/model/User.js @@ -1,18 +1,18 @@ (function(root, factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. - define([undefined], factory); + define([undefined, '../ApiClient'], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(undefined); + module.exports = factory(undefined, require('../ApiClient')); } else { // Browser globals (root is window) if (!root.SwaggerPetstore) { root.SwaggerPetstore = {}; } - factory(root.SwaggerPetstore); + factory(root.SwaggerPetstore, root.SwaggerPetstore.ApiClient); } -}(this, function(module) { +}(this, function(module, ApiClient) { 'use strict'; @@ -25,66 +25,67 @@ /** * datatype: Integer **/ - self.id = null; + self['id'] = null; /** * datatype: String **/ - self.username = null; + self['username'] = null; /** * datatype: String **/ - self.firstName = null; + self['firstName'] = null; /** * datatype: String **/ - self.lastName = null; + self['lastName'] = null; /** * datatype: String **/ - self.email = null; + self['email'] = null; /** * datatype: String **/ - self.password = null; + self['password'] = null; /** * datatype: String **/ - self.phone = null; + self['phone'] = null; /** * User Status * datatype: Integer **/ - self.userStatus = null; + self['userStatus'] = null; self.constructFromObject = function(data) { if (!data) { - return; + return this; } - self.id = data.id; + self['id'] = ApiClient.convertToType(data['id'], 'Integer'); - self.username = data.username; + self['username'] = ApiClient.convertToType(data['username'], 'String'); - self.firstName = data.firstName; + self['firstName'] = ApiClient.convertToType(data['firstName'], 'String'); - self.lastName = data.lastName; + self['lastName'] = ApiClient.convertToType(data['lastName'], 'String'); - self.email = data.email; + self['email'] = ApiClient.convertToType(data['email'], 'String'); - self.password = data.password; + self['password'] = ApiClient.convertToType(data['password'], 'String'); - self.phone = data.phone; + self['phone'] = ApiClient.convertToType(data['phone'], 'String'); - self.userStatus = data.userStatus; + self['userStatus'] = ApiClient.convertToType(data['userStatus'], 'Integer'); + return this; } @@ -92,98 +93,98 @@ * @return {Integer} **/ self.getId = function() { - return self.id; + return self['id']; } /** * @param {Integer} id **/ - self.setId = function (id) { - self.id = id; + self.setId = function(id) { + self['id'] = id; } /** * @return {String} **/ self.getUsername = function() { - return self.username; + return self['username']; } /** * @param {String} username **/ - self.setUsername = function (username) { - self.username = username; + self.setUsername = function(username) { + self['username'] = username; } /** * @return {String} **/ self.getFirstName = function() { - return self.firstName; + return self['firstName']; } /** * @param {String} firstName **/ - self.setFirstName = function (firstName) { - self.firstName = firstName; + self.setFirstName = function(firstName) { + self['firstName'] = firstName; } /** * @return {String} **/ self.getLastName = function() { - return self.lastName; + return self['lastName']; } /** * @param {String} lastName **/ - self.setLastName = function (lastName) { - self.lastName = lastName; + self.setLastName = function(lastName) { + self['lastName'] = lastName; } /** * @return {String} **/ self.getEmail = function() { - return self.email; + return self['email']; } /** * @param {String} email **/ - self.setEmail = function (email) { - self.email = email; + self.setEmail = function(email) { + self['email'] = email; } /** * @return {String} **/ self.getPassword = function() { - return self.password; + return self['password']; } /** * @param {String} password **/ - self.setPassword = function (password) { - self.password = password; + self.setPassword = function(password) { + self['password'] = password; } /** * @return {String} **/ self.getPhone = function() { - return self.phone; + return self['phone']; } /** * @param {String} phone **/ - self.setPhone = function (phone) { - self.phone = phone; + self.setPhone = function(phone) { + self['phone'] = phone; } /** @@ -191,19 +192,19 @@ * @return {Integer} **/ self.getUserStatus = function() { - return self.userStatus; + return self['userStatus']; } /** * set User Status * @param {Integer} userStatus **/ - self.setUserStatus = function (userStatus) { - self.userStatus = userStatus; + self.setUserStatus = function(userStatus) { + self['userStatus'] = userStatus; } - self.toJson = function () { + self.toJson = function() { return JSON.stringify(self); } }; diff --git a/samples/client/petstore/javascript/test/run_tests.html b/samples/client/petstore/javascript/test/run_tests.html index eb48f370365c..2916234bcf31 100644 --- a/samples/client/petstore/javascript/test/run_tests.html +++ b/samples/client/petstore/javascript/test/run_tests.html @@ -19,13 +19,13 @@ + + - -