Skip to content

Commit

Permalink
fix(execute): handle undefined serialized JSON Query param (#2844)
Browse files Browse the repository at this point in the history
When a query parameter has content-type JSON,
an undefined serialized value could result into
a JavaScript runtime error. This change will handle
undefined value and simply ignore it.
  • Loading branch information
acote-coveo committed Mar 8, 2023
1 parent e953f9c commit 285ade8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/execute/oas3/parameter-builders.js
Expand Up @@ -29,8 +29,16 @@ export function query({ req, value, parameter }) {

if (parameter.content) {
const effectiveMediaType = Object.keys(parameter.content)[0];
const serializedValue = serialize(value, effectiveMediaType);

if (serializedValue) {
req.query[parameter.name] = serializedValue;
} else if (parameter.allowEmptyValue && value !== undefined) {
const paramName = parameter.name;
req.query[paramName] = req.query[paramName] || {};
req.query[paramName].allowEmptyValue = true;
}

req.query[parameter.name] = serialize(value, effectiveMediaType);
return;
}

Expand Down
16 changes: 16 additions & 0 deletions test/client.js
Expand Up @@ -301,4 +301,20 @@ describe('http', () => {
});
});
});

test('should omit undefined JSON query parameter', (done) => {
Swagger('http://localhost:8000/sample-query-parameter-json.yaml')
.then((client) => {
expect(client).toBeTruthy();
expect(Object.keys(client.apis).length).toBe(1);
expect(client.apis.default).toBeTruthy();
expect(client.apis.default.test).toBeTruthy();

return client.apis.default.test({}).catch((err) => {
expect(err.status).toBe(404);
done();
});
})
.catch((err) => done(err));
});
});
23 changes: 23 additions & 0 deletions test/data/sample-query-parameter-json.yaml
@@ -0,0 +1,23 @@
openapi: 3.0.1
info:
version: "0.0.1"
title: Test Defect
description: >-
A client side JS error occurred when using the `TryItOut` feature of the SwaggerUI. It would try fail
to formatKeyValuePairs on a JSON query parameter.
paths:
/test:
get:
operationId: test
parameters:
- name: jsonArray
in: query
description: |
Query parameter formatted as JSON
content:
application/json:
schema:
type: array
responses:
'200':
description: OK

0 comments on commit 285ade8

Please sign in to comment.