]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix: remove nullish params when resolving (#1814)
authorskirtle <65301168+skirtles-code@users.noreply.github.com>
Mon, 24 Apr 2023 07:50:07 +0000 (08:50 +0100)
committerGitHub <noreply@github.com>
Mon, 24 Apr 2023 07:50:07 +0000 (09:50 +0200)
NOTES: This change improves allows passing `null` or `undefined` to a param to completely drop. In practice, this should be better than casting it to an empty string but it should make no change if you check the absence of empty params with `!route.params.optional` rather than `route.params.optional !== ''` or any other stricter check. If you were doing stricter checks for optional params, make sure to change them to looser checks. Note that when resolving from the URL, optional parameters are always absent in the resolved object.

packages/router/__tests__/router.spec.ts
packages/router/src/router.ts

index 5b7ea7b29f7dac67bf9c84bf41d752a8058050bd..df6b371467995680eda8a55140922339cad69b77 100644 (file)
@@ -297,20 +297,25 @@ describe('Router', () => {
     expect(router.currentRoute.value).toMatchObject({ params: { p: '0' } })
   })
 
-  it('casts null/undefined params to empty strings', async () => {
+  it('removes null/undefined params', async () => {
     const { router } = await newRouter()
-    expect(
-      router.resolve({ name: 'optional', params: { p: undefined } })
-    ).toMatchObject({
-      params: {},
+
+    const route1 = router.resolve({
+      name: 'optional',
+      params: { p: undefined },
     })
-    expect(
-      router.resolve({ name: 'optional', params: { p: null } })
-    ).toMatchObject({
-      params: {},
+    expect(route1.path).toBe('/optional')
+    expect(route1.params).toEqual({})
+
+    const route2 = router.resolve({
+      name: 'optional',
+      params: { p: null },
     })
+    expect(route2.path).toBe('/optional')
+    expect(route2.params).toEqual({})
+
     await router.push({ name: 'optional', params: { p: null } })
-    expect(router.currentRoute.value).toMatchObject({ params: {} })
+    expect(router.currentRoute.value.params).toEqual({})
     await router.push({ name: 'optional', params: {} })
   })
 
index a74289854f316320289b8315f372aead4b7af090..575c0bb7b25bca48c109328adff9c56d90b597c8 100644 (file)
@@ -491,7 +491,7 @@ export function createRouter(options: RouterOptions): Router {
       }
       // pass encoded values to the matcher, so it can produce encoded path and fullPath
       matcherLocation = assign({}, rawLocation, {
-        params: encodeParams(rawLocation.params),
+        params: encodeParams(targetParams),
       })
       // current location params are decoded, we need to encode them in case the
       // matcher merges the params