Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove nullish params when resolving #1814

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions packages/router/__tests__/router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {} })
})

Expand Down
2 changes: 1 addition & 1 deletion packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down