Skip to content

Commit

Permalink
Merge pull request #182 from lifeomic/handle-empty-multiValueQueryStr…
Browse files Browse the repository at this point in the history
…ingParameters

fix: multiValueQueryStringParameters should be null if empty
  • Loading branch information
sternetj authored Nov 7, 2024
2 parents 9d123f2 + 1731849 commit 4f28ad8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
22 changes: 12 additions & 10 deletions src/adapters/helpers/lambdaEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ export const lambdaEvent = (config: AlphaOptions, relativeUrl?: string) => {
'http://fake',
querystringWithArraySupport,
);
const params = Object.assign({}, parts.query, config.params);
const multiValueQueryStringParameters: Record<string, any> = { ...params };
const params: Record<string, any> = Object.assign({}, parts.query, config.params);
let multiValueParams: Record<string, any[]> | null = null;

Object.keys(multiValueQueryStringParameters).forEach((key) => {
if (!Array.isArray(multiValueQueryStringParameters[key])) {
delete multiValueQueryStringParameters[key];
} else {
delete params[key];
}
});
const hasMultiValueParams = Object.values(params).some((value) => Array.isArray(value));

if (hasMultiValueParams) {
Object.entries(params).forEach(([key, value]) => {
multiValueParams = multiValueParams || {};
multiValueParams[key] = Array.isArray(value) ? value : [value];
params[key] = Array.isArray(value) ? value.join(',') : value;
});
}

const httpMethod = (config.method as string).toUpperCase();
const requestTime = new Date();
Expand Down Expand Up @@ -73,7 +75,7 @@ export const lambdaEvent = (config: AlphaOptions, relativeUrl?: string) => {
userArn: null,
},
},
multiValueQueryStringParameters,
multiValueQueryStringParameters: multiValueParams,
};

if (Buffer.isBuffer(event.body)) {
Expand Down
4 changes: 3 additions & 1 deletion test/lambda-event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ test('Can parse URLs with duplicate parameters', () => {
httpMethod: 'GET',
path: '/lifeomic/dstu3/Questionnaire',
queryStringParameters: {
_tag: 'http://lifeomic.com/fhir/questionnaire-type|survey-form,http://lifeomic.com/fhir/dataset|0bb18fef-4e2d-4b91-a623-09527265a8b3,http://lifeomic.com/fhir/primary|0343bfcf-4e2d-4b91-a623-095272783bf3',
pageSize: '25',
},
multiValueQueryStringParameters: {
Expand All @@ -39,6 +40,7 @@ test('Can parse URLs with duplicate parameters', () => {
'http://lifeomic.com/fhir/dataset|0bb18fef-4e2d-4b91-a623-09527265a8b3',
'http://lifeomic.com/fhir/primary|0343bfcf-4e2d-4b91-a623-095272783bf3',
],
pageSize: ['25'],
},
}));
assertRequestId(result);
Expand All @@ -57,7 +59,7 @@ test('Can parse URLs without duplicates', () => {
pageSize: '25',
test: 'diffValue',
},
multiValueQueryStringParameters: {},
multiValueQueryStringParameters: null,
}));
assertRequestId(result);
});
Expand Down

0 comments on commit 4f28ad8

Please sign in to comment.