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 nested in arrays #3466

Merged
merged 2 commits into from
Apr 11, 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: 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