Updated handling for existing urlencoded body conversion and to suggest serialised value according to spec

This commit is contained in:
Vishal Shingala
2021-01-06 10:49:21 +05:30
parent 45869abdfc
commit a3748c57e2

View File

@@ -1077,6 +1077,33 @@ module.exports = {
return helper;
},
/**
* Generates appropriate collection element based on parameter location
*
* @param {Object} param - Parameter object habing key, value and description (optional)
* @param {String} location - Parameter location ("in" property of OAS defined parameter object)
* @returns {Object} - SDK element
*/
generateSdkParam: function (param, location) {
const sdkElementMap = {
'query': sdk.QueryParam,
'header': sdk.Header,
'path': sdk.Variable
};
let generatedParam = {
key: param.key,
value: param.value
};
// use appropriate sdk element based on location parmaeter is in for param generation
if (sdkElementMap[location]) {
generatedParam = new sdkElementMap[location](generatedParam);
}
param.description && (generatedParam.description = param.description);
return generatedParam;
},
/**
* Generates Auth helper for response, params (query, headers) in helper object is added in
* request (originalRequest) part of example.
@@ -1475,22 +1502,22 @@ module.exports = {
case 'form':
if (explode && _.isObject(paramValue)) {
_.forEach(paramValue, (value, key) => {
pmParams.push({
pmParams.push(this.generateSdkParam({
key: _.isArray(paramValue) ? paramName : key,
value: (value === undefined ? '' : value),
description
});
}, _.get(param, 'in')));
});
return pmParams;
}
break;
case 'deepObject':
_.forOwn(paramValue, (value, key) => {
pmParams.push({
pmParams.push(this.generateSdkParam({
key: param.name + '[' + key + ']',
value: (value === undefined ? '' : value),
description
});
}, _.get(param, 'in')));
});
return pmParams;
default:
@@ -1515,11 +1542,11 @@ module.exports = {
// prepend starting value to serialised value (valid for empty value also)
serialisedValue = startValue + serialisedValue;
pmParams.push({
pmParams.push(this.generateSdkParam({
key: paramName,
value: serialisedValue,
description
});
}, _.get(param, 'in')));
return pmParams;
},
@@ -1625,55 +1652,36 @@ module.exports = {
}
description = (required ? '(Required) ' : '') + description +
(enumValue ? ' (This can only be one of ' + enumValue + ')' : '');
if (encoding.hasOwnProperty(key)) {
encoding[key].name = key;
encoding[key].schema = {
type: typeof value
};
encoding[key].description = description;
params = this.convertParamsWithStyle(encoding[key], value, PARAMETER_SOURCE.REQUEST, components,
schemaCache);
// TODO: Show warning for incorrect schema if !params
params && params.forEach((element) => {
// Collection v2.1 schema allows urlencoded param value to be only string
if (typeof element.value !== 'string') {
try {
// convert other datatype to string (i.e. number, boolean etc)
element.value = JSON.stringify(element.value);
}
catch (e) {
// JSON.stringify can fail in few cases, suggest invalid type for such case
// eslint-disable-next-line max-len
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Exceptions
element.value = 'INVALID_URLENCODED_PARAM_TYPE';
}
}
delete element.description;
});
paramArray.push(...params);
}
else {
!encoding[key] && (encoding[key] = {});
encoding[key].name = key;
encoding[key].schema = {
type: typeof value
};
// for urlencoded body serialisation is treated similar to query param
// reference https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#fixed-fields-13
encoding[key].in = 'query';
encoding[key].description = description;
params = this.convertParamsWithStyle(encoding[key], value, PARAMETER_SOURCE.REQUEST, components,
schemaCache);
// TODO: Show warning for incorrect schema if !params
params && params.forEach((element) => {
// Collection v2.1 schema allows urlencoded param value to be only string
if (typeof value !== 'string') {
if (typeof element.value !== 'string') {
try {
// convert other datatype to string (i.e. number, boolean etc)
value = JSON.stringify(value);
element.value = JSON.stringify(element.value);
}
catch (e) {
// JSON.stringify can fail in few cases, suggest invalid type for such case
// eslint-disable-next-line max-len
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Exceptions
value = 'INVALID_URLENCODED_PARAM_TYPE';
element.value = 'INVALID_URLENCODED_PARAM_TYPE';
}
}
param = new sdk.QueryParam({
key: key,
value: value
});
param.description = description;
paramArray.push(param);
}
});
paramArray.push(...params);
});
updateOptions = {
mode: rDataMode,
@@ -2209,7 +2217,6 @@ module.exports = {
});
});
item.request.url.query.members.forEach((query) => {
query.description = _.get(query, 'description.content', '');
// Collection v2.1 schema allows query param value to be string/null
if (typeof query.value !== 'string') {
try {
@@ -3744,8 +3751,8 @@ module.exports = {
isPropSeparable;
if (_.isObject(encodingValue)) {
encodingValue.style && (resolvedProp.style = encodingValue.style);
encodingValue.explode && (resolvedProp.explode = encodingValue.explode);
_.has(encodingValue, 'style') && (resolvedProp.style = encodingValue.style);
_.has(encodingValue, 'explode') && (resolvedProp.explode = encodingValue.explode);
}
pSerialisationInfo = this.getParamSerialisationInfo(resolvedProp, PARAMETER_SOURCE.REQUEST,
@@ -3799,6 +3806,8 @@ module.exports = {
resolvedParamValue = this.deserialiseParamValue(schemaParam, uParam.value, PARAMETER_SOURCE.REQUEST,
components, schemaCache);
}
// store value of transaction to use in mismatch object
schemaParam.actualValue = uParam.value;
// param found in spec. check param's schema
setTimeout(() => {
@@ -3807,7 +3816,7 @@ module.exports = {
return cb(null, []);
}
this.checkValueAgainstSchema(mismatchProperty,
transactionPathPrefix + `urlencoded.[${index}].value`,
transactionPathPrefix + `.urlencoded.[${index}].value`,
uParam.key,
resolvedParamValue,
pathPrefix + '.properties[' + schemaParam.name + ']',
@@ -3818,7 +3827,29 @@ module.exports = {
}, 0);
}, (err, res) => {
let mismatches = [],
mismatchObj;
mismatchObj,
// fetches property name from schem path
getPropNameFromSchemPath = (schemaPath) => {
let regex = /\.properties\[(.+)\]/gm;
return _.last(regex.exec(schemaPath));
};
// update actual value and suggested value from JSON to serialized strings
_.forEach(_.flatten(res), (mismatchObj) => {
if (!_.isEmpty(mismatchObj)) {
let propertyName = getPropNameFromSchemPath(mismatchObj.schemaJsonPath),
schemaParam = _.find(resolvedSchemaParams, (param) => { return param.name === propertyName; }),
serializedParamValue;
if (schemaParam) {
// serialize param value (to be used in suggested value)
serializedParamValue = _.get(this.convertParamsWithStyle(schemaParam, _.get(mismatchObj,
'suggestedFix.suggestedValue'), PARAMETER_SOURCE.REQUEST, components, schemaCache), '[0].value');
_.set(mismatchObj, 'suggestedFix.actualValue', schemaParam.actualValue);
_.set(mismatchObj, 'suggestedFix.suggestedValue', serializedParamValue);
}
}
});
_.each(resolvedSchemaParams, (uParam) => {
if (!_.find(requestBody.urlencoded, (param) => { return param.key === uParam.name; })) {