From: Michał Kędrzyński Date: Fri, 30 Jun 2023 08:36:52 +0000 (+0200) Subject: fix: handle undefined path in router resolve X-Git-Tag: v4.3.0~39 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0ec4862ff54207c1c207027ed240e24efcfeb6cf;p=thirdparty%2Fvuejs%2Frouter.git fix: handle undefined path in router resolve --- diff --git a/packages/router/__tests__/router.spec.ts b/packages/router/__tests__/router.spec.ts index a60db539..951f2f0d 100644 --- a/packages/router/__tests__/router.spec.ts +++ b/packages/router/__tests__/router.spec.ts @@ -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() diff --git a/packages/router/src/matcher/index.ts b/packages/router/src/matcher/index.ts index 6c9a35d3..f7668dd5 100644 --- a/packages/router/src/matcher/index.ts +++ b/packages/router/src/matcher/index.ts @@ -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 diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index 1d6c6697..e5beb1f1 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -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 &&