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

@json does not work when there is a quoted value with commas in it followed by a comma separated list #46908

Closed
craigpipeandpiper opened this issue Apr 28, 2023 · 4 comments

Comments

@craigpipeandpiper
Copy link

Laravel Version

9.50.2

PHP Version

8.1.11

Database Driver & Version

MySQL 8.0.32

Description

Using the @json function can cause malformed PHP in the compiled blade template.

My previous issue was closed as too much emphasis seemed to be placed on the best practice of what i had distilled into a proof of concept. Attached is a more realistic example of how the @JSON function can be broken that is more in line with how views are actually used in the real world. I have managed to reproduce this several ways, if you need more examples please let me know.

In the example below $titles is an array set from the view, $names is also an array set by the view, "last name, first name" is a constant used to define parts of a JSON blob used for AJAX.

Any comma in the quoted first value causes the bug.

The error output from laravel is:

Unclosed '[' does not match ')'

Steps To Reproduce

<script>
    window.callProcessNamesList(@json(["last name, first name", $titles, $names]));
</script>
@driesvints
Copy link
Member

The @json directive really isn't meant to be used like that. It's best to assign the contents to a variable and then pass it to the directive.

@craigpipeandpiper
Copy link
Author

uh, this is exactly what i am doing...
Please stop closing and dismissing bugs without looking into them. This is an unhealthy obsession with keeping levels of bugs down to zero.

@driesvints
Copy link
Member

There is no bug here. If you can come up with a solution for the Blade compiler to allow this then feel free to send in a PR.

@craigpipeandpiper
Copy link
Author

I shall do this, in my own free time.

The blade compiler should never generate malformed php, full stop. Regardless of if its the "one true way" or not, it should generate correctly formed PHP or generate a proper exception describing why it is not valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants