Skip to content

Commit

Permalink
fix(execute): parse stringified objects
Browse files Browse the repository at this point in the history
  • Loading branch information
glowcloud committed Apr 19, 2024
1 parent 7f142cd commit 2702cf8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
16 changes: 15 additions & 1 deletion 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 Expand Up @@ -442,7 +450,13 @@ export function mergeInQueryOrForm(req = {}) {
req.formdata = formdata;
req.body = formdata;
} else {
req.body = encodeFormOrQuery(form);
let data = {};
if (typeof form === 'string') {
data = { form };
} else {
data = form;
}
req.body = encodeFormOrQuery(data);
}

delete req.form;
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

0 comments on commit 2702cf8

Please sign in to comment.