Skip to content

Commit 77289b9

Browse files
committed
bug #21333 [HttpKernel] Fix ArgumentValueResolver for arguments default null (chalasr)
This PR was merged into the 2.7 branch. Discussion ---------- [HttpKernel] Fix ArgumentValueResolver for arguments default null | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a If an argument has `null` as default value __and annotations are used for routing__, then it is not resolved by the ArgumentResolver e.g: ```php public function showAction(Request $request) { dump($request); // Request object } public function showAction(?Request $request) { dump($request); // Request object } public function showAction(Request $request = null) { dump($request); // null } ``` To me, the last example should have been a Request object too. Note that it is the same behavior when using `UserInterface` or whatever, I ran into this issue while trying to dump a user for a route accepting both anonymous and authenticated requests, the argument was always null while `$this->getUser()` returned the expected value. According to http://symfony.com/blog/new-in-symfony-3-2-user-value-resolver-for-controllers it should have worked through the argument. I propose to make it works as a bugfix. Any suggestion for improving the patch will be really welcomed. ping @iltar Commits ------- d3fa8a1 Avoid setting request attributes from signature arguments in AnnotationClassLoader
2 parents 5ba84ca + d3fa8a1 commit 77289b9

File tree

2 files changed

+3
-9
lines changed

2 files changed

+3
-9
lines changed

src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,6 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
138138
}
139139

140140
$defaults = array_replace($globals['defaults'], $annot->getDefaults());
141-
foreach ($method->getParameters() as $param) {
142-
if (!isset($defaults[$param->getName()]) && $param->isDefaultValueAvailable()) {
143-
$defaults[$param->getName()] = $param->getDefaultValue();
144-
}
145-
}
146141
$requirements = array_replace($globals['requirements'], $annot->getRequirements());
147142
$options = array_replace($globals['options'], $annot->getOptions());
148143
$schemes = array_merge($globals['schemes'], $annot->getSchemes());

src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,10 @@ public function testLoad($className, $routeData = array(), $methodArgs = array()
136136
array_intersect_assoc($routeData['options'], $route->getOptions()),
137137
'->load preserves options annotation'
138138
);
139-
$defaults = array_replace($methodArgs, $routeData['defaults']);
140139
$this->assertCount(
141-
count($defaults),
142-
array_intersect_assoc($defaults, $route->getDefaults()),
143-
'->load preserves defaults annotation and merges them with default arguments in method signature'
140+
count($routeData['defaults']),
141+
$route->getDefaults(),
142+
'->load preserves defaults annotation'
144143
);
145144
$this->assertEquals($routeData['schemes'], $route->getSchemes(), '->load preserves schemes annotation');
146145
$this->assertEquals($routeData['methods'], $route->getMethods(), '->load preserves methods annotation');

0 commit comments

Comments
 (0)