Skip to content

Commit 089275e

Browse files
[DI] Fix defaults overriding empty strings in AutowirePass
1 parent bb9cf2a commit 089275e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Compiler/AutowirePass.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ private function completeDefinition($id, Definition $definition)
9595
throw new RuntimeException(sprintf('Unable to autowire argument index %d ($%s) for the service "%s". If this is an object, give it a type-hint. Otherwise, specify this argument\'s value explicitly.', $index, $parameter->name, $id));
9696
}
9797

98-
// specifically pass the default value
99-
$arguments[$index] = $parameter->getDefaultValue();
98+
if (!array_key_exists($index, $arguments)) {
99+
// specifically pass the default value
100+
$arguments[$index] = $parameter->getDefaultValue();
101+
}
100102

101103
continue;
102104
}

Tests/Compiler/AutowirePassTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,22 @@ public function testIgnoreServiceWithClassNotExisting()
443443

444444
$this->assertTrue($container->hasDefinition('bar'));
445445
}
446+
447+
public function testEmptyStringIsKept()
448+
{
449+
$container = new ContainerBuilder();
450+
451+
$container->register('a', __NAMESPACE__.'\A');
452+
$container->register('lille', __NAMESPACE__.'\Lille');
453+
$container->register('foo', __NAMESPACE__.'\MultipleArgumentsOptionalScalar')
454+
->setAutowired(true)
455+
->setArguments(array('', ''));
456+
457+
$pass = new AutowirePass();
458+
$pass->process($container);
459+
460+
$this->assertEquals(array(new Reference('a'), '', new Reference('lille')), $container->getDefinition('foo')->getArguments());
461+
}
446462
}
447463

448464
class Foo

0 commit comments

Comments
 (0)