-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[11.x] fix: Model/JsonResource::toJson should not fail with prior json errors #52188
Conversation
Thanks @taylorotwell! |
I think this broke things for me in the following way: Some of my models have a |
@lee-van-oetz, I don't think this caused your failures:
|
Apologies,
I've verified the issue I'm having is caused by adding |
@lee-van-oetz, then you likely have a rogue, failed json encode/decode call earlier in application. When adding JSON_THROW_ON_ERROR to json_encode it does not reset Note that the rogue json failure could be the same one as #52204 did not backport that to 10.x |
Since this update, I must explicitly add "string" as the return type from any controller method that returns a model in my API. exception: "InvalidArgumentException"
|
@lotestudio, make sure you're on the latest 10.x version---there was another fix that prevented null model attributes from triggering the error |
@calebdw, I'm with v10.48.20 which is latest. |
This is a breaking change really. Anyone having The https://github.com/laravel/framework/blob/11.x/src/Illuminate/Http/JsonResponse.php#L82-L91 |
As I mentioned in another PR (#52293 (comment)):
I did not change the behavior of these methods, they still throw an exception if there's an error encountered when encoding json. I fixed a bug that was causing the exception to be thrown even when the encode was successful. The solution is to remove the dependency on global state elsewhere in the application, not to revert this PR. If you can post a minimum reproducible example then I can submit another PR to fix the issue. |
@calebdw semantics but imho you did change the behaviour of the function and limited the control of its only argument. It impacted a global state before and now it doesn't anymore. Whether this is breaking or not, considering the rather ambiguous impact of that option (true, this was rather difficult to predict!), this was good material for an upgrade guide. It's no longer a factor for our project but I guess you could introduce something similar like the empty string fix to lower the impact by introducing this before else: json_decode('{}', $options); |
We are seeing errors as well out of the blue on a Laravel 10 project with no code changes. Something backwards breaking occur here. I'm digging a bit deeper now trying to figure out why we are getting |
We encountered similar errors after applying this update. The issue stemmed from our use of To resolve this, we implemented changes similar to those in PR #52415, which prevent the decoding of |
Hello!
This PR updates the Model/JsonResource::toJson methods so they succeed if there's been prior json errors. It does this by using the
JSON_THROW_ON_ERROR
flag in conjunction with a try/catch.The problem
Out of the blue, a
Js::from($model)
call in a blade template started throwingJsonEncodingException
s when the model in question hasn't been updated in two years. It turns out thatJs::from
passes theJSON_THROW_ON_ERROR
flag to theModel::toJson
method which was in turn just checkingjson_last_error()
after thejson_encode
call.However, according to the PHP docs the
json_last_error
method:As the
Model::toJson
method was now being called with theJSON_THROW_ON_ERROR
flag the call tojson_encode
did not reset the global json state and the method was failing from a json call introduced in a middleware.Thanks!