Skip to content

Commit 50b4bc7

Browse files
committed
[HttpKernel] Fix ArgumentValueResolver for arguments with default value null
1 parent 0dd6a90 commit 50b4bc7

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

src/Symfony/Component/HttpKernel/Controller/ArgumentResolver.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ public function getArguments(Request $request, $controller)
5252
$arguments = array();
5353

5454
foreach ($this->argumentMetadataFactory->createArgumentMetadata($controller) as $metadata) {
55+
$resolvedValue = array();
5556
foreach ($this->argumentValueResolvers as $resolver) {
57+
if (array() !== $resolvedValue) {
58+
break;
59+
}
60+
5661
if (!$resolver->supports($request, $metadata)) {
5762
continue;
5863
}
@@ -64,11 +69,8 @@ public function getArguments(Request $request, $controller)
6469
}
6570

6671
foreach ($resolved as $append) {
67-
$arguments[] = $append;
72+
$resolvedValue[] = $append;
6873
}
69-
70-
// continue to the next controller argument
71-
continue 2;
7274
}
7375

7476
$representative = $controller;
@@ -79,7 +81,11 @@ public function getArguments(Request $request, $controller)
7981
$representative = get_class($representative);
8082
}
8183

82-
throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.', $representative, $metadata->getName()));
84+
if (array() === $resolvedValue && !$metadata->isNullable()) {
85+
throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.', $representative, $metadata->getName()));
86+
}
87+
88+
$arguments = array_merge($arguments, $resolvedValue);
8389
}
8490

8591
return $arguments;

src/Symfony/Component/HttpKernel/ControllerMetadata/ArgumentMetadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct($name, $type, $isVariadic, $hasDefaultValue, $defaul
4040
$this->isVariadic = $isVariadic;
4141
$this->hasDefaultValue = $hasDefaultValue;
4242
$this->defaultValue = $defaultValue;
43-
$this->isNullable = $isNullable || null === $type || ($hasDefaultValue && null === $defaultValue);
43+
$this->isNullable = $isNullable;
4444
}
4545

4646
/**

src/Symfony/Component/HttpKernel/ControllerMetadata/ArgumentMetadataFactory.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function createArgumentMetadata($controller)
5858
}
5959

6060
foreach ($reflection->getParameters() as $param) {
61-
$arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param), $this->isVariadic($param), $this->hasDefaultValue($param), $this->getDefaultValue($param), $param->allowsNull());
61+
$arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param), $this->isVariadic($param), $this->hasDefaultValue($param), $this->getDefaultValue($param), $this->isDefaultNull($param));
6262
}
6363

6464
return $arguments;
@@ -126,4 +126,13 @@ private function getType(\ReflectionParameter $parameter)
126126
return $info[1];
127127
}
128128
}
129+
130+
private function isDefaultNull(\ReflectionParameter $parameter)
131+
{
132+
if ($this->supportsParameterType && $this->getType($parameter)) {
133+
return $parameter->allowsNull();
134+
}
135+
136+
return $this->hasDefaultValue($parameter) && null === $parameter->getDefaultValue();
137+
}
129138
}

src/Symfony/Component/HttpKernel/Tests/ControllerMetadata/ArgumentMetadataFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function testNullableTypesSignature()
121121
new ArgumentMetadata('foo', 'string', false, false, null, true),
122122
new ArgumentMetadata('bar', \stdClass::class, false, false, null, true),
123123
new ArgumentMetadata('baz', 'string', false, true, 'value', true),
124-
new ArgumentMetadata('mandatory', null, false, false, null, true),
124+
new ArgumentMetadata('mandatory', null, false, false, null, false),
125125
), $arguments);
126126
}
127127

0 commit comments

Comments
 (0)