mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
Fixed issue where content-type with wild characters resulted in mismacthes for valid collection headrs
This commit is contained in:
@@ -3689,15 +3689,19 @@ module.exports = {
|
|||||||
_.forEach(_.keys(contentObj), (contentType) => {
|
_.forEach(_.keys(contentObj), (contentType) => {
|
||||||
let contentMediaType = this.parseMediaType(contentType);
|
let contentMediaType = this.parseMediaType(contentType);
|
||||||
|
|
||||||
mediaTypes.push(contentMediaType.type + '/' + contentMediaType.subtype);
|
mediaTypes.push({
|
||||||
|
type: contentMediaType.type,
|
||||||
|
subtype: contentMediaType.subtype,
|
||||||
|
contentType: contentMediaType.type + '/' + contentMediaType.subtype
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// prefer JSON > XML > Other media types for suggested header.
|
// prefer JSON > XML > Other media types for suggested header.
|
||||||
_.forEach(mediaTypes, (mediaType) => {
|
_.forEach(mediaTypes, (mediaType) => {
|
||||||
let headerFamily = this.getHeaderFamily(mediaType);
|
let headerFamily = this.getHeaderFamily(mediaType.contentType);
|
||||||
|
|
||||||
if (headerFamily !== HEADER_TYPE.INVALID) {
|
if (headerFamily !== HEADER_TYPE.INVALID) {
|
||||||
suggestedContentHeader = mediaType;
|
suggestedContentHeader = mediaType.contentType;
|
||||||
hasComputedType = true;
|
hasComputedType = true;
|
||||||
if (headerFamily === HEADER_TYPE.JSON) {
|
if (headerFamily === HEADER_TYPE.JSON) {
|
||||||
return false;
|
return false;
|
||||||
@@ -3707,7 +3711,7 @@ module.exports = {
|
|||||||
|
|
||||||
// if no JSON or XML, take whatever we have
|
// if no JSON or XML, take whatever we have
|
||||||
if (!hasComputedType && mediaTypes.length > 0) {
|
if (!hasComputedType && mediaTypes.length > 0) {
|
||||||
suggestedContentHeader = mediaTypes[0];
|
suggestedContentHeader = mediaTypes[0].contentType;
|
||||||
hasComputedType = true;
|
hasComputedType = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3763,24 +3767,40 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invalid type of header found
|
// Invalid type of header found
|
||||||
else if (!_.isEmpty(contentHeader) && !_.includes(mediaTypes, contentHeaderMediaType)) {
|
else if (!_.isEmpty(contentHeader)) {
|
||||||
let mismatchObj = {
|
let mismatchObj,
|
||||||
property: mismatchProperty,
|
matched = false;
|
||||||
transactionJsonPath: transactionPathPrefix + `[${contentHeaderIndex}].value`,
|
|
||||||
schemaJsonPath: schemaPathPrefix,
|
|
||||||
reasonCode: 'INVALID_TYPE',
|
|
||||||
reason: `The ${humanPropName} "Content-Type" needs to be "${suggestedContentHeader}",` +
|
|
||||||
` but we found "${contentHeaderMediaType}" instead`
|
|
||||||
};
|
|
||||||
|
|
||||||
if (options.suggestAvailableFixes) {
|
// wildcard header macthing
|
||||||
mismatchObj.suggestedFix = {
|
_.forEach(mediaTypes, (mediaType) => {
|
||||||
key: 'Content-Type',
|
let transactionHeader = _.split(contentHeaderMediaType, '/'),
|
||||||
actualValue: contentHeader.value,
|
headerTypeMatched = (mediaType.type === '*' || mediaType.type === transactionHeader[0]),
|
||||||
suggestedValue: suggestedContentHeader
|
headerSubtypeMatched = (mediaType.subtype === '*' || mediaType.subtype === transactionHeader[1]);
|
||||||
|
|
||||||
|
if (headerTypeMatched && headerSubtypeMatched) {
|
||||||
|
matched = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!matched) {
|
||||||
|
mismatchObj = {
|
||||||
|
property: mismatchProperty,
|
||||||
|
transactionJsonPath: transactionPathPrefix + `[${contentHeaderIndex}].value`,
|
||||||
|
schemaJsonPath: schemaPathPrefix,
|
||||||
|
reasonCode: 'INVALID_TYPE',
|
||||||
|
reason: `The ${humanPropName} "Content-Type" needs to be "${suggestedContentHeader}",` +
|
||||||
|
` but we found "${contentHeaderMediaType}" instead`
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (options.suggestAvailableFixes) {
|
||||||
|
mismatchObj.suggestedFix = {
|
||||||
|
key: 'Content-Type',
|
||||||
|
actualValue: contentHeader.value,
|
||||||
|
suggestedValue: suggestedContentHeader
|
||||||
|
};
|
||||||
|
}
|
||||||
|
mismatches.push(mismatchObj);
|
||||||
}
|
}
|
||||||
mismatches.push(mismatchObj);
|
|
||||||
}
|
}
|
||||||
return mismatches;
|
return mismatches;
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -77,6 +77,71 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"event": []
|
"event": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "6067a189-11f4-4098-b87c-d1365d9ceb9b",
|
||||||
|
"name": "Update all pets",
|
||||||
|
"request": {
|
||||||
|
"name": "Update all pets",
|
||||||
|
"description": {},
|
||||||
|
"url": {
|
||||||
|
"path": [
|
||||||
|
"pets"
|
||||||
|
],
|
||||||
|
"host": [
|
||||||
|
"{{baseUrl}}"
|
||||||
|
],
|
||||||
|
"query": [],
|
||||||
|
"variable": []
|
||||||
|
},
|
||||||
|
"header": [
|
||||||
|
{
|
||||||
|
"key": "Content-Type",
|
||||||
|
"value": "application/json"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"method": "PUT",
|
||||||
|
"auth": null,
|
||||||
|
"body": {
|
||||||
|
"mode": "raw",
|
||||||
|
"raw": "{\n \"id\": 33231879,\n \"name\": \"d\",\n \"tag\": \"proident ullamco\"\n}"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"response": [
|
||||||
|
{
|
||||||
|
"id": "6bf06974-2393-43de-8a80-1d682b3cf4f7",
|
||||||
|
"name": "content with all allowed types",
|
||||||
|
"originalRequest": {
|
||||||
|
"url": {
|
||||||
|
"path": [
|
||||||
|
"pets"
|
||||||
|
],
|
||||||
|
"host": [
|
||||||
|
"{{baseUrl}}"
|
||||||
|
],
|
||||||
|
"query": [],
|
||||||
|
"variable": []
|
||||||
|
},
|
||||||
|
"method": "PUT",
|
||||||
|
"body": {
|
||||||
|
"mode": "raw",
|
||||||
|
"raw": "{\n \"id\": 33231879,\n \"name\": \"d\",\n \"tag\": \"proident ullamco\"\n}"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"status": "OK",
|
||||||
|
"code": 200,
|
||||||
|
"header": [
|
||||||
|
{
|
||||||
|
"key": "Content-Type",
|
||||||
|
"value": "text/xml"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"body": "",
|
||||||
|
"cookie": [],
|
||||||
|
"_postman_previewlanguage": "text"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"event": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"event": []
|
"event": []
|
||||||
|
|||||||
@@ -25,6 +25,20 @@ paths:
|
|||||||
'application/json; charset=utf-8':
|
'application/json; charset=utf-8':
|
||||||
schema:
|
schema:
|
||||||
$ref: "#/components/schemas/Pet"
|
$ref: "#/components/schemas/Pet"
|
||||||
|
put:
|
||||||
|
summary: Update all pets
|
||||||
|
requestBody:
|
||||||
|
content:
|
||||||
|
application/*:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Pet"
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: content with all allowed types
|
||||||
|
content:
|
||||||
|
'*/*':
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Pet"
|
||||||
components:
|
components:
|
||||||
schemas:
|
schemas:
|
||||||
Pet:
|
Pet:
|
||||||
|
|||||||
@@ -556,6 +556,38 @@ describe('VALIDATE FUNCTION TESTS ', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Should correctly match and validate content type headers having wildcard characters' +
|
||||||
|
' with collection req/res body', function (done) {
|
||||||
|
let differentContentTypesSpec = fs.readFileSync(path.join(__dirname, VALIDATION_DATA_FOLDER_PATH +
|
||||||
|
'/differentContentTypesSpec.yaml'), 'utf-8'),
|
||||||
|
differentContentTypesCollection = fs.readFileSync(path.join(__dirname, VALIDATION_DATA_FOLDER_PATH +
|
||||||
|
'/differentContentTypesCollection.json'), 'utf-8'),
|
||||||
|
resultObj,
|
||||||
|
historyRequest = [],
|
||||||
|
options = {
|
||||||
|
showMissingInSchemaErrors: true,
|
||||||
|
suggestAvailableFixes: true
|
||||||
|
},
|
||||||
|
schemaPack = new Converter.SchemaPack({ type: 'string', data: differentContentTypesSpec }, options);
|
||||||
|
|
||||||
|
getAllTransactions(JSON.parse(differentContentTypesCollection), historyRequest);
|
||||||
|
|
||||||
|
schemaPack.validateTransaction(historyRequest, (err, result) => {
|
||||||
|
expect(err).to.be.null;
|
||||||
|
expect(result).to.be.an('object');
|
||||||
|
resultObj = result.requests[historyRequest[1].id].endpoints[0];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Both req and res body should have matched content types
|
||||||
|
*/
|
||||||
|
expect(resultObj.matched).to.eql(true);
|
||||||
|
expect(resultObj.mismatches).to.have.lengthOf(0);
|
||||||
|
expect(resultObj.responses[_.keys(resultObj.responses)[0]].matched).to.eql(true);
|
||||||
|
expect(resultObj.responses[_.keys(resultObj.responses)[0]].mismatches).to.have.lengthOf(0);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('Should be able to validate and suggest correct value for body with primitive data type', function (done) {
|
it('Should be able to validate and suggest correct value for body with primitive data type', function (done) {
|
||||||
let primitiveDataTypeBodySpec = fs.readFileSync(path.join(__dirname, VALIDATION_DATA_FOLDER_PATH +
|
let primitiveDataTypeBodySpec = fs.readFileSync(path.join(__dirname, VALIDATION_DATA_FOLDER_PATH +
|
||||||
'/primitiveDataTypeBodySpec.yaml'), 'utf-8'),
|
'/primitiveDataTypeBodySpec.yaml'), 'utf-8'),
|
||||||
|
|||||||
Reference in New Issue
Block a user