-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[8.x] Support JSON encoding Stringable #36012
Conversation
Isn't this a breaking change, for someone already serialising it? |
@GrahamCampbell Hmm, maybe? I don't see how though. Can you give an example of what you're thinking? If they are already JSON serializing it, they'll be getting an empty object ( |
There's a small breaking change if you return a Stringable response directly, so it was merged only on 9.x branch. #35680 It would be nice if we cast it directly on Response object to string so it can be merged as none breaking change. |
Even though it is a breaking change - it seems silly to depend on an empty object being returned when you return a string that isn't empty? |
Yeh, this is probably fine, on balance. |
Thanks for jumping in everyone. First off, sorry I didn't catch the 9.x change. I should have done a search prior to submitting this. So to be clear, the risk here is that someone may be returning On the other hand, this JSON encoding "bug" is quite likely to cause issues, and I'd love to see this fixed in 8.x, if at all possible. What I propose is that, in addition to this change, we also update the I already have this working, and can update this PR if you'd like. 👍 |
@reinink my comment just to elaborate on the current state, I also looking at porting back the PR to Laravel 8 when Taylor considering to delay Laravel 9 late last week and haven't decided on the best way to handle BC on response (if we want to handle it). Few things in my mind right now:
|
@crynobone Understood, thanks. I think directly casting the I've updated this PR to:
|
@reinink sounds good |
@taylorotwell Awesome. The PR should be ready to go. 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent work, everyone!
Just thought I would mention that this update broke our application. We have some middleware that injects a public function handle($request, Closure $next, $guard = null)
{
$response = $next($request);
$response->setContent(
Str::of($response->getContent())->replaceLast('</body>', '<script>/* snipped */</script></body>')
);
return $response;
} What was happening was the raw HTML was being output to the browser as quoted text. We have resolved the issue by changing the to the following... public function handle($request, Closure $next, $guard = null)
{
$response = $next($request);
$response->setContent(
str_replace('</body>', '<script>/* snipped */</script></body>', $response->getContent())
);
return $response;
} Perhaps this is a very edge case, but just thought it was worth mentioning. Thanks |
Currently if you JSON encode a
Stringable
instance, you'll get an empty object ({}
). This PR updates theStringable
class to implementJsonSerializable
, so that it's properly converted to a string when JSON encoded.I ran into this when returning JSON from a controller, and the
Stringable
was an empty object instead of a string.