Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
'<baseName>': <paramName><#hasMore>,</hasMore></pathParams>
};
var queryParams = {<#queryParams>
'<baseName>': <paramName><#hasMore>,</hasMore></queryParams>
'<baseName>': <#collectionFormat>this.buildCollectionParam(<paramName>, '<collectionFormat>')</collectionFormat><^collectionFormat><paramName></collectionFormat><#hasMore>,</hasMore></queryParams>
};
var headerParams = {<#headerParams>
'<baseName>': <paramName><#hasMore>,</hasMore></headerParams>
};
var formParams = {<#formParams>
'<baseName>': <paramName><#hasMore>,</hasMore></formParams>
'<baseName>': <#collectionFormat>this.buildCollectionParam(<paramName>, '<collectionFormat>')</collectionFormat><^collectionFormat><paramName></collectionFormat><#hasMore>,</hasMore></formParams>
};

var contentTypes = [<#consumes>'<mediaType>'<#hasMore>, </hasMore></consumes>];
Expand Down
27 changes: 27 additions & 0 deletions samples/client/petstore/javascript/src/ApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/javascript/src/api/PetApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
var pathParams = {
};
var queryParams = {
'status': status
'status': this.buildCollectionParam(status, 'multi')
};
var headerParams = {
};
Expand Down Expand Up @@ -134,7 +134,7 @@
var pathParams = {
};
var queryParams = {
'tags': tags
'tags': this.buildCollectionParam(tags, 'multi')
};
var headerParams = {
};
Expand Down
32 changes: 32 additions & 0 deletions samples/client/petstore/javascript/test/ApiClientTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down