diff --git a/src/http/index.js b/src/http/index.js index c3eec26da..2fd7340b2 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -323,6 +323,14 @@ function formatKeyValueBySerializationOption(key, value, skipEncoding, serializa const encodeFn = (v) => valueEncoder(v, escape); const encodeKeyFn = skipEncoding ? (k) => k : (k) => encodeFn(k); + if (typeof value === 'string') { + try { + value = JSON.parse(value); + } catch { + // can't parse the value so treat it as as a simple string + } + } + // Primitive if (typeof value !== 'object') { return [[encodeKeyFn(key), encodeFn(value)]]; diff --git a/test/oas3/execute/main.js b/test/oas3/execute/main.js index 6c2597eaa..caff9c83f 100644 --- a/test/oas3/execute/main.js +++ b/test/oas3/execute/main.js @@ -871,6 +871,71 @@ describe('buildRequest - OpenAPI Specification 3.0', () => { }); }); + it('should serialize JSON values provided as objects in `deepObject` style', () => { + const req = buildRequest({ + spec: { + openapi: '3.0.0', + paths: { + '/': { + post: { + operationId: 'myOp', + requestBody: { + content: { + 'application/x-www-form-urlencoded': { + schema: { + type: 'object', + properties: { + a: { + type: 'object', + properties: { + b: { + type: 'string', + }, + c: { + type: 'array', + }, + d: { + type: 'object', + }, + }, + }, + }, + }, + encoding: { + a: { + style: 'deepObject', + }, + }, + }, + }, + }, + }, + }, + }, + }, + operationId: 'myOp', + requestBody: { + a: { + b: 'c', + c: ['d', 'e'], + d: { + e: 'f', + }, + }, + }, + }); + + expect(req).toEqual({ + method: 'POST', + url: `/`, + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + credentials: 'same-origin', + body: 'a%5Bb%5D=c&a%5Bc%5D=%5B%22d%22%2C%22e%22%5D&a%5Bd%5D=%7B%22e%22%3A%22f%22%7D', + }); + }); + it('should serialize JSON values provided as arrays of stringified objects', () => { const req = buildRequest({ spec: {