Skip to content

Commit

Permalink
bug #20621 [HttpKernel] Fix exception when serializing request attrib…
Browse files Browse the repository at this point in the history
…utes (nicolas-grekas)

This PR was merged into the 3.2 branch.

Discussion
----------

[HttpKernel] Fix exception when serializing request attributes

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Serializing request attributes obviously fails easily since once can put anything there (I've just got an "Exception: Serialization of 'Closure' is not allowed").
Yet, we don't need attributes when forging a request to feed the TraceableUrlMatcher in RouterController.
Well, technically one could for sure register a listener before the router to add an attribute to the request, then use that in the url matcher.
But, it makes no sense. And if it were to have, the profiler is already broken in this respect because in e.g. 3.1, the attribute array that is used here has already been processed by the ValueExporter. So the original values have already been altered.

Let's just handle request attributes as all the other collected data: using var dumper.

Commits
-------

2e404d0 [HttpKernel] Fix exception when serializing request attributes
  • Loading branch information
fabpot committed Nov 24, 2016
2 parents 1df5e7f + 2e404d0 commit 380c268
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
Expand Up @@ -87,7 +87,7 @@ private function getTraces(RequestDataCollector $request, $method)
$traceRequest = Request::create(
$request->getPathInfo(),
$request->getRequestServer(true)->get('REQUEST_METHOD'),
$request->getRequestAttributes(true)->all(),
array(),
$request->getRequestCookies(true)->all(),
array(),
$request->getRequestServer(true)->all()
Expand Down
Expand Up @@ -127,7 +127,7 @@ public function collect(Request $request, Response $response, \Exception $except
if ('request_headers' === $key || 'response_headers' === $key) {
$value = array_map(function ($v) { return isset($v[1]) ? $v : $v[0]; }, $value);
}
if ('request_server' !== $key && 'request_attributes' !== $key && 'request_cookies' !== $key) {
if ('request_server' !== $key && 'request_cookies' !== $key) {
$this->data[$key] = array_map(array($this, 'cloneVar'), $value);
}
}
Expand Down Expand Up @@ -190,9 +190,9 @@ public function getRequestCookies($raw = false)
return new ParameterBag($raw ? $this->data['request_cookies'] : array_map(array($this, 'cloneVar'), $this->data['request_cookies']));
}

public function getRequestAttributes($raw = false)
public function getRequestAttributes()
{
return new ParameterBag($raw ? $this->data['request_attributes'] : array_map(array($this, 'cloneVar'), $this->data['request_attributes']));
return new ParameterBag($this->data['request_attributes']);
}

public function getResponseHeaders()
Expand Down Expand Up @@ -271,7 +271,17 @@ public function getIdentifier()
*/
public function getRouteParams()
{
return isset($this->data['request_attributes']['_route_params']) ? array_map(array($this, 'cloneVar'), $this->data['request_attributes']['_route_params']) : array();
if (!isset($this->data['request_attributes']['_route_params'])) {
return array();
}

$data = $this->data['request_attributes']['_route_params'];
$params = array();
foreach ($data->getRawData()[1] as $k => $v) {
$params[$k] = $data->seek($k);
}

return $params;
}

/**
Expand Down
Expand Up @@ -47,7 +47,7 @@ public function testCollect()
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestQuery());
$this->assertSame('html', $c->getFormat());
$this->assertEquals('foobar', $c->getRoute());
$this->assertEquals(array('name' => $cloner->cloneVar('foo')), $c->getRouteParams());
$this->assertEquals(array('name' => $cloner->cloneVar(array('name' => 'foo'))->seek('name')), $c->getRouteParams());
$this->assertSame(array(), $c->getSessionAttributes());
$this->assertSame('en', $c->getLocale());
$this->assertEquals($cloner->cloneVar($request->attributes->get('resource')), $attributes->get('resource'));
Expand Down

0 comments on commit 380c268

Please sign in to comment.