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): parse stringified objects #3474

Merged
merged 3 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
8 changes: 8 additions & 0 deletions src/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)]];
Expand Down
65 changes: 65 additions & 0 deletions test/oas3/execute/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down