Skip to content

Commit

Permalink
fix(oas3-encoding): encode form-urlencoded array values as comma-sepa…
Browse files Browse the repository at this point in the history
…rated values by default
  • Loading branch information
shockey authored Jan 13, 2018
2 parents 153cd52 + 275c76a commit 2a28482
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/execute/oas3/build-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,22 @@ export default function (options, req) {
req.form = {}
Object.keys(requestBody).forEach((k) => {
const val = requestBody[k]
let newVal

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

req.form[k] = {
value: typeof val === 'object' ? JSON.stringify(val) : val
value: newVal
}
})
}
Expand Down
73 changes: 73 additions & 0 deletions test/bugs/ui-4071.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// https://github.com/swagger-api/swagger-ui/issues/4071

import expect, {createSpy, spyOn} from 'expect'
import {execute, buildRequest, baseUrl, self as stubs} from '../../src/execute'

const spec = {
openapi: '3.0.0',
servers: [
{
url: 'https://workbcjobs.api.gov.bc.ca/v1'
}
],
paths: {
'/jobs': {
post: {
operationId: 'postJobs',
description: 'The job feed endpoint returns an array of job records that satisfy the supplied criteria.',
responses: {
default: {
description: 'Unexpected error'
}
},
requestBody: {
content: {
'application/x-www-form-urlencoded': {
schema: {
type: 'object',
properties: {
industries: {
type: 'array',
items: {
type: 'integer'
}
}
}
},
encoding: {
industries: {
style: 'form'
}
}
}
}
}
}
}
}
}


it('should generate a request with application/x-www-form-urlencoded', function () {
const req = buildRequest({
spec,
requestContentType: 'application/x-www-form-urlencoded',
operationId: 'postJobs',
requestBody: {
industries: [
1,
16
]
}
})

expect(req).toEqual({
url: 'https://workbcjobs.api.gov.bc.ca/v1/jobs',
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'industries=1%2C16'
})
})

0 comments on commit 2a28482

Please sign in to comment.