Loading core/lib/Drupal/Component/Serialization/YamlSymfony.php +1 −1 Original line number Diff line number Diff line Loading @@ -43,7 +43,7 @@ public static function decode($raw) { $yaml = new Parser(); // Make sure we have a single trailing newline. A very simple config like // 'foo: bar' with no newline will fail to parse otherwise. return $yaml->parse($raw, SymfonyYaml::PARSE_EXCEPTION_ON_INVALID_TYPE); return $yaml->parse($raw, SymfonyYaml::PARSE_EXCEPTION_ON_INVALID_TYPE | SymfonyYaml::PARSE_CUSTOM_TAGS); } catch (\Exception $e) { throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e); Loading core/tests/Drupal/Tests/Component/Serialization/YamlSymfonyTest.php +69 −0 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ use Drupal\Component\Serialization\Exception\InvalidDataTypeException; use Drupal\Component\Serialization\YamlSymfony; use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Component\Yaml\Tag\TaggedValue; /** * Tests the YamlSymfony serialization implementation. Loading Loading @@ -106,4 +107,72 @@ public function testDecodeObjectSupportDisabled(): void { YamlSymfony::decode($yaml); } /** * Tests that YAML custom tags are supported and parsed. * * @covers ::decode * * @dataProvider taggedValuesProvider */ public function testCustomTagSupport($expected, $yaml) { try { $this->assertEquals($expected, YamlSymfony::decode($yaml)); } catch (InvalidDataTypeException $e) { $message = 'Custom tag support is not enabled. Enable the `Yaml::PARSE_CUSTOM_TAGS` flag to prevent the %s exception.'; $this->fail(sprintf($message, InvalidDataTypeException::class)); } } /** * Data provider for testCustomTagSupport(). * * @return array * A list of test data. */ public function taggedValuesProvider() { return [ 'sequences' => [ [ new TaggedValue('foo', ['yaml']), new TaggedValue('quz', ['bar']), ], <<<YAML - !foo - yaml - !quz [bar] YAML ], 'mappings' => [ new TaggedValue('foo', [ 'foo' => new TaggedValue('quz', ['bar']), 'quz' => new TaggedValue('foo', ['quz' => 'bar']), ]), <<<YAML !foo foo: !quz [bar] quz: !foo quz: bar YAML ], 'inline' => [ [ new TaggedValue('foo', [ 'foo', 'bar', ]), new TaggedValue('quz', [ 'foo' => 'bar', 'quz' => new TaggedValue('bar', ['one' => 'bar']), ]), ], <<<YAML - !foo [foo, bar] - !quz {foo: bar, quz: !bar {one: bar}} YAML ], ]; } } Loading
core/lib/Drupal/Component/Serialization/YamlSymfony.php +1 −1 Original line number Diff line number Diff line Loading @@ -43,7 +43,7 @@ public static function decode($raw) { $yaml = new Parser(); // Make sure we have a single trailing newline. A very simple config like // 'foo: bar' with no newline will fail to parse otherwise. return $yaml->parse($raw, SymfonyYaml::PARSE_EXCEPTION_ON_INVALID_TYPE); return $yaml->parse($raw, SymfonyYaml::PARSE_EXCEPTION_ON_INVALID_TYPE | SymfonyYaml::PARSE_CUSTOM_TAGS); } catch (\Exception $e) { throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e); Loading
core/tests/Drupal/Tests/Component/Serialization/YamlSymfonyTest.php +69 −0 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ use Drupal\Component\Serialization\Exception\InvalidDataTypeException; use Drupal\Component\Serialization\YamlSymfony; use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Component\Yaml\Tag\TaggedValue; /** * Tests the YamlSymfony serialization implementation. Loading Loading @@ -106,4 +107,72 @@ public function testDecodeObjectSupportDisabled(): void { YamlSymfony::decode($yaml); } /** * Tests that YAML custom tags are supported and parsed. * * @covers ::decode * * @dataProvider taggedValuesProvider */ public function testCustomTagSupport($expected, $yaml) { try { $this->assertEquals($expected, YamlSymfony::decode($yaml)); } catch (InvalidDataTypeException $e) { $message = 'Custom tag support is not enabled. Enable the `Yaml::PARSE_CUSTOM_TAGS` flag to prevent the %s exception.'; $this->fail(sprintf($message, InvalidDataTypeException::class)); } } /** * Data provider for testCustomTagSupport(). * * @return array * A list of test data. */ public function taggedValuesProvider() { return [ 'sequences' => [ [ new TaggedValue('foo', ['yaml']), new TaggedValue('quz', ['bar']), ], <<<YAML - !foo - yaml - !quz [bar] YAML ], 'mappings' => [ new TaggedValue('foo', [ 'foo' => new TaggedValue('quz', ['bar']), 'quz' => new TaggedValue('foo', ['quz' => 'bar']), ]), <<<YAML !foo foo: !quz [bar] quz: !foo quz: bar YAML ], 'inline' => [ [ new TaggedValue('foo', [ 'foo', 'bar', ]), new TaggedValue('quz', [ 'foo' => 'bar', 'quz' => new TaggedValue('bar', ['one' => 'bar']), ]), ], <<<YAML - !foo [foo, bar] - !quz {foo: bar, quz: !bar {one: bar}} YAML ], ]; } }