From a2ad4fa8b391a117d34a2902336c4fc09b39941d Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 8 Jan 2021 10:29:29 +0100 Subject: [PATCH] fix handling float-like key attribute values --- .../Config/Definition/PrototypedArrayNode.php | 4 +++ .../Tests/Definition/NormalizationTest.php | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Symfony/Component/Config/Definition/PrototypedArrayNode.php b/src/Symfony/Component/Config/Definition/PrototypedArrayNode.php index 7f52b57997..68bb270172 100644 --- a/src/Symfony/Component/Config/Definition/PrototypedArrayNode.php +++ b/src/Symfony/Component/Config/Definition/PrototypedArrayNode.php @@ -237,6 +237,10 @@ class PrototypedArrayNode extends ArrayNode } elseif (isset($v[$this->keyAttribute])) { $k = $v[$this->keyAttribute]; + if (\is_float($k)) { + $k = var_export($k, true); + } + // remove the key attribute when required if ($this->removeKeyAttribute) { unset($v[$this->keyAttribute]); diff --git a/src/Symfony/Component/Config/Tests/Definition/NormalizationTest.php b/src/Symfony/Component/Config/Tests/Definition/NormalizationTest.php index 931cf987ea..948ca275c6 100644 --- a/src/Symfony/Component/Config/Tests/Definition/NormalizationTest.php +++ b/src/Symfony/Component/Config/Tests/Definition/NormalizationTest.php @@ -201,6 +201,32 @@ class NormalizationTest extends TestCase $this->assertNormalized($tree, $data, $data); } + public function testFloatLikeValueAsMapKeyAttribute() + { + $tree = (new TreeBuilder('root')) + ->getRootNode() + ->useAttributeAsKey('number') + ->arrayPrototype() + ->children() + ->scalarNode('foo')->end() + ->end() + ->end() + ->end() + ->buildTree() + ; + + $this->assertNormalized($tree, [ + [ + 'number' => 3.0, + 'foo' => 'bar', + ], + ], [ + '3.0' => [ + 'foo' => 'bar', + ], + ]); + } + public static function assertNormalized(NodeInterface $tree, $denormalized, $normalized) { self::assertSame($normalized, $tree->normalize($denormalized));