Correctly set json based Content-Type based on produces or consumes defined in swagger

This commit is contained in:
Vishrut Shah 2017-04-06 21:32:07 -07:00
Родитель 4630ba65a0
Коммит f1c2aa8255
2 изменённых файлов: 23 добавлений и 2 удалений

Просмотреть файл

@ -555,6 +555,10 @@ class SpecValidator {
}
}
if (!(options.headers['content-type'] || options.headers['Content-Type'])) {
options.headers['Content-Type'] = utils.getJsonContentType(operation.consumes);
}
let request = null;
let validationResult = {};
if (!foundIssues) {
@ -687,7 +691,7 @@ class SpecValidator {
}
//ensure content-type header is present
if (!(exampleResponseHeaders['content-type'] || exampleResponseHeaders['Content-Type'])) {
exampleResponseHeaders['content-type'] = operation.produces[0];
exampleResponseHeaders['content-type'] = utils.getJsonContentType(operation.produces);
}
let exampleResponse = new ResponseWrapper(exampleResponseStatusCode, exampleResponseBody, exampleResponseHeaders);
let validationResult = self.validateResponse(operation, exampleResponse);

Просмотреть файл

@ -360,4 +360,21 @@ exports.removeObject = function removeObject(doc, ptr) {
log.error(err);
}
return result;
};
};
/*
* Finds the first content-type that contains "/json". Only supported Content-Types are
* "text/json" & "application/json" so we perform first best match that contains '/json'
* @param {Array} Array of content-types.
*
* @param {string} content-type that contains "/json".
*/
exports.getJsonContentType = function getJsonContentType(consumesOrProduces) {
var firstMatchedJson = null;
if (consumesOrProduces) {
firstMatchedJson = consumesOrProduces.find((contentType) => {
return (contentType.match(/.*\/json.*/ig) !== null);
});
}
return firstMatchedJson;
};