bug #18954 [3.1][HttpKernel] Fix RequestDataCollector starting the session (romainneutron)

This PR was merged into the 3.1 branch.

Discussion
----------

[3.1][HttpKernel] Fix RequestDataCollector starting the session

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18951
| License       | MIT

The RequestDataCollector starts a non-started session on kernel response. This produces bug #18951
I'm not sure if this is the right fix, let's discuss it.

Commits
-------

ab62dcf [HttpKernel] Fix RequestDataCollector starting the session
This commit is contained in:
Fabien Potencier 2016-06-03 10:45:02 +02:00
commit 22aa620e80
2 changed files with 19 additions and 2 deletions

View File

@ -130,7 +130,7 @@ class RequestDataCollector extends DataCollector implements EventSubscriberInter
unset($this->controllers[$request]);
}
if (null !== $session) {
if (null !== $session && $session->isStarted()) {
if ($request->attributes->has('_redirected')) {
$this->data['redirect'] = $session->remove('sf_redirect');
}
@ -291,7 +291,7 @@ class RequestDataCollector extends DataCollector implements EventSubscriberInter
public function onKernelResponse(FilterResponseEvent $event)
{
if (!$event->isMasterRequest() || !$event->getRequest()->hasSession()) {
if (!$event->isMasterRequest() || !$event->getRequest()->hasSession() || !$event->getRequest()->getSession()->isStarted()) {
return;
}

View File

@ -11,7 +11,10 @@
namespace Symfony\Component\HttpKernel\Tests\DataCollector;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
@ -52,6 +55,20 @@ class RequestDataCollectorTest extends \PHPUnit_Framework_TestCase
$this->assertSame('application/json', $c->getContentType());
}
public function testKernelResponseDoesNotStartSession()
{
$kernel = $this->getMock(HttpKernelInterface::class);
$request = new Request();
$session = new Session(new MockArraySessionStorage());
$request->setSession($session);
$response = new Response();
$c = new RequestDataCollector();
$c->onKernelResponse(new FilterResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $response));
$this->assertFalse($session->isStarted());
}
/**
* Test various types of controller callables.
*/