Merge pull request #87 from vishrutshah/content-type
Correctly set json based Content-Type based on produces or consumes defined in swagger
This commit is contained in:
Коммит
750a7a74fc
|
@ -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;
|
||||
};
|
||||
|
|
Загрузка…
Ссылка в новой задаче