diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 62fad41cd828e..22ca97b546528 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -387,7 +387,7 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0, } } - if (\is_string($value) && '' !== $value && '&' === $value[0] && Parser::preg_match(Parser::REFERENCE_PATTERN, $value, $matches)) { + if (!$isQuoted && \is_string($value) && '' !== $value && '&' === $value[0] && Parser::preg_match(Parser::REFERENCE_PATTERN, $value, $matches)) { $references[$matches['ref']] = $matches['value']; $value = $matches['value']; } @@ -521,6 +521,7 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a } break; default: + $isValueQuoted = \in_array($mapping[$i], ['"', "'"]); $value = self::parseScalar($mapping, $flags, [',', '}', "\n"], $i, null === $tag, $references); // Spec: Keys MUST be unique; first one wins. // Parser cannot abort this mapping earlier, since lines @@ -529,7 +530,7 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a if ('<<' === $key) { $output += $value; } elseif ($allowOverwrite || !isset($output[$key])) { - if (\is_string($value) && '' !== $value && '&' === $value[0] && Parser::preg_match(Parser::REFERENCE_PATTERN, $value, $matches)) { + if (!$isValueQuoted && \is_string($value) && '' !== $value && '&' === $value[0] && Parser::preg_match(Parser::REFERENCE_PATTERN, $value, $matches)) { $references[$matches['ref']] = $matches['value']; $value = $matches['value']; } diff --git a/src/Symfony/Component/Yaml/Tests/InlineTest.php b/src/Symfony/Component/Yaml/Tests/InlineTest.php index 0b76d91e4c92d..2d09d678bc701 100644 --- a/src/Symfony/Component/Yaml/Tests/InlineTest.php +++ b/src/Symfony/Component/Yaml/Tests/InlineTest.php @@ -905,4 +905,22 @@ public function ideographicSpaceProvider(): array ["'a b'", 'a b'], ]; } + + public function testParseQuotedReferenceLikeStringsInMapping() + { + $yaml = <<assertSame(['foo' => '&foo', 'bar' => '&bar'], Inline::parse($yaml)); + } + + public function testParseQuotedReferenceLikeStringsInSequence() + { + $yaml = <<assertSame(['&foo', '&bar'], Inline::parse($yaml)); + } }