Skip to content

Commit d3583b9

Browse files
committed
bug #59323 [Serializer] Fix exception thrown by YamlEncoder (VincentLanglet)
This PR was merged into the 6.4 branch. Discussion ---------- [Serializer] Fix exception thrown by `YamlEncoder` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix the example given #59306 (comment) | License | MIT DecoderInterface::decode is supposed to throw scoped exception cf https://github.com/symfony/symfony/blob/4078cb1642332cf5bbd45f5118edf766580cfb1e/src/Symfony/Component/Serializer/Encoder/DecoderInterface.php#L33-L35 but the YamlEncoder::decode could throw ParseException. Others similar Encoder, like XmlEncoder or JsonEncoder are throwing NotEncodableValue so I did the same cf example https://github.com/symfony/symfony/blob/da155534524f0d9c501023d10bb423fce4cd4482/src/Symfony/Component/Serializer/Encoder/JsonEncode.php#L41-L45 Commits ------- ab9de2d Fix exception thrown by YamlEncoder
2 parents c557e18 + ab9de2d commit d3583b9

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
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy