Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(execute): fix encoding for stringified request body #3476

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
char0n marked this conversation as resolved.
Show resolved Hide resolved
}
});
} 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