Skip to content

Commit

Permalink
Check for equality to avoid double parsing some query params
Browse files Browse the repository at this point in the history
  • Loading branch information
ckeboss committed Dec 18, 2019
1 parent 1724c63 commit 5b2b5ab
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"coveralls": "^3.0.5",
"deasync": "^0.1.16",
"express": "^4.17.1",
"get-port": "^5.0.0",
"mocha": "^6.2.0",
"morgan": "^1.9.1",
"nodemon": "^2.0.0",
Expand Down
23 changes: 23 additions & 0 deletions src/middlewares/openapi.request.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,22 @@ export class RequestValidator {
*/
parameters.parseJson.forEach(item => {
if (item.reqField === 'query' && queryParamsToValidate[item.name]) {
if (queryParamsToValidate[item.name] === req[item.reqField][item.name]) {
queryParamsToValidate[item.name] = req[item.reqField][item.name] = JSON.parse(
queryParamsToValidate[item.name],
);

/**
* The query param we parse and the query param express
* parsed are the same value, so we assign them the same
* parsed value.
*/
return;
}
/**
* They query params are not the same, so we parse the
* `queryParamsToValidate` and don't return.
*/
queryParamsToValidate[item.name] = JSON.parse(
queryParamsToValidate[item.name],
);
Expand All @@ -175,6 +191,13 @@ export class RequestValidator {
*/
parameters.parseArray.forEach(item => {
if (item.reqField === 'query' && queryParamsToValidate[item.name]) {
if (queryParamsToValidate[item.name] === req[item.reqField][item.name]) {
queryParamsToValidate[item.name] = req[item.reqField][item.name] = queryParamsToValidate[item.name].split(
item.delimiter,
);

return;
}
queryParamsToValidate[item.name] = queryParamsToValidate[item.name].split(
item.delimiter,
);
Expand Down
17 changes: 10 additions & 7 deletions test/openapi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path';
import { expect } from 'chai';
import * as request from 'supertest';
import * as getPort from 'get-port';
import { createApp } from './common/app';
import * as packageJson from '../package.json';

Expand All @@ -11,13 +12,15 @@ describe(packageJson.name, () => {
before(() => {
const apiSpecPath = path.join('test', 'resources', 'openapi.yaml');
const apiSpecJson = require('./resources/openapi.json');
return Promise.all([
createApp({ apiSpec: apiSpecPath }, 3001),
createApp({ apiSpec: apiSpecJson }, 3002),
]).then(([a1, a2]) => {
apps.push(a1);
apps.push(a2);
basePath = (<any>a1).basePath;
return Promise.all([getPort(), getPort()]).then(ports => {
return Promise.all([
createApp({ apiSpec: apiSpecPath }, ports[0]),
createApp({ apiSpec: apiSpecJson }, ports[1]),
]).then(([a1, a2]) => {
apps.push(a1);
apps.push(a2);
basePath = (<any>a1).basePath;
});
});
});

Expand Down

0 comments on commit 5b2b5ab

Please sign in to comment.