8000 [HttpFoundation] deprecate call to Request::getSession() when Request::hasSession() returns false by fmata · Pull Request #26564 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpFoundation] deprecate call to Request::getSession() when Request::hasSession() returns false #26564

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

Merged
merged 1 commit into from
Mar 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpFoundation] deprecate call to Request::getSession() when Request…
…::hasSession() returns false
  • Loading branch information
fmata committed Mar 17, 2018
commit 4110d57da13780bb96c0ad8b0bc73c4bc5a51a93
2 changes: 1 addition & 1 deletion UPGRADE-4.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ HttpFoundation
--------------

* Passing the file size to the constructor of the `UploadedFile` class is deprecated.

* The `getClientSize()` method of the `UploadedFile` class is deprecated. Use `getSize()` instead.
* Deprecated `Symfony\Component\HttpFoundation\Request::getSession()` when no session has been set. Use `Symfony\Component\HttpFoundation\Request::hasSession()` instead.

Security
--------
Expand Down
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ HttpFoundation

* The `$size` argument of the `UploadedFile` constructor has been removed.
* The `getClientSize()` method of the `UploadedFile` class has been removed.
* The `getSession()` method of the `Request` class throws an exception when session is null.
Copy link
Contributor

Choose a reason for hiding this comment

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

mentioning the deprecation in UPGRADE-4.1.md and HttpFoundation/CHANGELOG.md is missing

Copy link
Contributor Author
@fmata fmata Mar 17, 2018

Choose a reason for hiding this comment

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

Done. I will send a PR (edit : #26580) to update the Github PR template to help contributors to do such things, thanks.


Security
--------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ public function toolbarAction(Request $request, $token)
throw new NotFoundHttpException('The profiler must be enabled.');
}

$session = $request->getSession();

if (null !== $session && $session->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
if ($request->hasSession() && ($session = $request->getSession()) && $session->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
// keep current flashes for one more request if using AutoExpireFlashBag
$session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
}
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* The `get()` method of the `AcceptHeader` class now takes into account the
`*` and `*/*` default values (if they are present in the Accept HTTP header)
when looking for items.
* deprecated `Request::getSession()` when no session has been set. Use `Request::hasSession()` instead.

4.0.0
-----
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,11 @@ public function getSession()
$this->setSession($session = $session());
}

if (null === $session) {
@trigger_error(sprintf('Calling "%s()" when no session has been set is deprecated since Symfony 4.1 and will throw an exception in 5.0. Use "hasSession()" instead.', __METHOD__), E_USER_DEPRECATED);
// throw new \BadMethodCallException('Session has not been set');
}

return $session;
}

Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
10000
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,15 @@ public function testGetSession()
$this->assertObjectHasAttribute('attributeName', $session);
}

/**
* @group legacy
* @expectedDeprecation Calling "Symfony\Component\HttpFoundation\Request::getSession()" when no session has been set is deprecated since Symfony 4.1 and will throw an exception in 5.0. Use "hasSession()" instead.
*/
public function testGetSessionNullable()
{
(new Request())->getSession();
}

public function testHasPreviousSession()
{
$request = new Request();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ final class SessionValueResolver implements ArgumentValueResolverInterface
*/
public function supports(Request $request, ArgumentMetadata $argument)
{
if (!$request->hasSession()) {
return false;
}

$type = $argument->getType();
if (SessionInterface::class !== $type && !is_subclass_of($type, SessionInterface::class)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\Security\Guard\Authenticator;

use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -42,7 +41,7 @@ abstract protected function getLoginUrl();
*/
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
if ($request->getSession() instanceof SessionInterface) {
if ($request->hasSession()) {
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
}

Expand Down
0