Skip to content

Commit 8e211f3

Browse files
committed
fix: Fix support of array whitelist query param
1 parent d4ba6bb commit 8e211f3

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/ActionParameterHandler.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ export class ActionParameterHandler<T extends BaseDriver> {
132132
case 'date':
133133
const normalizedValue = this.normalizeStringValue(value, param.name, param.targetName);
134134
return param.isArray ? [normalizedValue] : normalizedValue;
135+
case 'array':
136+
return [value];
135137
}
136138
} else if (Array.isArray(value)) {
137139
return value.map(v => this.normalizeStringValue(v, param.name, param.targetName));

test/functional/action-params.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ describe(``, () => {
139139
multipleDateValues?: Date[];
140140
}
141141

142+
class QueryWhitelistClass {
143+
@IsArray()
144+
@IsBoolean({ each: true })
145+
@Transform(value => (Array.isArray(value) ? value.map(v => v !== 'false') : value !== 'false'))
146+
multipleBooleanValues?: boolean[];
147+
}
148+
142149
@Controller()
143150
class UserActionParamsController {
144151
@Get('/users')
@@ -239,6 +246,14 @@ describe(``, () => {
239246
return `<html><body>hello</body></html>`;
240247
}
241248

249+
@Get('/photos-params-whitelist')
250+
getPhotosWithWhitelistQuery(
251+
@QueryParams({ validate: { whitelist: true, forbidNonWhitelisted: true } }) query: QueryWhitelistClass
252+
): string {
253+
queryParams3 = query;
254+
return `<html><body>hello</body></html>`;
255+
}
256+
242257
@Get('/photos-with-required')
243258
getPhotosWithIdRequired(@QueryParam('limit', { required: true }) limit: number): string {
244259
queryParamLimit = limit;
@@ -594,6 +609,14 @@ describe(``, () => {
594609
expect(queryParams1.multipleDateValues).toEqual([new Date('2017-02-01T01:00:00Z')]);
595610
});
596611

612+
it("@QueryParams should give a proper values from request's query with validate whitelist option on", async () => {
613+
expect.assertions(3);
614+
const response = await axios.get('/photos-params-whitelist?multipleBooleanValues=false');
615+
expect(response.status).toEqual(HttpStatusCodes.OK);
616+
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
617+
expect(queryParams3.multipleBooleanValues).toEqual([false]);
618+
});
619+
597620
it("@QueryParams should give a proper values from request's query parameters with nested json", async () => {
598621
expect.assertions(13);
599622
const response = await axios.get(

0 commit comments

Comments
 (0)