Skip to content

Commit 1fc5f12

Browse files
committed
fix: use hasOwnProperty instead of truthiness check for parameter presence in _parse
The check `!params[segments[0].name]` used truthiness to determine if a parameter key was present, causing falsy-but-valid values like `0` to be treated as missing. For example, `route('posts.show', { post: 0 })` would incorrectly wrap params to `{ post: { post: 0 } }` because `!0` is `true`. Replaced with `!params.hasOwnProperty(segments[0].name)` to check for key presence instead.
1 parent f92110a commit 1fc5f12

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/js/Router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export default class Router extends String {
263263
);
264264
} else if (
265265
segments.length === 1 &&
266-
!params[segments[0].name] &&
266+
!params.hasOwnProperty(segments[0].name) &&
267267
(params.hasOwnProperty(Object.values(route.bindings)[0]) || params.hasOwnProperty('id'))
268268
) {
269269
// If there is only one template segment and `params` is an object, that object is

tests/js/route.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,10 @@ describe('route()', () => {
593593
expect(route('posts.update', 0)).toBe('https://ziggy.dev/posts/0');
594594
});
595595

596+
test('can handle an object parameter with a falsy value for the segment name', () => {
597+
expect(route('posts.show', { post: 0 })).toBe('https://ziggy.dev/posts/0');
598+
});
599+
596600
test('can accept a custom Ziggy configuration object', () => {
597601
const config = {
598602
url: 'http://notYourAverage.dev',

0 commit comments

Comments
 (0)