Handle empty response.body from superagent

Superagent does not always produce a `body`. See
http://visionmedia.github.io/superagent/ for details. When it
doesn't, we should deserialize the raw `response.text` rather
than returning `null`. Currently, the JS client always returns
`null` when the return type is String! This commit fixes
that.
This commit is contained in:
delenius
2016-02-17 09:30:20 -08:00
parent 61215f31fb
commit 692c865c76
7 changed files with 104 additions and 27 deletions

View File

@@ -21,6 +21,15 @@
*/
this.basePath = 'http://petstore.swagger.io/v2'.replace(/\/+$/, '');
this.authentications = {
'petstore_auth': {type: 'oauth2'},
'test_api_client_id': {type: 'apiKey', in: 'header', name: 'x-test_api_client_id'},
'test_api_client_secret': {type: 'apiKey', in: 'header', name: 'x-test_api_client_secret'},
'api_key': {type: 'apiKey', in: 'header', name: 'api_key'},
'test_api_key_query': {type: 'apiKey', in: 'query', name: 'test_api_key_query'},
'test_api_key_header': {type: 'apiKey', in: 'header', name: 'test_api_key_header'}
};
/**
* The default HTTP headers to be included for all API calls.
*/
@@ -158,6 +167,42 @@
}
};
ApiClient.prototype.applyAuthToRequest = function applyAuthToRequest(request, authNames) {
var _this = this;
authNames.forEach(function(authName) {
var auth = _this.authentications[authName];
switch (auth.type) {
case 'basic':
if (auth.username || auth.password) {
request.auth(auth.username || '', auth.password || '');
}
break;
case 'apiKey':
if (auth.apiKey) {
var data = {};
if (auth.apiKeyPrefix) {
data[auth.name] = auth.apiKeyPrefix + ' ' + auth.apiKey;
} else {
data[auth.name] = auth.apiKey;
}
if (auth.in === 'header') {
request.set(data);
} else {
request.query(data);
}
}
break;
case 'oauth2':
if (auth.accessToken) {
request.set({'Authorization': 'Bearer ' + auth.accessToken});
}
break;
default:
throw new Error('Unknown authentication type: ' + auth.type);
}
});
};
ApiClient.prototype.deserialize = function deserialize(response, returnType) {
if (response == null || returnType == null) {
return null;
@@ -166,18 +211,23 @@
// See http://visionmedia.github.io/superagent/#parsing-response-bodies
var data = response.body;
if (data == null) {
return null;
// Superagent does not always produce a body; use the unparsed response
// as a fallback
data = response.text;
}
return ApiClient.convertToType(data, returnType);
};
ApiClient.prototype.callApi = function callApi(path, httpMethod, pathParams,
queryParams, headerParams, formParams, bodyParam, contentTypes, accepts,
returnType) {
queryParams, headerParams, formParams, bodyParam, authNames, contentTypes,
accepts, returnType) {
var _this = this;
var url = this.buildUrl(path, pathParams);
var request = superagent(httpMethod, url);
// apply authentications
this.applyAuthToRequest(request, authNames);
// set query parameters
request.query(this.normalizeParams(queryParams));