You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you have a path definition of a multipart/form-data with a schema that contains an array and you pass just one element in this array, the backend receives a single value and not an array with just one element. This causes an exception by the openapi validator.
The problem lays in the function in the file http.js
function buildFormData(reqForm) {
/**
* Build a new FormData instance, support array as field value
* OAS2.0 - when collectionFormat is multi
* OAS3.0 - when explode of Encoding Object is true
* @param {Object} reqForm - ori req.form
* @return {FormData} - new FormData instance
*/
return Object.entries(reqForm).reduce((formData, [name, input]) => {
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of formatKeyValue(name, input, true)) {
if (Array.isArray(value)) {
// eslint-disable-next-line no-restricted-syntax
for (const v of value) {
formData.append(key, v);
}
} else {
formData.append(key, value);
}
}
return formData;
}, new FormData());
}
if in formData a fieldname is repeated more than once, then it is sent as an array, which is the actual behaviour of the function buildFormData. This curl, as an example, sends correctly photos as an array
But, if you have just one element in your array, the equivalent curl "produced" by the function buildFormData just sends a single value and not an array
The simplest way to fix it I think is to add '[]' to the name of the field in case it's an array:
function buildFormData(reqForm) {
/**
* Build a new FormData instance, support array as field value
* OAS2.0 - when collectionFormat is multi
* OAS3.0 - when explode of Encoding Object is true
* @param {Object} reqForm - ori req.form
* @return {FormData} - new FormData instance
*/
return Object.entries(reqForm).reduce((formData, [name, input]) => {
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of formatKeyValue(name, input, true)) {
if (Array.isArray(value)) {
// eslint-disable-next-line no-restricted-syntax
for (const v of value) {
formData.append(key + '[]', v);
}
} else {
formData.append(key, value);
}
}
return formData;
}, new FormData());
}
The text was updated successfully, but these errors were encountered:
mottolini
added a commit
to mottolini/swagger-js
that referenced
this issue
Sep 22, 2020
Q&A
Content & configuration
Swagger/OpenAPI definition:
Describe the bug you're encountering
When you have a path definition of a multipart/form-data with a schema that contains an array and you pass just one element in this array, the backend receives a single value and not an array with just one element. This causes an exception by the openapi validator.
The problem lays in the function in the file http.js
if in formData a fieldname is repeated more than once, then it is sent as an array, which is the actual behaviour of the function buildFormData. This curl, as an example, sends correctly
photos
as an arrayBut, if you have just one element in your array, the equivalent curl "produced" by the function buildFormData just sends a single value and not an array
The correct form to send an array is with the [] after the name of the field. The following curl sends an array even if there is just one element:
How to fix it
The simplest way to fix it I think is to add '[]' to the name of the field in case it's an array:
The text was updated successfully, but these errors were encountered: