From 0ec4862ff54207c1c207027ed240e24efcfeb6cf Mon Sep 17 00:00:00 2001 From: =?utf8?q?Micha=C5=82=20K=C4=99drzy=C5=84ski?= Date: Fri, 30 Jun 2023 10:36:52 +0200 Subject: [PATCH] fix: handle undefined path in router resolve --- packages/router/__tests__/router.spec.ts | 11 +++++++++++ packages/router/src/matcher/index.ts | 2 +- packages/router/src/router.ts | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) 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 && -- 2.39.5