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 0b56ffbbe81..c95bec5e347 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 @@ -7,6 +7,7 @@ import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenModel; import io.swagger.codegen.CodegenOperation; +import io.swagger.codegen.CodegenParameter; import io.swagger.codegen.CodegenProperty; import io.swagger.codegen.CodegenType; import io.swagger.codegen.SupportingFile; @@ -445,6 +446,35 @@ public CodegenModel fromModel(String name, Model model, Map allDe return codegenModel; } + @Override + public Map postProcessOperations(Map objs) { + // Generate and store argument list string of each operation into + // vendor-extension: x-codegen-argList. + Map operations = (Map) objs.get("operations"); + if (operations != null) { + List ops = (List) operations.get("operation"); + for (CodegenOperation operation : ops) { + List argList = new ArrayList(); + boolean hasOptionalParams = false; + for (CodegenParameter p : operation.allParams) { + if (p.required != null && p.required) { + argList.add(p.paramName); + } else { + hasOptionalParams = true; + } + } + if (hasOptionalParams) { + argList.add("opts"); + } + if (!usePromises) { + argList.add("callback"); + } + operation.vendorExtensions.put("x-codegen-argList", StringUtils.join(argList, ", ")); + } + } + return objs; + } + @Override public Map postProcessModels(Map objs) { List models = (List) objs.get("models"); @@ -488,7 +518,7 @@ public Map postProcessModels(Map objs) { } allowableValues.put("enumVars", enumVars); } - // set vendor-extension: x-hasMoreRequired + // set vendor-extension: x-codegen-hasMoreRequired CodegenProperty lastRequired = null; for (CodegenProperty var : cm.vars) { if (var.required != null && var.required) { @@ -497,9 +527,9 @@ public Map postProcessModels(Map objs) { } for (CodegenProperty var : cm.vars) { if (var == lastRequired) { - var.vendorExtensions.put("x-hasMoreRequired", false); + var.vendorExtensions.put("x-codegen-hasMoreRequired", false); } else if (var.required != null && var.required) { - var.vendorExtensions.put("x-hasMoreRequired", true); + var.vendorExtensions.put("x-codegen-hasMoreRequired", true); } } } diff --git a/modules/swagger-codegen/src/main/resources/Javascript/api.mustache b/modules/swagger-codegen/src/main/resources/Javascript/api.mustache index a42ad349820..79a5fd2a674 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/api.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/api.mustache @@ -1,54 +1,54 @@ -(function(root, factory) { +{{=< >=}}(function(root, factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. - define(['../ApiClient'{{#imports}}, '../model/{{import}}'{{/imports}}], factory); + define(['../ApiClient'<#imports>, '../model/'], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(require('../ApiClient'){{#imports}}, require('../model/{{import}}'){{/imports}}); + module.exports = factory(require('../ApiClient')<#imports>, require('../model/')); } else { // Browser globals (root is window) - if (!root.{{moduleName}}) { - root.{{moduleName}} = {}; + if (!root.) { + root. = {}; } - root.{{moduleName}}.{{classname}} = factory(root.{{moduleName}}.ApiClient{{#imports}}, root.{{moduleName}}.{{import}}{{/imports}}); + root.. = factory(root..ApiClient<#imports>, root..); } -}(this, function(ApiClient{{#imports}}, {{import}}{{/imports}}) { +}(this, function(ApiClient<#imports>, ) { 'use strict'; - var {{classname}} = function {{classname}}(apiClient) { + var = function (apiClient) { this.apiClient = apiClient || ApiClient.default; var self = this; - {{#operations}} - {{#operation}} + <#operations> + <#operation> /** - * {{summary}} - * {{notes}} - {{#allParams}} * @param {{=<% %>=}}{<% dataType %>} <%={{ }}=%> {{paramName}} {{description}} - {{/allParams}} {{^usePromises}}* @param {function} callback the callback function, accepting three arguments: error, data, response{{/usePromises}}{{#returnType}} - * data is of type: {{{returnType}}}{{/returnType}} + * + * <#allParams> + * @param {} <#required><^required>opts[''] <^usePromises> + * @param {function} callback the callback function, accepting three arguments: error, data, response<#returnType> + * data is of type: <&returnType> */ - self.{{nickname}} = function({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{^usePromises}}{{#hasParams}}, {{/hasParams}}callback{{/usePromises}}) { - var postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}; - {{#allParams}}{{#required}} - // verify the required parameter '{{paramName}}' is set - if ({{paramName}} == null) { - throw "Missing the required parameter '{{paramName}}' when calling {{nickname}}"; + self. = function() {<#hasOptionalParams> + opts = opts || {}; + var postBody = <#bodyParam><#required><^required>opts['']<^bodyParam>null; + <#allParams><#required> + // verify the required parameter '' is set + if ( == null) { + throw "Missing the required parameter '' when calling "; } - {{/required}}{{/allParams}} + - {{=< >=}} var pathParams = {<#pathParams> - '': <#hasMore>, + '': <#required><^required>opts['']<#hasMore>, }; var queryParams = {<#queryParams> - '': <#collectionFormat>this.apiClient.buildCollectionParam(, '')<^collectionFormat><#hasMore>, + '': <#collectionFormat>this.apiClient.buildCollectionParam(<#required><^required>opts[''], '')<^collectionFormat><#required><^required>opts['']<#hasMore>, }; var headerParams = {<#headerParams> - '': <#hasMore>, + '': <#required><^required>opts['']<#hasMore>, }; var formParams = {<#formParams> - '': <#collectionFormat>this.apiClient.buildCollectionParam(, '')<^collectionFormat><#hasMore>, + '': <#collectionFormat>this.apiClient.buildCollectionParam(<#required><^required>opts[''], '')<^collectionFormat><#required><^required>opts['']<#hasMore>, }; var authNames = [<#authMethods>''<#hasMore>, ]; @@ -61,11 +61,11 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType<^usePromises>, callback ); - <={{ }}=> + } - {{/operation}} - {{/operations}} + + }; - return {{classname}}; -})); + return ; +}));<={{ }}=> diff --git a/modules/swagger-codegen/src/main/resources/Javascript/model.mustache b/modules/swagger-codegen/src/main/resources/Javascript/model.mustache index 9420afde771..1f0f48226a7 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/model.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/model.mustache @@ -18,7 +18,7 @@ {{#description}}/** * {{description}} **/{{/description}} - var {{classname}} = function {{classname}}({{#vars}}{{#required}}{{name}}{{#vendorExtensions.x-hasMoreRequired}}, {{/vendorExtensions.x-hasMoreRequired}}{{/required}}{{/vars}}) { {{#parent}}/* extends {{{parent}}}*/{{/parent}} + var {{classname}} = function {{classname}}({{#vars}}{{#required}}{{name}}{{#vendorExtensions.x-codegen-hasMoreRequired}}, {{/vendorExtensions.x-codegen-hasMoreRequired}}{{/required}}{{/vars}}) { {{#parent}}/* extends {{{parent}}}*/{{/parent}} {{#vars}}{{#required}} /**{{#description}} * {{{description}}}{{/description}} diff --git a/samples/client/petstore/javascript-promise/src/api/PetApi.js b/samples/client/petstore/javascript-promise/src/api/PetApi.js index 26e0e633ad6..b9dd09d6d9f 100644 --- a/samples/client/petstore/javascript-promise/src/api/PetApi.js +++ b/samples/client/petstore/javascript-promise/src/api/PetApi.js @@ -24,14 +24,13 @@ /** * Update an existing pet * - * @param {Pet} body Pet object that needs to be added to the store - + * @param {Pet} opts['body'] Pet object that needs to be added to the store */ - self.updatePet = function(body) { - var postBody = body; + self.updatePet = function(opts) { + opts = opts || {}; + var postBody = opts['body']; - var pathParams = { }; var queryParams = { @@ -51,20 +50,19 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Add a new pet to the store * - * @param {Pet} body Pet object that needs to be added to the store - + * @param {Pet} opts['body'] Pet object that needs to be added to the store */ - self.addPet = function(body) { - var postBody = body; + self.addPet = function(opts) { + opts = opts || {}; + var postBody = opts['body']; - var pathParams = { }; var queryParams = { @@ -84,25 +82,24 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Finds Pets by status * Multiple status values can be provided with comma seperated strings - * @param {[String]} status Status values that need to be considered for filter - + * @param {[String]} opts['status'] Status values that need to be considered for filter * data is of type: [Pet] */ - self.findPetsByStatus = function(status) { + self.findPetsByStatus = function(opts) { + opts = opts || {}; var postBody = null; - var pathParams = { }; var queryParams = { - 'status': this.apiClient.buildCollectionParam(status, 'multi') + 'status': this.apiClient.buildCollectionParam(opts['status'], 'multi') }; var headerParams = { }; @@ -119,25 +116,24 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Finds Pets by tags * Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing. - * @param {[String]} tags Tags to filter by - + * @param {[String]} opts['tags'] Tags to filter by * data is of type: [Pet] */ - self.findPetsByTags = function(tags) { + self.findPetsByTags = function(opts) { + opts = opts || {}; var postBody = null; - var pathParams = { }; var queryParams = { - 'tags': this.apiClient.buildCollectionParam(tags, 'multi') + 'tags': this.apiClient.buildCollectionParam(opts['tags'], 'multi') }; var headerParams = { }; @@ -154,14 +150,13 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Find pet by ID * 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 {Integer} petId ID of pet that needs to be fetched * data is of type: Pet */ self.getPetById = function(petId) { @@ -173,7 +168,6 @@ } - var pathParams = { 'petId': petId }; @@ -194,18 +188,18 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Updates a pet in the store with form data * - * @param {String} petId ID of pet that needs to be updated - * @param {String} name Updated name of the pet - * @param {String} status Updated status of the pet - + * @param {String} petId ID of pet that needs to be updated + * @param {String} opts['name'] Updated name of the pet + * @param {String} opts['status'] Updated status of the pet */ - self.updatePetWithForm = function(petId, name, status) { + self.updatePetWithForm = function(petId, opts) { + opts = opts || {}; var postBody = null; // verify the required parameter 'petId' is set @@ -214,7 +208,6 @@ } - var pathParams = { 'petId': petId }; @@ -223,8 +216,8 @@ var headerParams = { }; var formParams = { - 'name': name, - 'status': status + 'name': opts['name'], + 'status': opts['status'] }; var authNames = ['petstore_auth']; @@ -237,17 +230,17 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Deletes a pet * - * @param {Integer} petId Pet id to delete - * @param {String} apiKey - + * @param {Integer} petId Pet id to delete + * @param {String} opts['apiKey'] */ - self.deletePet = function(petId, apiKey) { + self.deletePet = function(petId, opts) { + opts = opts || {}; var postBody = null; // verify the required parameter 'petId' is set @@ -256,14 +249,13 @@ } - var pathParams = { 'petId': petId }; var queryParams = { }; var headerParams = { - 'api_key': apiKey + 'api_key': opts['apiKey'] }; var formParams = { }; @@ -278,18 +270,18 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * uploads an image * - * @param {Integer} petId ID of pet to update - * @param {String} additionalMetadata Additional data to pass to server - * @param {File} file file to upload - + * @param {Integer} petId ID of pet to update + * @param {String} opts['additionalMetadata'] Additional data to pass to server + * @param {File} opts['file'] file to upload */ - self.uploadFile = function(petId, additionalMetadata, file) { + self.uploadFile = function(petId, opts) { + opts = opts || {}; var postBody = null; // verify the required parameter 'petId' is set @@ -298,7 +290,6 @@ } - var pathParams = { 'petId': petId }; @@ -307,8 +298,8 @@ var headerParams = { }; var formParams = { - 'additionalMetadata': additionalMetadata, - 'file': file + 'additionalMetadata': opts['additionalMetadata'], + 'file': opts['file'] }; var authNames = ['petstore_auth']; @@ -321,14 +312,13 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Fake endpoint to test byte array return by 'Find pet by ID' * 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 {Integer} petId ID of pet that needs to be fetched * data is of type: 'String' */ self.getPetByIdWithByteArray = function(petId) { @@ -340,7 +330,6 @@ } - var pathParams = { 'petId': petId }; @@ -361,20 +350,19 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Fake endpoint to test byte array in body parameter for adding a new pet to the store * - * @param {String} body Pet object in the form of byte array - + * @param {String} opts['body'] Pet object in the form of byte array */ - self.addPetUsingByteArray = function(body) { - var postBody = body; + self.addPetUsingByteArray = function(opts) { + opts = opts || {}; + var postBody = opts['body']; - var pathParams = { }; var queryParams = { @@ -394,7 +382,7 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } diff --git a/samples/client/petstore/javascript-promise/src/api/StoreApi.js b/samples/client/petstore/javascript-promise/src/api/StoreApi.js index 57a3c57d86f..d47898d7495 100644 --- a/samples/client/petstore/javascript-promise/src/api/StoreApi.js +++ b/samples/client/petstore/javascript-promise/src/api/StoreApi.js @@ -24,14 +24,12 @@ /** * Returns pet inventories by status * Returns a map of status codes to quantities - * data is of type: {'String': 'Integer'} */ self.getInventory = function() { var postBody = null; - var pathParams = { }; var queryParams = { @@ -51,21 +49,20 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Place an order for a pet * - * @param {Order} body order placed for purchasing the pet - + * @param {Order} opts['body'] order placed for purchasing the pet * data is of type: Order */ - self.placeOrder = function(body) { - var postBody = body; + self.placeOrder = function(opts) { + opts = opts || {}; + var postBody = opts['body']; - var pathParams = { }; var queryParams = { @@ -85,14 +82,13 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Find purchase order by ID * For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions - * @param {String} orderId ID of pet that needs to be fetched - + * @param {String} orderId ID of pet that needs to be fetched * data is of type: Order */ self.getOrderById = function(orderId) { @@ -104,7 +100,6 @@ } - var pathParams = { 'orderId': orderId }; @@ -125,14 +120,13 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Delete purchase order by ID * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors - * @param {String} orderId ID of the order that needs to be deleted - + * @param {String} orderId ID of the order that needs to be deleted */ self.deleteOrder = function(orderId) { var postBody = null; @@ -143,7 +137,6 @@ } - var pathParams = { 'orderId': orderId }; @@ -164,7 +157,7 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } diff --git a/samples/client/petstore/javascript-promise/src/api/UserApi.js b/samples/client/petstore/javascript-promise/src/api/UserApi.js index bb93561ec1b..893ab3e68f1 100644 --- a/samples/client/petstore/javascript-promise/src/api/UserApi.js +++ b/samples/client/petstore/javascript-promise/src/api/UserApi.js @@ -24,14 +24,13 @@ /** * Create user * This can only be done by the logged in user. - * @param {User} body Created user object - + * @param {User} opts['body'] Created user object */ - self.createUser = function(body) { - var postBody = body; + self.createUser = function(opts) { + opts = opts || {}; + var postBody = opts['body']; - var pathParams = { }; var queryParams = { @@ -51,20 +50,19 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Creates list of users with given input array * - * @param {[User]} body List of user object - + * @param {[User]} opts['body'] List of user object */ - self.createUsersWithArrayInput = function(body) { - var postBody = body; + self.createUsersWithArrayInput = function(opts) { + opts = opts || {}; + var postBody = opts['body']; - var pathParams = { }; var queryParams = { @@ -84,20 +82,19 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Creates list of users with given input array * - * @param {[User]} body List of user object - + * @param {[User]} opts['body'] List of user object */ - self.createUsersWithListInput = function(body) { - var postBody = body; + self.createUsersWithListInput = function(opts) { + opts = opts || {}; + var postBody = opts['body']; - var pathParams = { }; var queryParams = { @@ -117,27 +114,26 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Logs user into the system * - * @param {String} username The user name for login - * @param {String} password The password for login in clear text - + * @param {String} opts['username'] The user name for login + * @param {String} opts['password'] The password for login in clear text * data is of type: 'String' */ - self.loginUser = function(username, password) { + self.loginUser = function(opts) { + opts = opts || {}; var postBody = null; - var pathParams = { }; var queryParams = { - 'username': username, - 'password': password + 'username': opts['username'], + 'password': opts['password'] }; var headerParams = { }; @@ -154,19 +150,17 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Logs out current logged in user session * - */ self.logoutUser = function() { var postBody = null; - var pathParams = { }; var queryParams = { @@ -186,14 +180,13 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Get user by user name * - * @param {String} username The name that needs to be fetched. Use user1 for testing. - + * @param {String} username The name that needs to be fetched. Use user1 for testing. * data is of type: User */ self.getUserByName = function(username) { @@ -205,7 +198,6 @@ } - var pathParams = { 'username': username }; @@ -226,18 +218,18 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Updated user * This can only be done by the logged in user. - * @param {String} username name that need to be deleted - * @param {User} body Updated user object - + * @param {String} username name that need to be deleted + * @param {User} opts['body'] Updated user object */ - self.updateUser = function(username, body) { - var postBody = body; + self.updateUser = function(username, opts) { + opts = opts || {}; + var postBody = opts['body']; // verify the required parameter 'username' is set if (username == null) { @@ -245,7 +237,6 @@ } - var pathParams = { 'username': username }; @@ -266,14 +257,13 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } /** * Delete user * This can only be done by the logged in user. - * @param {String} username The name that needs to be deleted - + * @param {String} username The name that needs to be deleted */ self.deleteUser = function(username) { var postBody = null; @@ -284,7 +274,6 @@ } - var pathParams = { 'username': username }; @@ -305,7 +294,7 @@ pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType ); - + } diff --git a/samples/client/petstore/javascript-promise/src/model/Pet.js b/samples/client/petstore/javascript-promise/src/model/Pet.js index 6b43162941c..3b8bdeef123 100644 --- a/samples/client/petstore/javascript-promise/src/model/Pet.js +++ b/samples/client/petstore/javascript-promise/src/model/Pet.js @@ -16,7 +16,7 @@ 'use strict'; - var Pet = function Pet(photoUrls, name) { + var Pet = function Pet(name, photoUrls) { /** * datatype: String diff --git a/samples/client/petstore/javascript-promise/test/api/PetApiTest.js b/samples/client/petstore/javascript-promise/test/api/PetApiTest.js index f57770999b8..d444529a927 100644 --- a/samples/client/petstore/javascript-promise/test/api/PetApiTest.js +++ b/samples/client/petstore/javascript-promise/test/api/PetApiTest.js @@ -29,7 +29,7 @@ var createRandomPet = function() { describe('PetApi', function() { it('should create and get pet', function(done) { var pet = createRandomPet(); - api.addPet(pet) + api.addPet({body: pet}) .then(function() { return api.getPetById(pet.id) }) diff --git a/samples/client/petstore/javascript-promise/test/run_tests.html b/samples/client/petstore/javascript-promise/test/run_tests.html index 2916234bcf3..2f3e3f1e33a 100644 --- a/samples/client/petstore/javascript-promise/test/run_tests.html +++ b/samples/client/petstore/javascript-promise/test/run_tests.html @@ -9,6 +9,7 @@ +