swagger-api/swagger-js

OpenAPI 2.x: make execute parameter builders for query and formData consistent with each other

glowcloud opened this issue · 1 comments

The logic for query and formData parameter builders should be the same.

The current form data builder processes truthy values differently than query, adding allowEmptyValue to it. It also adds an empty string as parameter value if the value is undefined and allowEmptyValue is true.

Form data builder:

function formDataBuilder({ req, value, parameter }) {
if (value === false && parameter.type === 'boolean') {
value = 'false';
}
if (value === 0 && ['number', 'integer'].indexOf(parameter.type) > -1) {
value = '0';
}
if (value || parameter.allowEmptyValue) {
req.form = req.form || {};
req.form[parameter.name] = {
value,
allowEmptyValue: parameter.allowEmptyValue,
collectionFormat: parameter.collectionFormat,
};
}
}

Query builder:

function queryBuilder({ req, value, parameter }) {
req.query = req.query || {};
if (value === false && parameter.type === 'boolean') {
value = 'false';
}
if (value === 0 && ['number', 'integer'].indexOf(parameter.type) > -1) {
value = '0';
}
if (value) {
req.query[parameter.name] = {
collectionFormat: parameter.collectionFormat,
value,
};
} else if (parameter.allowEmptyValue && value !== undefined) {
const paramName = parameter.name;
req.query[paramName] = req.query[paramName] || {};
req.query[paramName].allowEmptyValue = true;
}
}

Addressed in #3437