-
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
[10.x] backport #52188 #52293
[10.x] backport #52188 #52293
Conversation
Thanks! |
We have a big issue with this PR, since this update, we are getting a
reverting this PR totally fix this issue The issue is coming from casting an empty column to array : how to reproduce :
so catching this in this function :
is causing the exception to be thrown |
That means there's a failed json_encode/decode earlier in the application. Could be due to #52204 which I have not backported, but I can do that real quick |
I edited my message, but it seems that is indeed from the same issue, but I don't think the JSON_THROW_ON_ERROR should be directly into the Model class |
In this PR you completly disable the option to give 0 as
Anyway, this a breaking change, and this should not be in a minor update in laravel 10 |
No, this is correct---the model was throwing an exception anyway if there was a json error. The issue that I fixed was that the exception was being thrown for a previous json error even when the model was successfully converted to json. |
you can try it doing this in a controller :
it was not crashing before, and crash now |
This is because the system relies on global state which is a bad idea and very brittle as you can see. This PR specifically fixed a bug where a I've opened a PR which backports the request json_decode issue (#52389), however, the code really needs to be cleaned up to not rely on global state as this error you are experiencing could still happen if other json errors occur earlier in the application |
I aggre that it should throw an error when dealing with faulty Json, but this is not my point. • This change is a breaking change and cause any model using an empty array casted column to crash the app At least it should be notted as a breaking change in the upgrade to Laravel 10 Documentation @taylorotwell what is your opinion on the matter ? |
well, just without your changes, try that :
Before, the user would be return, and now, you have an exception making the app to crash |
BLUFAn exception is being thrown when returning a Model from a controller that has an array-casted attribute set to an empty string (an empty string is not valid json). First and foremost, this is data integrity issue---database columns that are cast to However, it would be possible to prevent this parse error by converting falsely json values to a json null ( // src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
public function fromJson($value, $asObject = false)
{
- return Json::decode($value ?? '', ! $asObject);
+ return Json::decode($value ?: 'null', ! $asObject);
} AnalysisI've stepped through the framework with a debugger and here's what I've found: The exception is being thrown by the framework/src/Illuminate/Http/JsonResponse.php Lines 73 to 92 in 763b942
A framework/src/Illuminate/Database/Eloquent/Model.php Lines 1648 to 1657 in 763b942
framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php Lines 734 to 740 in 763b942
Otherwise the attribute is casted to an array: framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php Lines 768 to 770 in 763b942
framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php Lines 1272 to 1275 in 763b942
Which leads to framework/src/Illuminate/Database/Eloquent/Casts/Json.php Lines 32 to 37 in 763b942
|
I get the reasoning about that it shouldn't have worked, but you've proved yourself that it is a breaking change even if it shouldn't have worked before. Even I have apps that still run on old MySQL 5 with TEXT fields for json and some have empty data for good or bad reasons, and the argument that your data is wrong does not make it any different that a change broke what was working before without any notice or warning. So this should either go all the way to fix it or not fix it at all in a minor release. |
Thank you all :) |
Hello!
We still have an application on 10.x and are suffering the same failures referenced in the original issue. This backports #52188 to 10.x
Thanks!