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

[HttpKernel] Handle nullable callback of StreamedResponse #51972

Merged
merged 1 commit into from
Oct 12, 2023

Conversation

elementaire
Copy link
Contributor

@elementaire elementaire commented Oct 10, 2023

Q A
Branch? 6.3
Bug fix? yes
New feature? no
Deprecations? no
Tickets -
License MIT

This PR fixes a bug introduced by #51396. Having a callback in StreamResponsed is not a mandatory by design.

So the method getCallback must check it like sendContent does.

When we are dealing with cacheable ressource, only headers are sent from StreamResponsed, for example:

#[Route(path: '/download')]
public function download(Request $request): StreamedResponse
{
    // Get the last modified of a file.
    $lastModified = \DateTime::createFromFormat('d/m/Y H:i:s', '10/10/2023 13:22:00');

    $response = (new StreamedResponse())->setLastModified($lastModified);

    if ($response->isNotModified($request)) {
        return $response;
    }

    // Get the stream of a file and attach it to the callback.
    // ...

    return $response;
}

HttpKernel class have to handle it else if you obtain HTTP 500 with message Value of type null is not callable.

cc @nicolas-grekas

EDIT: found by @alexismarquis

@carsonbot carsonbot added this to the 6.3 milestone Oct 10, 2023
@OskarStark OskarStark changed the title [HttpKernel] Handle nullable callback of StreamedResponse [HttpKernel] Handle nullable callback of StreamedResponse Oct 10, 2023
@nicolas-grekas
Copy link
Member

Thank you @elementaire.

@nicolas-grekas nicolas-grekas merged commit dd6342a into symfony:6.3 Oct 12, 2023
3 of 9 checks passed
{
if (!isset($this->callback)) {
Copy link
Member

Choose a reason for hiding this comment

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

I would use null === $this->callback for consistency with our other null checks

Copy link
Member

Choose a reason for hiding this comment

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

isset() is what we use on 7.0 because the property is not declared as nullable

Copy link
Member

Choose a reason for hiding this comment

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

but this whole ticket is about fixing the fact that the callback can be null. So to me, the 7.0 type should also be changed as part of this fix.

Copy link
Member

Choose a reason for hiding this comment

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

btw, as the property is protected and not private, this is a BC break to turn it into a non-nullable potentially-uninitialized property as the way to represent an existing state.

nicolas-grekas added a commit that referenced this pull request Oct 21, 2023
…with null (xabbuh)

This PR was merged into the 7.0 branch.

Discussion
----------

[HttpFoundation] initialize protected callback property with null

| Q             | A
| ------------- | ---
| Branch?       | 7.0
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #51972 (comment)
| License       | MIT

Commits
-------

b780c12 initialize protected callback property with null
@fabpot fabpot mentioned this pull request Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants