Skip to content

Commit

Permalink
Fixed multipart support for array type request body properties
Browse files Browse the repository at this point in the history
  • Loading branch information
neidhart-orlich-bt committed Mar 13, 2020
1 parent 5c60291 commit 20b883c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
57 changes: 29 additions & 28 deletions src/execute/oas3/build-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,9 @@ export default function (options, req) {
req.form = {}
Object.keys(requestBody).forEach((k) => {
const val = requestBody[k]
let newVal

let isFile

if (typeof File !== 'undefined') {
isFile = val instanceof File // eslint-disable-line no-undef
}

if (typeof Blob !== 'undefined') {
isFile = isFile || val instanceof Blob // eslint-disable-line no-undef
}

if (typeof Buffer !== 'undefined') {
isFile = isFile || Buffer.isBuffer(val)
}

if (typeof val === 'object' && !isFile) {
if (Array.isArray(val)) {
newVal = val.toString()
}
else {
newVal = JSON.stringify(val)
}
}
else {
newVal = val
}

req.form[k] = {
value: newVal
value: toPartContent(val, true)
}
})
}
Expand All @@ -105,6 +78,34 @@ export default function (options, req) {
return req
}

function toPartContent(val, inspectArrays){
if (inspectArrays && Array.isArray(val)) {
// setting 'inspectArrays' to false to avoid cycles in arrays that contain themselves
return val.map(v => toPartContent(v, false))
}

let isFile

if (typeof File !== 'undefined') {
isFile = val instanceof File // eslint-disable-line no-undef
}

if (typeof Blob !== 'undefined') {
isFile = isFile || val instanceof Blob // eslint-disable-line no-undef
}

if (typeof Buffer !== 'undefined') {
isFile = isFile || Buffer.isBuffer(val)
}

if (typeof val === 'object' && !isFile) {
return JSON.stringify(val)
}
else {
return val
}
}

// Add security values, to operations - that declare their need on them
// Adapted from the Swagger2 implementation
export function applySecurities({request, securities = {}, operation = {}, spec}) {
Expand Down
7 changes: 6 additions & 1 deletion src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,12 @@ export function mergeInQueryOrForm(req = {}) {
const FormData = require('isomorphic-form-data') // eslint-disable-line global-require
req.body = new FormData()
Object.keys(form).forEach((key) => {
req.body.append(key, formatValue(form[key], true))
const val = form[key]
if (Array.isArray(val.value)){
val.value.forEach(v => req.body.append(key, formatValue({value: v}, true)))
} else {
req.body.append(key, formatValue(val, true))
}
})
}
else {
Expand Down

0 comments on commit 20b883c

Please sign in to comment.