]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix: handle undefined path in router resolve
authorMichał Kędrzyński <kedrzu@gmail.com>
Fri, 30 Jun 2023 08:36:52 +0000 (10:36 +0200)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Fri, 15 Dec 2023 14:31:15 +0000 (15:31 +0100)
packages/router/__tests__/router.spec.ts
packages/router/src/matcher/index.ts
packages/router/src/router.ts

index a60db5393ebfda912ee00f4d0d99d2e495f93d44..951f2f0db829454f05a1a241b8c2deb3a3b055fb 100644 (file)
@@ -319,6 +319,17 @@ describe('Router', () => {
     await router.push({ name: 'optional', params: {} })
   })
 
+  it('handles undefined path', async () => {
+    const { router } = await newRouter()
+
+    const route1 = router.resolve({
+      path: undefined,
+      params: { p: 'a' },
+    })
+    expect(route1.path).toBe('/')
+    expect(route1.params).toEqual({ p: 'a' })
+  })
+
   it('removes null/undefined optional params when current location has it', async () => {
     const { router } = await newRouter()
 
index 6c9a35d3510c238b25a2b97e7aa09de9a5e35084..f7668dd5e51fb2c63f42ae0831bf5ac8c0029fb3 100644 (file)
@@ -290,7 +290,7 @@ export function createRouterMatcher(
       )
       // throws if cannot be stringified
       path = matcher.stringify(params)
-    } else if ('path' in location) {
+    } else if ('path' in location && location.path != null) {
       // no need to resolve the path with the matcher as it was provided
       // this also allows the user to control the encoding
       path = location.path
index 1d6c669756acbc3eaf279dd97529ebfee6ff5f09..e5beb1f1b6ccd09e940a2a7e2f5b72084c5344e0 100644 (file)
@@ -463,7 +463,7 @@ export function createRouter(options: RouterOptions): Router {
     let matcherLocation: MatcherLocationRaw
 
     // path could be relative in object as well
-    if ('path' in rawLocation) {
+    if ('path' in rawLocation && rawLocation.path != null) {
       if (
         __DEV__ &&
         'params' in rawLocation &&