support for value prop in example

Add support for value prop in example
This commit is contained in:
Luis Tejeda
2022-04-12 16:51:58 -05:00
committed by Erik Mendoza
parent fa623d32c0
commit ac9df06e84
3 changed files with 117 additions and 3 deletions

View File

@@ -3,7 +3,8 @@
* utils.js contains other util functions
*/
const { formatDataPath, checkIsCorrectType, isKnownType } = require('./common/schemaUtilsCommon.js'),
const { formatDataPath, checkIsCorrectType, isKnownType,
formatSchemaPathFromAJVErrorToConvertToDataPath } = require('./common/schemaUtilsCommon.js'),
{ getConcreteSchemaUtils } = require('./common/versionUtils.js'),
async = require('async'),
sdk = require('postman-collection'),
@@ -1360,6 +1361,28 @@ module.exports = {
return example;
},
useExampleValueAsValue(schema, example, components) {
let isCandidate = true,
schemaProperties = [],
schemaObject,
schemaDataPath = '',
exampleProperties = [];
if (!schema) {
return isCandidate;
}
if (schema.$ref) {
schemaDataPath = formatDataPath(formatSchemaPathFromAJVErrorToConvertToDataPath(schema.$ref));
schemaObject = _.get(components, schemaDataPath);
}
else {
schemaObject = schema;
}
schemaProperties = Object.keys(schemaObject.properties).sort();
exampleProperties = Object.keys(example.value).sort();
return JSON.stringify(schemaProperties) === JSON.stringify(exampleProperties);
},
/**
* converts one of the eamples or schema in Media Type object to postman data
* @param {*} bodyObj is MediaTypeObject
@@ -1410,7 +1433,8 @@ module.exports = {
}
bodyData = bodyObj.example;
// return example value if present else example is returned
if (bodyData.hasOwnProperty('value')) {
if (bodyData.hasOwnProperty('value') &&
this.useExampleValueAsValue(bodyObj.schema, bodyObj.example, components)) {
bodyData = bodyData.value;
}
}