diff --git a/src/execute/oas3/content-serializer.js b/src/execute/oas3/content-serializer.js index f31dcc025..1803f06d3 100644 --- a/src/execute/oas3/content-serializer.js +++ b/src/execute/oas3/content-serializer.js @@ -9,6 +9,17 @@ export default function serialize(value, mediaType) { // Assume the user has a JSON string return value; } + + if (Array.isArray(value)) { + value = value.map((v) => { + try { + return JSON.parse(v); + } catch (e) { + return v; + } + }); + } + return JSON.stringify(value); } diff --git a/test/oas3/execute/main.js b/test/oas3/execute/main.js index 50796a634..6c2597eaa 100644 --- a/test/oas3/execute/main.js +++ b/test/oas3/execute/main.js @@ -871,6 +871,96 @@ describe('buildRequest - OpenAPI Specification 3.0', () => { }); }); + it('should serialize JSON values provided as arrays of stringified objects', () => { + const req = buildRequest({ + spec: { + openapi: '3.0.0', + paths: { + '/{pathPartial}': { + post: { + operationId: 'myOp', + parameters: [ + { + name: 'query', + in: 'query', + content: { + 'application/json': { + schema: { + type: 'array', + items: { + type: 'object', + }, + }, + }, + }, + }, + { + name: 'FooHeader', + in: 'header', + content: { + 'application/json': { + schema: { + type: 'array', + items: { + type: 'object', + }, + }, + }, + }, + }, + { + name: 'pathPartial', + in: 'path', + content: { + 'application/json': { + schema: { + type: 'array', + items: { + type: 'object', + }, + }, + }, + }, + }, + { + name: 'myCookie', + in: 'cookie', + content: { + 'application/json': { + schema: { + type: 'array', + items: { + type: 'object', + }, + }, + }, + }, + }, + ], + }, + }, + }, + }, + operationId: 'myOp', + parameters: { + query: ['{"a":1}', '{"b":"2"}'], + FooHeader: ['{"foo":"bar"}'], + pathPartial: ['{"baz":"qux"}'], + myCookie: ['{"flavor":"chocolate chip"}'], + }, + }); + + expect(req).toEqual({ + method: 'POST', + url: `/${escape('[{"baz":"qux"}]')}?query=${escape('[{"a":1},{"b":"2"}]')}`, + credentials: 'same-origin', + headers: { + FooHeader: '[{"foo":"bar"}]', + Cookie: 'myCookie=[{"flavor":"chocolate chip"}]', + }, + }); + }); + it('should not serialize undefined parameters', () => { const spec = { openapi: '3.0.1',