From e082bb6f38a4be3728ceab72bb88164b65e1358d Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Wed, 22 Nov 2017 23:25:01 -0600 Subject: [PATCH] Stringify object values before serialization --- src/execute/oas3/build-request.js | 2 +- test/oas3/execute/main.js | 52 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/execute/oas3/build-request.js b/src/execute/oas3/build-request.js index e1a8995dc..1ecfce6b4 100644 --- a/src/execute/oas3/build-request.js +++ b/src/execute/oas3/build-request.js @@ -51,7 +51,7 @@ export default function (options, req) { Object.keys(requestBody).forEach((k) => { const val = requestBody[k] req.form[k] = { - value: val + value: typeof val === 'object' ? JSON.stringify(val) : val } }) } diff --git a/test/oas3/execute/main.js b/test/oas3/execute/main.js index d1c5479c1..e3c661c33 100644 --- a/test/oas3/execute/main.js +++ b/test/oas3/execute/main.js @@ -169,6 +169,58 @@ describe('buildRequest - OpenAPI Specification 3.0', function () { }) }) + it('should stringify object values of form data bodies', function () { + // Given + const spec = { + openapi: '3.0.0', + servers: [ + { + url: 'http://petstore.swagger.io/v2', + name: 'Petstore' + } + ], + paths: { + '/one': { + get: { + operationId: 'getOne', + requestBody: { + content: { + 'application/x-www-form-urlencoded': { + schema: { + type: 'object' + } + } + } + } + } + } + } + } + + // when + const req = buildRequest({ + spec, + operationId: 'getOne', + requestBody: { + a: 1, + b: { + c: 3, + d: 4 + } + } + }) + + expect(req).toEqual({ + method: 'GET', + url: 'http://petstore.swagger.io/v2/one', + credentials: 'same-origin', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + }, + body: 'a=1&b=%7B%22c%22%3A3%2C%22d%22%3A4%7D' + }) + }) + it('should build a request for the given operationId with a requestBody, and not be overriden by an invalid Swagger2 body parameter value', function () { // Given const spec = {