]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix: handle missing parser
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 20 Aug 2025 11:30:26 +0000 (13:30 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 20 Aug 2025 11:30:26 +0000 (13:30 +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 b5e31f18186efc43508a33c1e909902a0e844b5a..9c8b63e7ac6af5923a4fef5e05fe5e9d62a7719a 100644 (file)
@@ -400,4 +400,109 @@ describe('MatcherPatternQueryParam', () => {
       expect(matcher.match({ other: 'value' })).toEqual({ missing: 'default' })
     })
   })
+
+  describe('defaultValue', () => {
+    describe('match', () => {
+      it('should fallback to PARAM_PARSER_DEFAULTS.get when parser.get is undefined', () => {
+        const matcher = new MatcherPatternQueryParam(
+          'test',
+          'test_param',
+          'value',
+          {}
+        )
+        // Should use PARAM_PARSER_DEFAULTS.get which returns value ?? null
+        expect(matcher.match({ test_param: 'value' })).toEqual({
+          test: 'value',
+        })
+        expect(matcher.match({ test_param: null })).toEqual({ test: null })
+        expect(matcher.match({})).toEqual({ test: undefined })
+      })
+
+      it('should handle array format with missing get method', () => {
+        const matcher = new MatcherPatternQueryParam(
+          'test',
+          'test_param',
+          'array',
+          {}
+        )
+        // Should use PARAM_PARSER_DEFAULTS.get which returns value ?? null
+        expect(matcher.match({ test_param: ['a', 'b'] })).toEqual({
+          test: ['a', 'b'],
+        })
+        expect(matcher.match({ test_param: 'single' })).toEqual({
+          test: ['single'],
+        })
+      })
+
+      it('should handle both format with missing get method', () => {
+        const matcher = new MatcherPatternQueryParam(
+          'test',
+          'test_param',
+          'both',
+          {}
+        )
+        // Should use PARAM_PARSER_DEFAULTS.get which returns value ?? null
+        expect(matcher.match({ test_param: 'value' })).toEqual({
+          test: 'value',
+        })
+        expect(matcher.match({ test_param: ['a', 'b'] })).toEqual({
+          test: ['a', 'b'],
+        })
+      })
+    })
+
+    describe('build', () => {
+      it('should fallback to PARAM_PARSER_DEFAULTS.set when parser.set is undefined', () => {
+        const matcher = new MatcherPatternQueryParam(
+          'test',
+          'test_param',
+          'value',
+          {}
+        )
+        // Should use PARAM_PARSER_DEFAULTS.set which converts to string
+        expect(matcher.build({ test: 'value' })).toEqual({
+          test_param: 'value',
+        })
+        expect(matcher.build({ test: 123 })).toEqual({ test_param: '123' })
+        expect(matcher.build({ test: true })).toEqual({ test_param: 'true' })
+        expect(matcher.build({ test: null })).toEqual({ test_param: null })
+        expect(matcher.build({ test: undefined })).toEqual({})
+      })
+
+      it('should handle array values with missing set method', () => {
+        const matcher = new MatcherPatternQueryParam(
+          'test',
+          'test_param',
+          'array',
+          {}
+        )
+        // Should use PARAM_PARSER_DEFAULTS.set which handles arrays
+        expect(matcher.build({ test: ['a', 'b'] })).toEqual({
+          test_param: ['a', 'b'],
+        })
+        expect(matcher.build({ test: [1, 2] })).toEqual({
+          test_param: ['1', '2'],
+        })
+        expect(matcher.build({ test: [1, true] })).toEqual({
+          test_param: ['1', 'true'],
+        })
+      })
+
+      it('should handle both format with missing set method', () => {
+        const matcher = new MatcherPatternQueryParam(
+          'test',
+          'test_param',
+          'both',
+          {}
+        )
+        // Should use PARAM_PARSER_DEFAULTS.set
+        expect(matcher.build({ test: 'value' })).toEqual({
+          test_param: 'value',
+        })
+        expect(matcher.build({ test: ['a', 'b'] })).toEqual({
+          test_param: ['a', 'b'],
+        })
+      })
+    })
+  })
 })
index 7d1b4cd1128abc32b742aa845a9ac58b3625d594..530a1c58d7933cc7107ffc71d27883e7f7241a62 100644 (file)
@@ -5,7 +5,7 @@ import {
   MatcherPattern,
   MatcherQueryParams,
 } from './matcher-pattern'
-import { ParamParser } from './param-parsers'
+import { ParamParser, PARAM_PARSER_DEFAULTS } from './param-parsers'
 
 /**
  * Handles the `query` part of a URL. It can transform a query object into an
@@ -55,7 +55,7 @@ export class MatcherPatternQueryParam<T, ParamName extends string>
           try {
             ;(value as unknown[]).push(
               // for ts errors
-              this.parser.get!(v)
+              (this.parser.get ?? PARAM_PARSER_DEFAULTS.get)(v) as T
             )
           } catch (error) {
             // we skip the invalid value unless there is no defaultValue
@@ -75,12 +75,13 @@ export class MatcherPatternQueryParam<T, ParamName extends string>
       }
     } else {
       try {
-        // FIXME: fallback to default getter
         value =
           // non existing query param should falll back to defaultValue
           valueBeforeParse === undefined
             ? valueBeforeParse
-            : this.parser.get!(valueBeforeParse)
+            : ((this.parser.get ?? PARAM_PARSER_DEFAULTS.get)(
+                valueBeforeParse
+              ) as T)
       } catch (error) {
         if (this.defaultValue === undefined) {
           throw error
@@ -103,8 +104,9 @@ export class MatcherPatternQueryParam<T, ParamName extends string>
     }
 
     return {
-      // FIXME: default setter
-      [this.queryKey]: this.parser.set!(paramValue),
+      [this.queryKey]: (this.parser.set ?? PARAM_PARSER_DEFAULTS.set)(
+        paramValue as any
+      ),
     }
   }
 }