Skip to content

Commit

Permalink
fix(param-decorator): @query take first value by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorikairox committed Dec 25, 2023
1 parent 9c6c339 commit f04cad2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ out
gen
node_modules
coverage
deno.lock
21 changes: 21 additions & 0 deletions spec/method-param-decorator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class SimpleController {
return myvalue;
}

@Get('/query/myvalue/default')
simpleGetMyValueDefault(@Query('myvalue') myvalue: string[]) {
return myvalue;
}

@Get('/query/myvalue/last')
simpleGetMyValueLast(@Query('myvalue', { value: 'last' }) myvalue: string) {
return myvalue;
Expand Down Expand Up @@ -182,6 +187,22 @@ Deno.test(`@Query decorator with value 'first' to return the first value for a g
await app.close();
});

Deno.test(`@Query decorator with value and no option to return the first value for a given query parameter`, async () => {
await app.init(MyModule);
const listenEvent = await app.listen(0);

const res = await fetch(
`http://localhost:${listenEvent.port}/query/myvalue/default?myvalue=foo&myvalue=bar`,
{
method: 'GET',
},
);
const text = await res.text();
assertEquals(text, `foo`);

await app.close();
});

Deno.test(`@Query decorator with no key and value 'array' to return all values of all query parameters`, async () => {
await app.init(MyModule);
const listenEvent = await app.listen(0);
Expand Down
6 changes: 3 additions & 3 deletions src/router/controller/params/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,19 @@ function formatQueryValue(
queryValue: string[] | undefined,
value: 'first' | 'last' | 'array' | undefined,
) {
if (!queryValue || !value) {
if (!queryValue) {
return undefined;
}

switch (value || 'last') {
switch (value) {
case 'first':
return queryValue[0];
case 'last':
return queryValue[queryValue.length - 1];
case 'array':
return queryValue;
default:
return undefined;
return queryValue[0];
}
}

Expand Down

0 comments on commit f04cad2

Please sign in to comment.