8000 bug #17197 [Yaml] cast arrays to objects after parsing has finished (… · symfony/symfony@5991565 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5991565

Browse files
committed
bug #17197 [Yaml] cast arrays to objects after parsing has finished (xabbuh)
This PR was merged into the 2.8 branch. Discussion ---------- [Yaml] cast arrays to objects after parsing has finished | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17190 | License | MIT | Doc PR | Casting arrays to objects must happen after the complete YAML string has been parsed to avoid errors when parsing subsequent lines. Commits ------- 8251d67 cast arrays to objects after parsing has finished
2 parents 674c070 + 8251d67 commit 5991565

File tree

2 files changed

+13
-4
lines changed
  • src/Symfony/Component/Yaml
    • < 8000 div class="PRIVATE_TreeView-item-visual prc-TreeView-TreeViewItemVisual-dRlGq" aria-hidden="true">
      Tests

2 files changed

+13
-4
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 4 additions & 4 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

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