Verified Commit a91f4f4e authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3108309 by kim.pepper, bonrita, alexpott, andypost, longwave: Support...

Issue #3108309 by kim.pepper, bonrita, alexpott, andypost, longwave: Support Yaml::PARSE_CUSTOM_TAGS in \Drupal\Component\Serialization\YamlSymfony::decode
parent 85c3a3c2
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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);
+69 −0
Original line number Diff line number Diff line
@@ -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.
@@ -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
      ],
    ];
  }

}