Skip to content

[Yaml] Deprecate duplicate mapping keys containing null #57595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADE-7.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ Security
--------

* Deprecate argument `$secret` of `RememberMeToken` and `RememberMeAuthenticator`

Yaml
----

* Deprecate parsing duplicate mapping keys whose value is `null`
2 changes: 1 addition & 1 deletion src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CHANGELOG
7.2
---

* Duplicate mapping keys throw a `ParseException` even when such key is `null`
* Deprecate parsing duplicate mapping keys whose value is `null`

7.1
---
Expand Down
18 changes: 15 additions & 3 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,11 @@ private function doParse(string $value, int $flags): mixed
if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
// Spec: Keys MUST be unique; first one wins.
// But overwriting is allowed when a merge node is used in current block.
if ($allowOverwrite || !\array_key_exists($key, $data)) {
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);
}

if (null !== $subTag) {
$data[$key] = new TaggedValue($subTag, '');
} else {
Expand All @@ -320,7 +324,11 @@ private function doParse(string $value, int $flags): mixed
}

$data += $value;
} elseif ($allowOverwrite || !\array_key_exists($key, $data)) {
} 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);
}

// Spec: Keys MUST be unique; first one wins.
// But overwriting is allowed when a merge node is used in current block.
if (null !== $subTag) {
Expand All @@ -336,7 +344,11 @@ private function doParse(string $value, int $flags): mixed
$value = $this->parseValue(rtrim($values['value']), $flags, $context);
// Spec: Keys MUST be unique; first one wins.
// But overwriting is allowed when a merge node is used in current block.
if ($allowOverwrite || !\array_key_exists($key, $data)) {
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);
}

$data[$key] = $value;
} else {
throw new ParseException(\sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine);
Expand Down
16 changes: 14 additions & 2 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
namespace Symfony\Component\Yaml\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Tag\TaggedValue;
use Symfony\Component\Yaml\Yaml;

class ParserTest extends TestCase
{
use ExpectDeprecationTrait;

private ?Parser $parser;

protected function setUp(): void
Expand Down Expand Up @@ -1026,15 +1029,24 @@ public static function getParseExceptionOnDuplicateData()
EOD;
$tests[] = [$yaml, 'child_sequence', 6];

return $tests;
}

/**
* @group legacy
*/
public function testNullAsDuplicatedData()
{
$this->expectDeprecation('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.');

$yaml = <<<EOD
parent:
child:
child2:
child:
EOD;
$tests[] = [$yaml, 'child', 4];

return $tests;
Yaml::parse($yaml);
}

public function testEmptyValue()
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Yaml/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
],
"require": {
"php": ">=8.2",
"symfony/deprecation-contracts": "^2.5|^3.0",
"symfony/polyfill-ctype": "^1.8"
},
"require-dev": {
Expand Down