8000 Fix exception thrown by YamlEncoder · symfony/symfony@ab9de2d · GitHub
[go: up one dir, main page]

Skip to content

Commit ab9de2d

Browse files
Fix exception thrown by YamlEncoder
1 parent 322e13c commit ab9de2d

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/Symfony/Component/Serializer/Encoder/YamlEncoder.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Symfony\Component\Serializer\Encoder;
1313

14+
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
1415
use Symfony\Component\Serializer\Exception\RuntimeException;
1516
use Symfony\Component\Yaml\Dumper;
17+
use Symfony\Component\Yaml\Exception\ParseException;
1618
use Symfony\Component\Yaml\Parser;
1719
use Symfony\Component\Yaml\Yaml;
1820

@@ -85,7 +87,11 @@ public function decode(string $data, string $format, array $context = []): mixed
8587
{
8688
$context = array_merge($this->defaultContext, $context);
8789

88-
return $this->parser->parse($data, $context[self::YAML_FLAGS]);
90+
try {
91+
return $this->parser->parse($data, $context[self::YAML_FLAGS]);
92+
} catch (ParseException $e) {
93+
throw new NotEncodableValueException($e->getMessage(), $e->getCode(), $e);
94+
}
8995
}
9096

9197
public function supportsDecoding(string $format): bool

src/Symfony/Component/Serializer/Tests/Encoder/YamlEncoderTest.php

Lines changed: 9 additions & 0 deletions
< 8000 /thead>
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Serializer\Encoder\YamlEncoder;
16+
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
1617
use Symfony\Component\Yaml\Yaml;
1718

1819
/**
@@ -81,4 +82,12 @@ public function testContext()
8182
$this->assertEquals(['foo' => $obj], $encoder->decode("foo: !php/object 'O:8:\"stdClass\":1:{s:3:\"bar\";i:2;}'", 'yaml'));
8283
$this->assertEquals(['foo' => null], $encoder->decode("foo: !php/object 'O:8:\"stdClass\":1:{s:3:\"bar\";i:2;}'", 'yaml', [YamlEncoder::YAML_FLAGS => 0]));
8384
}
85+
86+
public function testInvalidYaml()
87+
{
88+
$encoder = new YamlEncoder();
89+
90+
$this->expectException(NotEncodableValueException::class);
91+
$encoder->decode("\t", 'yaml');
92+
}
8493
}

0 commit comments

Comments
 (0)
0