diff --git a/src/http/index.js b/src/http/index.js index 2fd7340b2..6b774b861 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -413,6 +413,19 @@ export function encodeFormOrQuery(data) { * @param {string} parameterName - Parameter name * @return {object} encoded parameter names and values */ + if (typeof data === 'string') { + try { + data = JSON.parse(data); + Object.entries(data).forEach(([key, value]) => { + if (typeof value === 'object' && !Array.isArray(value)) { + data[key] = JSON.stringify(value); + } + }); + } catch { + return valueEncoder(data, 'reserved'); + } + } + const encodedQuery = Object.keys(data).reduce((result, parameterName) => { // eslint-disable-next-line no-restricted-syntax for (const [key, value] of formatKeyValue(parameterName, data[parameterName])) { diff --git a/test/oas3/execute/main.js b/test/oas3/execute/main.js index caff9c83f..88dc06a5c 100644 --- a/test/oas3/execute/main.js +++ b/test/oas3/execute/main.js @@ -1026,6 +1026,45 @@ describe('buildRequest - OpenAPI Specification 3.0', () => { }); }); + it('should serialize JSON values provided for schemas without properties', () => { + const req = buildRequest({ + spec: { + openapi: '3.0.0', + paths: { + '/': { + post: { + operationId: 'myOp', + requestBody: { + content: { + 'application/x-www-form-urlencoded': { + schema: { + type: 'object', + }, + }, + }, + }, + }, + }, + }, + }, + operationId: 'myOp', + requestBody: JSON.stringify({ + primitiveParam: 'string', + objectParam: { a: { b: 'c' }, d: [1, 2, 3] }, + }), + }); + + expect(req).toEqual({ + method: 'POST', + url: `/`, + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + credentials: 'same-origin', + body: 'primitiveParam=string&objectParam=%7B%22a%22%3A%7B%22b%22%3A%22c%22%7D%2C%22d%22%3A%5B1%2C2%2C3%5D%7D', + }); + }); + it('should not serialize undefined parameters', () => { const spec = { openapi: '3.0.1',