Skip to content

Commit

Permalink
fix(execute): parse stringified objects nested in arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
glowcloud committed Apr 10, 2024
1 parent f72651a commit 5c60b7e
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/execute/oas3/content-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ 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);
}

Expand Down
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 5c60b7e

Please sign in to comment.