Skip to content

Commit b56b11e

Browse files
authored
Merge pull request #1187 from shockey/bug/3788-form-data-object-value
Stringify object values before serialization
2 parents cb2ae20 + e082bb6 commit b56b11e

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/execute/oas3/build-request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default function (options, req) {
5151
Object.keys(requestBody).forEach((k) => {
5252
const val = requestBody[k]
5353
req.form[k] = {
54-
value: val
54+
value: typeof val === 'object' ? JSON.stringify(val) : val
5555
}
5656
})
5757
}

test/oas3/execute/main.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,58 @@ describe('buildRequest - OpenAPI Specification 3.0', function () {
169169
})
170170
})
171171

172+
it('should stringify object values of form data bodies', function () {
173+
// Given
174+
const spec = {
175+
openapi: '3.0.0',
176+
servers: [
177+
{
178+
url: 'http://petstore.swagger.io/v2',
179+
name: 'Petstore'
180+
}
181+
],
182+
paths: {
183+
'/one': {
184+
get: {
185+
operationId: 'getOne',
186+
requestBody: {
187+
content: {
188+
'application/x-www-form-urlencoded': {
189+
schema: {
190+
type: 'object'
191+
}
192+
}
193+
}
194+
}
195+
}
196+
}
197+
}
198+
}
199+
200+
// when
201+
const req = buildRequest({
202+
spec,
203+
operationId: 'getOne',
204+
requestBody: {
205+
a: 1,
206+
b: {
207+
c: 3,
208+
d: 4
209+
}
210+
}
211+
})
212+
213+
expect(req).toEqual({
214+
method: 'GET',
215+
url: 'http://petstore.swagger.io/v2/one',
216+
credentials: 'same-origin',
217+
headers: {
218+
'Content-Type': 'application/x-www-form-urlencoded'
219+
},
220+
body: 'a=1&b=%7B%22c%22%3A3%2C%22d%22%3A4%7D'
221+
})
222+
})
223+
172224
it('should build a request for the given operationId with a requestBody, and not be overriden by an invalid Swagger2 body parameter value', function () {
173225
// Given
174226
const spec = {

0 commit comments

Comments
 (0)