Merge pull request #2034 from xhh/javascript-collection-format

[JavaScript] Support collectionFormat for parameters in JavaScript client
This commit is contained in:
wing328 2016-02-04 19:02:38 +08:00
commit 96f9e93cff
5 changed files with 90 additions and 4 deletions

View File

@ -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;

View File

@ -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>];

View File

@ -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;

View File

@ -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 = {
};

View File

@ -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