Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 061ac77

Browse filesBrowse files
committed
bug #40923 [Yaml] expose references detected in inline notation structures (xabbuh)
This PR was merged into the 4.4 branch. Discussion ---------- [Yaml] expose references detected in inline notation structures | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #40823 | License | MIT | Doc PR | Commits ------- 6d7d3fb expose references detected in inline notation structures
2 parents a88c7fa + 6d7d3fb commit 061ac77
Copy full SHA for 061ac77

File tree

Expand file treeCollapse file tree

4 files changed

+32
-11
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+32
-11
lines changed

‎src/Symfony/Component/Yaml/Inline.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Inline.php
+17-7Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static function initialize(int $flags, int $parsedLineNumber = null, stri
5858
*
5959
* @throws ParseException
6060
*/
61-
public static function parse(string $value = null, int $flags = 0, array $references = [])
61+
public static function parse(string $value = null, int $flags = 0, array &$references = [])
6262
{
6363
self::initialize($flags);
6464

@@ -267,7 +267,7 @@ private static function dumpNull(int $flags): string
267267
*
268268
* @throws ParseException When malformed inline YAML string is parsed
269269
*/
270-
public static function parseScalar(string $scalar, int $flags = 0, array $delimiters = null, int &$i = 0, bool $evaluate = true, array $references = [])
270+
public static function parseScalar(string $scalar, int $flags = 0, array $delimiters = null, int &$i = 0, bool $evaluate = true, array &$references = [])
271271
{
272272
if (\in_array($scalar[$i], ['"', "'"])) {
273273
// quoted scalar
@@ -343,7 +343,7 @@ private static function parseQuotedScalar(string $scalar, int &$i): string
343343
*
344344
* @throws ParseException When malformed inline YAML string is parsed
345345
*/
346-
private static function parseSequence(string $sequence, int $flags, int &$i = 0, array $references = []): array
346+
private static function parseSequence(string $sequence, int $flags, int &$i = 0, array &$references = []): array
347347
{
348348
$output = [];
349349
$len = \strlen($sequence);
@@ -385,6 +385,11 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
385385
}
386386
}
387387

388+
if (\is_string($value) && '' !== $value && '&' === $value[0] && Parser::preg_match(Parser::REFERENCE_PATTERN, $value, $matches)) {
389+
$references[$matches['ref']] = $matches['value'];
390+
$value = $matches['value'];
391+
}
392+
388393
--$i;
389394
}
390395

@@ -407,7 +412,7 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
407412
*
408413
* @throws ParseException When malformed inline YAML string is parsed
409414
*/
410-
private static function parseMapping(string $mapping, int $flags, int &$i = 0, array $references = [])
415+
private static function parseMapping(string $mapping, int $flags, int &$i = 0, array &$references = [])
411416
{
412417
$output = [];
413418
$len = \strlen($mapping);
@@ -433,14 +438,14 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
433438
// key
434439
$offsetBeforeKeyParsing = $i;
435440
$isKeyQuoted = \in_array($mapping[$i], ['"', "'"], true);
436-
$key = self::parseScalar($mapping, $flags, [':', ' '], $i, false, []);
441+
$key = self::parseScalar($mapping, $flags, [':', ' '], $i, false);
437442

438443
if ($offsetBeforeKeyParsing === $i) {
439444
throw new ParseException('Missing mapping key.', self::$parsedLineNumber + 1, $mapping);
440445
}
441446

442447
if ('!php/const' === $key) {
443-
$key .= ' '.self::parseScalar($mapping, $flags, [':'], $i, false, []);
448+
$key .= ' '.self::parseScalar($mapping, $flags, [':'], $i, false);
444449
$key = self::evaluateScalar($key, $flags);
445450
}
446451

@@ -522,6 +527,11 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
522527
if ('<<' === $key) {
523528
$output += $value;
524529
} elseif ($allowOverwrite || !isset($output[$key])) {
530+
if (\is_string($value) && '' !== $value && '&' === $value[0] && Parser::preg_match(Parser::REFERENCE_PATTERN, $value, $matches)) {
531+
$references[$matches['ref']] = $matches['value'];
532+
$value = $matches['value'];
533+
}
534+
525535
if (null !== $tag) {
526536
$output[$key] = new TaggedValue($tag, $value);
527537
} else {
@@ -548,7 +558,7 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
548558
*
549559
* @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
550560
*/
551-
private static function evaluateScalar(string $scalar, int $flags, array $references = [])
561+
private static function evaluateScalar(string $scalar, int $flags, array &$references = [])
552562
{
553563
$scalar = trim($scalar);
554564
$scalarLower = strtolower($scalar);

‎src/Symfony/Component/Yaml/Parser.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Parser.php
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Parser
2525
{
2626
public const TAG_PATTERN = '(?P<tag>![\w!.\/:-]+)';
2727
public const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
28+
public const REFERENCE_PATTERN = '#^&(?P<ref>[^ ]++) *+(?P<value>.*)#u';
2829

2930
private $filename;
3031
private $offset = 0;
@@ -158,7 +159,7 @@ private function doParse(string $value, int $flags)
158159
}
159160
$context = 'sequence';
160161

161-
if (isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
162+
if (isset($values['value']) && '&' === $values['value'][0] && self::preg_match(self::REFERENCE_PATTERN, $values['value'], $matches)) {
162163
$isRef = $matches['ref'];
163164
$this->refsBeingParsed[] = $isRef;
164165
$values['value'] = $matches['value'];
@@ -296,7 +297,7 @@ private function doParse(string $value, int $flags)
296297
$data += $parsed; // array union
297298
}
298299
}
299-
} elseif ('<<' !== $key && isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]++) *+(?P<value>.*)#u', $values['value'], $matches)) {
300+
} elseif ('<<' !== $key && isset($values['value']) && '&' === $values['value'][0] && self::preg_match(self::REFERENCE_PATTERN, $values['value'], $matches)) {
300301
$isRef = $matches['ref'];
301302
$this->refsBeingParsed[] = $isRef;
302303
$values['value'] = $matches['value'];

‎src/Symfony/Component/Yaml/Tests/InlineTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/InlineTest.php
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ public function testParseScalarWithCorrectlyQuotedStringShouldReturnString()
187187
*/
188188
public function testParseReferences($yaml, $expected)
189189
{
190-
$this->assertSame($expected, Inline::parse($yaml, 0, ['var' => 'var-value']));
190+
$references = ['var' => 'var-value'];
191+
$this->assertSame($expected, Inline::parse($yaml, 0, $references));
191192
}
192193

193194
public function getDataForParseReferences()
@@ -211,7 +212,8 @@ public function testParseMapReferenceInSequence()
211212
'b' => 'Clark',
212213
'c' => 'Brian',
213214
];
214-
$this->assertSame([$foo], Inline::parse('[*foo]', 0, ['foo' => $foo]));
215+
$references = ['foo' => $foo];
216+
$this->assertSame([$foo], Inline::parse('[*foo]', 0, $references));
215217
}
216218

217219
public function testParseUnquotedAsterisk()

‎src/Symfony/Component/Yaml/Tests/ParserTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/ParserTest.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,10 @@ public function testReferenceResolvingInInlineStrings()
10731073
'map' => ['key' => 'var-value'],
10741074
'list_in_map' => ['key' => ['var-value']],
10751075
'map_in_map' => ['foo' => ['bar' => 'var-value']],
1076+
'foo' => ['bar' => 'baz'],
1077+
'bar' => ['foo' => 'baz'],
1078+
'baz' => ['foo'],
1079+
'foobar' => ['foo'],
10761080
], Yaml::parse(<<<'EOF'
10771081
var: &var var-value
10781082
scalar: *var
@@ -1083,6 +1087,10 @@ public function testReferenceResolvingInInlineStrings()
10831087
map: { key: *var }
10841088
list_in_map: { key: [*var] }
10851089
map_in_map: { foo: { bar: *var } }
1090+
foo: { bar: &baz baz }
1091+
bar: { foo: *baz }
1092+
baz: [ &foo foo ]
1093+
foobar: [ *foo ]
10861094
EOF
10871095
));
10881096
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.