8000 cast arrays to objects after parsing has finished · symfony/symfony@8251d67 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8251d67

Browse files
committed
cast arrays to objects after parsing has finished
Casting arrays to objects must happen after the complete YAML string has been parsed to avoid errors when parsing subsequent lines.
1 parent 5da874d commit 8251d67

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,6 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
240240
if ($isRef) {
241241
$this->refs[$isRef] = $data[$key];
242242
}
243-
244-
if ($objectForMap && !is_object($data)) {
245-
$data = (object) $data;
246-
}
247243
} else {
248244
// multiple documents are not supported
249245
if ('---' === $this->currentLine) {
@@ -307,6 +303,10 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
307303
mb_internal_encoding($mbEncoding);
308304
}
309305

306+
if ($objectForMap && !is_object($data)) {
307+
$data = (object) $data;
308+
}
309+
310310
return empty($data) ? null : $data;
311311
}
312312

@@ -574,7 +574,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
574574
$previousLineIndented = false;
575575
$previousLineBlank = false;
576576

577-
for ($i = 0; $i < count($blockLines); $i++) {
577+
for ($i = 0; $i < count($blockLines); ++$i) {
578578
if ('' === $blockLines[$i]) {
579579
$text .= "\n";
580580
$previousLineIndented = false;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,15 @@ public function testObjectForMapEnabledWithInlineMapping()
460460
$this->assertEquals('cat', $result->fiz);
461461
}
462462

463+
public function testObjectForMapIsAppliedAfterParsing()
464+
{
465+
$expected = new \stdClass();
466+
$expected->foo = 'bar';
467+
$expected->baz = 'foobar';
468+
469+
$this->assertEquals($expected, $this->parser->parse("foo: bar\nbaz: foobar", false, false, true));
470+
}
471+
463472
/**
464473
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
465474
*/

0 commit comments

Comments
 (0)
0