forked from loafle/openapi-generator-original
JavaScript client: support collectionFormat for parameters
Closes #1953
This commit is contained in:
parent
35039bf122
commit
47f25efb25
@ -31,6 +31,8 @@
|
|||||||
if (param == null) {
|
if (param == null) {
|
||||||
// return empty string for null and undefined
|
// return empty string for null and undefined
|
||||||
return '';
|
return '';
|
||||||
|
} else if (param instanceof Date) {
|
||||||
|
return param.toJSON();
|
||||||
} else {
|
} else {
|
||||||
return param.toString();
|
return param.toString();
|
||||||
}
|
}
|
||||||
@ -131,6 +133,31 @@
|
|||||||
return newParams;
|
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) {
|
ApiClient.prototype.deserialize = function deserialize(response, returnType) {
|
||||||
if (response == null || returnType == null) {
|
if (response == null || returnType == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -42,13 +42,13 @@
|
|||||||
'<baseName>': <paramName><#hasMore>,</hasMore></pathParams>
|
'<baseName>': <paramName><#hasMore>,</hasMore></pathParams>
|
||||||
};
|
};
|
||||||
var queryParams = {<#queryParams>
|
var queryParams = {<#queryParams>
|
||||||
'<baseName>': <paramName><#hasMore>,</hasMore></queryParams>
|
'<baseName>': <#collectionFormat>this.buildCollectionParam(<paramName>, '<collectionFormat>')</collectionFormat><^collectionFormat><paramName></collectionFormat><#hasMore>,</hasMore></queryParams>
|
||||||
};
|
};
|
||||||
var headerParams = {<#headerParams>
|
var headerParams = {<#headerParams>
|
||||||
'<baseName>': <paramName><#hasMore>,</hasMore></headerParams>
|
'<baseName>': <paramName><#hasMore>,</hasMore></headerParams>
|
||||||
};
|
};
|
||||||
var formParams = {<#formParams>
|
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>];
|
var contentTypes = [<#consumes>'<mediaType>'<#hasMore>, </hasMore></consumes>];
|
||||||
|
@ -31,6 +31,8 @@
|
|||||||
if (param == null) {
|
if (param == null) {
|
||||||
// return empty string for null and undefined
|
// return empty string for null and undefined
|
||||||
return '';
|
return '';
|
||||||
|
} else if (param instanceof Date) {
|
||||||
|
return param.toJSON();
|
||||||
} else {
|
} else {
|
||||||
return param.toString();
|
return param.toString();
|
||||||
}
|
}
|
||||||
@ -131,6 +133,31 @@
|
|||||||
return newParams;
|
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) {
|
ApiClient.prototype.deserialize = function deserialize(response, returnType) {
|
||||||
if (response == null || returnType == null) {
|
if (response == null || returnType == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -100,7 +100,7 @@
|
|||||||
var pathParams = {
|
var pathParams = {
|
||||||
};
|
};
|
||||||
var queryParams = {
|
var queryParams = {
|
||||||
'status': status
|
'status': this.buildCollectionParam(status, 'multi')
|
||||||
};
|
};
|
||||||
var headerParams = {
|
var headerParams = {
|
||||||
};
|
};
|
||||||
@ -134,7 +134,7 @@
|
|||||||
var pathParams = {
|
var pathParams = {
|
||||||
};
|
};
|
||||||
var queryParams = {
|
var queryParams = {
|
||||||
'tags': tags
|
'tags': this.buildCollectionParam(tags, 'multi')
|
||||||
};
|
};
|
||||||
var headerParams = {
|
var headerParams = {
|
||||||
};
|
};
|
||||||
|
@ -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() {
|
describe('#buildUrl', function() {
|
||||||
it('should work without path parameters in the path', function() {
|
it('should work without path parameters in the path', function() {
|
||||||
expect(apiClient.buildUrl('/abc', {})).to
|
expect(apiClient.buildUrl('/abc', {})).to
|
||||||
|
Loading…
x
Reference in New Issue
Block a user