]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix: take last value in query params
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 29 Aug 2025 13:51:34 +0000 (15:51 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 29 Aug 2025 13:51:34 +0000 (15:51 +0200)
packages/router/src/experimental/route-resolver/matchers/matcher-pattern-query.spec.ts
packages/router/src/experimental/route-resolver/matchers/matcher-pattern-query.ts

index 95f7ba0cd091df9e84e1db348c964c3a5d8162a4..a9ad79b729f1e1bfaa290a8a91b70a076e0ed009 100644 (file)
@@ -19,7 +19,7 @@ describe('MatcherPatternQueryParam', () => {
       expect(matcher.match({ user_id: 'abc123' })).toEqual({ userId: 'abc123' })
     })
 
-    it('takes first value from array', () => {
+    it('takes last value from array', () => {
       const matcher = new MatcherPatternQueryParam(
         'userId',
         'user_id',
@@ -27,7 +27,7 @@ describe('MatcherPatternQueryParam', () => {
         PARAM_PARSER_DEFAULTS
       )
       expect(matcher.match({ user_id: ['first', 'second'] })).toEqual({
-        userId: 'first',
+        userId: 'second',
       })
     })
 
index 0502f027837902883293ec81c503b8e355591785..559b679530ffd6a94b16bae370fb04ed8e6e74af 100644 (file)
@@ -32,12 +32,13 @@ export class MatcherPatternQueryParam<T, ParamName extends string>
   match(query: MatcherQueryParams): Record<ParamName, T> {
     const queryValue: MatcherQueryParamsValue | undefined = query[this.queryKey]
 
-    // Check if query param is missing for default value handling
-
+    // normalize the value coming from the query based on the expected format
+    // value => keep the last value if multiple
+    // array => null becomes [], single value becomes [value]
     let valueBeforeParse =
       this.format === 'value'
         ? Array.isArray(queryValue)
-          ? queryValue[0]
+          ? queryValue.at(-1)
           : queryValue
         : // format === 'array'
           Array.isArray(queryValue)