Skip to content

Commit 380c268

Browse files
committed
bug #20621 [HttpKernel] Fix exception when serializing request attributes (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
2 parents 1df5e7f + 2e404d0 commit 380c268

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/Symfony/Bundle/WebProfilerBundle/Controller/RouterController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private function getTraces(RequestDataCollector $request, $method)
8787
$traceRequest = Request::create(
8888
$request->getPathInfo(),
8989
$request->getRequestServer(true)->get('REQUEST_METHOD'),
90-
$request->getRequestAttributes(true)->all(),
90+
array(),
9191
$request->getRequestCookies(true)->all(),
9292
array(),
9393
$request->getRequestServer(true)->all()

src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function collect(Request $request, Response $response, \Exception $except
127127
if ('request_headers' === $key || 'response_headers' === $key) {
128128
$value = array_map(function ($v) { return isset($v[1]) ? $v : $v[0]; }, $value);
129129
}
130-
if ('request_server' !== $key && 'request_attributes' !== $key && 'request_cookies' !== $key) {
130+
if ('request_server' !== $key && 'request_cookies' !== $key) {
131131
$this->data[$key] = array_map(array($this, 'cloneVar'), $value);
132132
}
133133
}
@@ -190,9 +190,9 @@ public function getRequestCookies($raw = false)
190190
return new ParameterBag($raw ? $this->data['request_cookies'] : array_map(array($this, 'cloneVar'), $this->data['request_cookies']));
191191
}
192192

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

198198
public function getResponseHeaders()
@@ -271,7 +271,17 @@ public function getIdentifier()
271271
*/
272272
public function getRouteParams()
273273
{
274-
return isset($this->data['request_attributes']['_route_params']) ? array_map(array($this, 'cloneVar'), $this->data['request_attributes']['_route_params']) : array();
274+
if (!isset($this->data['request_attributes']['_route_params'])) {
275+
return array();
276+
}
277+
278+
$data = $this->data['request_attributes']['_route_params'];
279+
$params = array();
280+
foreach ($data->getRawData()[1] as $k => $v) {
281+
$params[$k] = $data->seek($k);
282+
}
283+
284+
return $params;
275285
}
276286

277287
/**

src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testCollect()
4747
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestQuery());
4848
$this->assertSame('html', $c->getFormat());
4949
$this->assertEquals('foobar', $c->getRoute());
50-
$this->assertEquals(array('name' => $cloner->cloneVar('foo')), $c->getRouteParams());
50+
$this->assertEquals(array('name' => $cloner->cloneVar(array('name' => 'foo'))->seek('name')), $c->getRouteParams());
5151
$this->assertSame(array(), $c->getSessionAttributes());
5252
$this->assertSame('en', $c->getLocale());
5353
$this->assertEquals($cloner->cloneVar($request->attributes->get('resource')), $attributes->get('resource'));

0 commit comments

Comments
 (0)