Skip to content

[11.x] Fixes Illuminate\Http\Response to output empty string if $content is set to null#53872

Merged
taylorotwell merged 5 commits into
11.xfrom
11/fix-octane-972
Dec 20, 2024
Merged

[11.x] Fixes Illuminate\Http\Response to output empty string if $content is set to null#53872
taylorotwell merged 5 commits into
11.xfrom
11/fix-octane-972

Conversation

@crynobone
Copy link
Copy Markdown
Member

  1. Symfony set the content to null for response()->noContent() https://github.com/symfony/http-foundation/blob/e88a66c3997859532bc2ddd6dd8f35aba2711744/Response.php#L246-L249
  2. This cause issue with PSR-7 HTTP Stream in Octane

fixed laravel/octane#972

`$content` is set to `null`

1. Symfony set the content to `null` for `response()->noContent()` https://github.com/symfony/http-foundation/blob/e88a66c3997859532bc2ddd6dd8f35aba2711744/Response.php#L246-L249
2. This cause issue with PSR-7 HTTP Stream in Octane

fixed laravel/octane#972

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Comment thread src/Illuminate/Http/Response.php Outdated
#[\Override]
public function getContent(): string|false
{
return transform(parent::getContent(), fn ($content) => $content, '');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not just?

return parent::getContent() ?? '';

@rodrigopedra
Copy link
Copy Markdown
Contributor

Also, for the same hash you linked the $this->setContent(null);, setContent() actually uses the null-coalescing operator to convert null to an empty string:

https://github.com/symfony/http-foundation/blob/e88a66c3997859532bc2ddd6dd8f35aba2711744/Response.php#L418-L423

Maybe the problem is something else?

@rodrigopedra
Copy link
Copy Markdown
Contributor

Does octane calls Illuminate\Http\ResponseTrait@getOriginalContent(), the Response::$original property is stored unchanged, that might be the issue.

@crynobone
Copy link
Copy Markdown
Member Author

I'm a little hesitant to follow Symfony style since their phpdoc indicate string|null while ours is mixed

Also with mixed we could potentially introduce breaking changes for application that expected that they can directly return null (maybe 🤔)

@rodrigopedra
Copy link
Copy Markdown
Contributor

I agree, but our override at the end defers to theirs

From our current code null should end up being handled by parent::setContent($content); which already coalesces null to an empty string

@crynobone crynobone marked this pull request as ready for review December 20, 2024 01:43
@taylorotwell taylorotwell merged commit 620f717 into 11.x Dec 20, 2024
@taylorotwell taylorotwell deleted the 11/fix-octane-972 branch December 20, 2024 14:56
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

Successfully merging this pull request may close these issues.

sync_worker_exec_payload: TypeError: fwrite(): Argument #2 ($data) must be of type string, null given

3 participants