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

Ensure Ziggy.defaults is always an object #371

Merged
merged 4 commits into from
Nov 21, 2020
Merged

Conversation

bakerkretzmar
Copy link
Collaborator

This PR fixes #366 by ensuring that the JavaScript Ziggy.defaults property is always an object, even when it's empty.

The bug in #366 happens because previously, when defaults was an empty PHP array, it was encoded as an empty JavaScript array too. JavaScript arrays of course have several built-in methods, like shift() and reduce(), and so if you had a route parameter name that matched one of those methods, and you tried to access it on an array, it would return that method:

const defaults = [];
const name1 = 'users';
const name2 = 'shift';

!!defaults[name1]; // false, because it evaluates to defaults['users'] which doesn't exist
!!defaults[name2]; // true, because it evaluates to defaults['shift'] which DOES exist and is a function

If there are default parameters, defaults will be an object, which avoids this issue, but if there aren't, it would have been an array, so any route parameter names matching built-in JavaScript array methods would cause errors like this.

Copy link

@sbine sbine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏆

@bakerkretzmar bakerkretzmar merged commit 38460b4 into main Nov 21, 2020
@bakerkretzmar bakerkretzmar deleted the jbk/param-called-shift branch November 21, 2020 01:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

route() is returning a function
2 participants