Skip to content

Commit

Permalink
fix(execute): fix encoding for stringified request body (#3476)
Browse files Browse the repository at this point in the history
  • Loading branch information
glowcloud committed Apr 23, 2024
1 parent 994a9c5 commit 4c28575
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
39 changes: 39 additions & 0 deletions test/oas3/execute/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 4c28575

Please sign in to comment.