Consistent response formats, more resilient against invalid schemas

This commit is contained in:
Abhijit Kane
2020-01-01 19:50:50 +05:30
parent 9a4b38d72d
commit a2bec105a9
2 changed files with 58 additions and 41 deletions

View File

@@ -1838,14 +1838,14 @@ module.exports = {
if (!schemaPathVar) {
// extra pathVar present in given request.
if (options.showMissingInSchemaErrors) {
mismatches.push({
property: mismatchProperty,
// not adding the pathVar name to the jsonPath because URL is just a string
transactionJsonPath: transactionPathPrefix,
schemaJsonPath: null,
reasonCode: 'MISSING_IN_SCHEMA',
reason: `The path variable ${pathVar.key} was not found in the schema`
});
mismatches.push({
property: mismatchProperty,
// not adding the pathVar name to the jsonPath because URL is just a string
transactionJsonPath: transactionPathPrefix,
schemaJsonPath: null,
reasonCode: 'MISSING_IN_SCHEMA',
reason: `The path variable ${pathVar.key} was not found in the schema`
});
}
return cb(null, mismatches);
}
@@ -1918,13 +1918,13 @@ module.exports = {
if (!schemaParam) {
// no schema param found
if (options.showMissingInSchemaErrors) {
mismatches.push({
property: mismatchProperty,
transactionJsonPath: transactionPathPrefix + '[?(@.key==\'' + pQuery.key + '\')]',
schemaJsonPath: null,
reasonCode: 'MISSING_IN_SCHEMA',
reason: `The query parameter ${pQuery.key} was not found in the schema`
});
mismatches.push({
property: mismatchProperty,
transactionJsonPath: transactionPathPrefix + '[?(@.key==\'' + pQuery.key + '\')]',
schemaJsonPath: null,
reasonCode: 'MISSING_IN_SCHEMA',
reason: `The query parameter ${pQuery.key} was not found in the schema`
});
}
return cb(null, mismatches);
}
@@ -1972,13 +1972,13 @@ module.exports = {
if (!schemaHeader) {
// no schema header found
if (options.showMissingInSchemaErrors) {
mismatches.push({
property: mismatchProperty,
transactionJsonPath: transactionPathPrefix + '[?(@.key==\'' + pHeader.key + '\')]',
schemaJsonPath: null,
reasonCode: 'MISSING_IN_SCHEMA',
reason: `The header ${pHeader.key} was not found in the schema`
});
mismatches.push({
property: mismatchProperty,
transactionJsonPath: transactionPathPrefix + '[?(@.key==\'' + pHeader.key + '\')]',
schemaJsonPath: null,
reasonCode: 'MISSING_IN_SCHEMA',
reason: `The header ${pHeader.key} was not found in the schema`
});
}
return cb(null, []);
}
@@ -2035,13 +2035,13 @@ module.exports = {
if (!schemaHeader) {
// no schema header found
if (options.showMissingInSchemaErrors) {
mismatches.push({
property: mismatchProperty,
transactionJsonPath: transactionPathPrefix + '/' + pHeader.key,
schemaJsonPath: schemaPathPrefix + '/headers',
reasonCode: 'MISSING_IN_SCHEMA',
reason: `The header ${pHeader.key} was not found in the schema`
});
mismatches.push({
property: mismatchProperty,
transactionJsonPath: transactionPathPrefix + '/' + pHeader.key,
schemaJsonPath: schemaPathPrefix + '/headers',
reasonCode: 'MISSING_IN_SCHEMA',
reason: `The header ${pHeader.key} was not found in the schema`
});
}
return cb(null, []);
}
@@ -2092,11 +2092,20 @@ module.exports = {
if (requestBody && requestBody.mode === 'raw' && jsonSchemaBody) {
// only raw for now
// the unknown formats are ones that are allowed in OAS, but not JSON schema
let ajv = new Ajv({ unknownFormats: ['int32', 'int64'], allErrors: true }),
validate = ajv.compile(deref.resolveRefs(jsonSchemaBody, 'request', components)),
let ajv,
validate,
res = true;
try {
ajv = new Ajv({ unknownFormats: ['int32', 'int64'], allErrors: true });
validate = ajv.compile(deref.resolveRefs(jsonSchemaBody, 'request', components));
res = validate(JSON.parse(requestBody.raw));
}
catch (e) {
// something went wrong validating the schema
// input was invalid. Don't throw mismatch
}
if (!res) {
mismatches.push({
property: mismatchProperty,
@@ -2175,6 +2184,7 @@ module.exports = {
if (!thisSchemaResponse) {
// still didn't find a response
responseCallback(null);
}
else {
// check headers and body
@@ -2199,7 +2209,7 @@ module.exports = {
});
}
}, (err, result) => {
var retVal = _.keyBy(result, 'id');
var retVal = _.keyBy(_.reject(result, (ai) => { return !ai; }), 'id');
return cb(null, retVal);
});
},