Skip to content

Commit

Permalink
fix(execute): parse stringified objects nested in arrays (#3466)
Browse files Browse the repository at this point in the history
  • Loading branch information
glowcloud authored Apr 11, 2024
1 parent f72651a commit b1f2ee7
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/execute/oas3/content-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ export default function serialize(value, mediaType) {
// Assume the user has a JSON string
return value;
}

if (Array.isArray(value)) {
value = value.map((v) => {
try {
return JSON.parse(v);
} catch (e) {
return v;
}
});
}

return JSON.stringify(value);
}

return value.toString();
return String(value);
}
90 changes: 90 additions & 0 deletions test/oas3/execute/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,96 @@ describe('buildRequest - OpenAPI Specification 3.0', () => {
});
});

it('should serialize JSON values provided as arrays of stringified objects', () => {
const req = buildRequest({
spec: {
openapi: '3.0.0',
paths: {
'/{pathPartial}': {
post: {
operationId: 'myOp',
parameters: [
{
name: 'query',
in: 'query',
content: {
'application/json': {
schema: {
type: 'array',
items: {
type: 'object',
},
},
},
},
},
{
name: 'FooHeader',
in: 'header',
content: {
'application/json': {
schema: {
type: 'array',
items: {
type: 'object',
},
},
},
},
},
{
name: 'pathPartial',
in: 'path',
content: {
'application/json': {
schema: {
type: 'array',
items: {
type: 'object',
},
},
},
},
},
{
name: 'myCookie',
in: 'cookie',
content: {
'application/json': {
schema: {
type: 'array',
items: {
type: 'object',
},
},
},
},
},
],
},
},
},
},
operationId: 'myOp',
parameters: {
query: ['{"a":1}', '{"b":"2"}'],
FooHeader: ['{"foo":"bar"}'],
pathPartial: ['{"baz":"qux"}'],
myCookie: ['{"flavor":"chocolate chip"}'],
},
});

expect(req).toEqual({
method: 'POST',
url: `/${escape('[{"baz":"qux"}]')}?query=${escape('[{"a":1},{"b":"2"}]')}`,
credentials: 'same-origin',
headers: {
FooHeader: '[{"foo":"bar"}]',
Cookie: 'myCookie=[{"flavor":"chocolate chip"}]',
},
});
});

it('should not serialize undefined parameters', () => {
const spec = {
openapi: '3.0.1',
Expand Down

0 comments on commit b1f2ee7

Please sign in to comment.