From 66bb94af117f53ec08de1e8b65c8954d85d9a82f Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 20 Jun 2025 14:56:25 +0200 Subject: [PATCH] raise a parse error for duplicate keys --- UPGRADE-8.0.md | 5 +++++ src/Symfony/Component/Yaml/CHANGELOG.md | 5 +++++ src/Symfony/Component/Yaml/Parser.php | 6 +++--- src/Symfony/Component/Yaml/Tests/ParserTest.php | 7 +------ src/Symfony/Component/Yaml/composer.json | 1 - 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/UPGRADE-8.0.md b/UPGRADE-8.0.md index 18707ddb21f16..9a5c2fe09b628 100644 --- a/UPGRADE-8.0.md +++ b/UPGRADE-8.0.md @@ -307,3 +307,8 @@ VarExporter * Restrict `ProxyHelper::generateLazyProxy()` to generating abstraction-based lazy decorators; use native lazy proxies otherwise * Remove `LazyGhostTrait` and `LazyProxyTrait`, use native lazy objects instead * Remove `ProxyHelper::generateLazyGhost()`, use native lazy objects instead + +Yaml +---- + + * Remove support for parsing duplicate mapping keys whose value is `null` diff --git a/src/Symfony/Component/Yaml/CHANGELOG.md b/src/Symfony/Component/Yaml/CHANGELOG.md index 364bf66dfd68f..054d262ee1803 100644 --- a/src/Symfony/Component/Yaml/CHANGELOG.md +++ b/src/Symfony/Component/Yaml/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +8.0 +--- + + * Remove support for parsing duplicate mapping keys whose value is `null` + 7.3 --- diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index be5890829b64e..a8b741bc92f37 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -301,7 +301,7 @@ private function doParse(string $value, int $flags): mixed // But overwriting is allowed when a merge node is used in current block. if ($allowOverwrite || !isset($data[$key])) { if (!$allowOverwrite && \array_key_exists($key, $data)) { - trigger_deprecation('symfony/yaml', '7.2', 'Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated and will throw a ParseException in 8.0.', $key, $this->getRealCurrentLineNb() + 1); + throw new ParseException(\sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine); } if (null !== $subTag) { @@ -326,7 +326,7 @@ private function doParse(string $value, int $flags): mixed $data += $value; } elseif ($allowOverwrite || !isset($data[$key])) { if (!$allowOverwrite && \array_key_exists($key, $data)) { - trigger_deprecation('symfony/yaml', '7.2', 'Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated and will throw a ParseException in 8.0.', $key, $this->getRealCurrentLineNb() + 1); + throw new ParseException(\sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine); } // Spec: Keys MUST be unique; first one wins. @@ -346,7 +346,7 @@ private function doParse(string $value, int $flags): mixed // But overwriting is allowed when a merge node is used in current block. if ($allowOverwrite || !isset($data[$key])) { if (!$allowOverwrite && \array_key_exists($key, $data)) { - trigger_deprecation('symfony/yaml', '7.2', 'Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated and will throw a ParseException in 8.0.', $key, $this->getRealCurrentLineNb() + 1); + throw new ParseException(\sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine); } $data[$key] = $value; diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index 836ec23ffa582..d127c4a4758c5 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -20,8 +20,6 @@ class ParserTest extends TestCase { - use ExpectUserDeprecationMessageTrait; - private ?Parser $parser; protected function setUp(): void @@ -1059,12 +1057,9 @@ public static function getParseExceptionOnDuplicateData() return $tests; } - /** - * @group legacy - */ public function testNullAsDuplicatedData() { - $this->expectUserDeprecationMessage('Since symfony/yaml 7.2: Duplicate key "child" detected on line 4 whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated and will throw a ParseException in 8.0.'); + $this->expectException(ParseException::class); $yaml = <<=8.4", - "symfony/deprecation-contracts": "^2.5|^3.0", "symfony/polyfill-ctype": "^1.8" }, "require-dev": {