diff --git a/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache index 222ade010a7..b301da5c301 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache @@ -31,6 +31,8 @@ if (param == null) { // return empty string for null and undefined return ''; + } else if (param instanceof Date) { + return param.toJSON(); } else { return param.toString(); } @@ -131,6 +133,31 @@ return newParams; }; + /** + * Build parameter value according to the given collection format. + * @param {String} collectionFormat one of 'csv', 'ssv', 'tsv', 'pipes' and 'multi' + */ + ApiClient.prototype.buildCollectionParam = function buildCollectionParam(param, collectionFormat) { + if (param == null) { + return null; + } + switch (collectionFormat) { + case 'csv': + return param.map(this.paramToString).join(','); + case 'ssv': + return param.map(this.paramToString).join(' '); + case 'tsv': + return param.map(this.paramToString).join('\t'); + case 'pipes': + return param.map(this.paramToString).join('|'); + case 'multi': + // return the array directly as Superagent will handle it as expected + return param.map(this.paramToString); + default: + throw new Error('Unknown collection format: ' + collectionFormat); + } + }; + ApiClient.prototype.deserialize = function deserialize(response, returnType) { if (response == null || returnType == null) { return null; diff --git a/modules/swagger-codegen/src/main/resources/Javascript/api.mustache b/modules/swagger-codegen/src/main/resources/Javascript/api.mustache index f60c421eda7..57c3192400f 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/api.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/api.mustache @@ -42,13 +42,13 @@ '': <#hasMore>, }; var queryParams = {<#queryParams> - '': <#hasMore>, + '': <#collectionFormat>this.buildCollectionParam(, '')<^collectionFormat><#hasMore>, }; var headerParams = {<#headerParams> '': <#hasMore>, }; var formParams = {<#formParams> - '': <#hasMore>, + '': <#collectionFormat>this.buildCollectionParam(, '')<^collectionFormat><#hasMore>, }; var contentTypes = [<#consumes>''<#hasMore>, ]; diff --git a/samples/client/petstore/javascript/src/ApiClient.js b/samples/client/petstore/javascript/src/ApiClient.js index 17b271bc5ec..9968b2dfc88 100644 --- a/samples/client/petstore/javascript/src/ApiClient.js +++ b/samples/client/petstore/javascript/src/ApiClient.js @@ -31,6 +31,8 @@ if (param == null) { // return empty string for null and undefined return ''; + } else if (param instanceof Date) { + return param.toJSON(); } else { return param.toString(); } @@ -131,6 +133,31 @@ return newParams; }; + /** + * Build parameter value according to the given collection format. + * @param {String} collectionFormat one of 'csv', 'ssv', 'tsv', 'pipes' and 'multi' + */ + ApiClient.prototype.buildCollectionParam = function buildCollectionParam(param, collectionFormat) { + if (param == null) { + return null; + } + switch (collectionFormat) { + case 'csv': + return param.map(this.paramToString).join(','); + case 'ssv': + return param.map(this.paramToString).join(' '); + case 'tsv': + return param.map(this.paramToString).join('\t'); + case 'pipes': + return param.map(this.paramToString).join('|'); + case 'multi': + // return the array directly as Superagent will handle it as expected + return param.map(this.paramToString); + default: + throw new Error('Unknown collection format: ' + collectionFormat); + } + }; + ApiClient.prototype.deserialize = function deserialize(response, returnType) { if (response == null || returnType == null) { return null; diff --git a/samples/client/petstore/javascript/src/api/PetApi.js b/samples/client/petstore/javascript/src/api/PetApi.js index 227ab40acc9..91e330eab92 100644 --- a/samples/client/petstore/javascript/src/api/PetApi.js +++ b/samples/client/petstore/javascript/src/api/PetApi.js @@ -100,7 +100,7 @@ var pathParams = { }; var queryParams = { - 'status': status + 'status': this.buildCollectionParam(status, 'multi') }; var headerParams = { }; @@ -134,7 +134,7 @@ var pathParams = { }; var queryParams = { - 'tags': tags + 'tags': this.buildCollectionParam(tags, 'multi') }; var headerParams = { }; diff --git a/samples/client/petstore/javascript/test/ApiClientTest.js b/samples/client/petstore/javascript/test/ApiClientTest.js index daae849bcf1..a7714e6a593 100644 --- a/samples/client/petstore/javascript/test/ApiClientTest.js +++ b/samples/client/petstore/javascript/test/ApiClientTest.js @@ -36,6 +36,38 @@ describe('ApiClient', function() { }); }); + describe('#buildCollectionParam', function() { + var param; + + beforeEach(function() { + param = ['aa', 'bb', 123]; + }); + + it('works for csv', function() { + expect(apiClient.buildCollectionParam(param, 'csv')).to.be('aa,bb,123'); + }); + + it('works for ssv', function() { + expect(apiClient.buildCollectionParam(param, 'ssv')).to.be('aa bb 123'); + }); + + it('works for tsv', function() { + expect(apiClient.buildCollectionParam(param, 'tsv')).to.be('aa\tbb\t123'); + }); + + it('works for pipes', function() { + expect(apiClient.buildCollectionParam(param, 'pipes')).to.be('aa|bb|123'); + }); + + it('works for multi', function() { + expect(apiClient.buildCollectionParam(param, 'multi')).to.eql(['aa', 'bb', '123']); + }); + + it('fails for invalid collection format', function() { + expect(function() { apiClient.buildCollectionParam(param, 'INVALID'); }).to.throwError(); + }); + }); + describe('#buildUrl', function() { it('should work without path parameters in the path', function() { expect(apiClient.buildUrl('/abc', {})).to