Skip to content

[Yaml] Dumper: TaggedValue at top level fails unless inline. #46725

@donquixote

Description

@donquixote

Symfony version(s) affected

6.1.0

Description

The Dumper::dump() method does not allow TaggedValue in the top level, unless it is in "inline" mode.
EDIT: The parser seems alright.

We should either fix it, or more explicitly enforce a contract where this is not allowed.

How to reproduce

Dumper inline -> ok

    public function testDumpingTaggedValueTopLevelScalar()
    {
        $data = new TaggedValue('user', 'jane');

        $yaml = $this->dumper->dump($data);

        $expected = '!user jane';
        $this->assertSame($expected, $yaml);
    }

    public function testDumpingTaggedValueTopLevelAssocInline()
    {
        $data = new TaggedValue('user', ['name' => 'jane']);

        $yaml = $this->dumper->dump($data);

        $expected = '!user { name: jane }';
        $this->assertSame($expected, $yaml);
    }

Dumper not inline -> broken

    public function testDumpingTaggedValueTopLevelAssoc()
    {
        $data = new TaggedValue('user', ['name' => 'jane']);

        $this->expectException(\TypeError::class);
        $this->expectExceptionMessage('Symfony\Component\Yaml\Inline::isHash(): Argument #1 ($value) must be of type ArrayObject|stdClass|array, Symfony\Component\Yaml\Tag\TaggedValue given');

        $this->dumper->dump($data, 2);
    }

Parser works fine

EDIT: I previously thought this was broken, but now found that it works correctly.

    public function testTaggedValueTopLevelScalar() {
        $yml = '!user barbara';
        $data = $this->parser->parse($yml, Yaml::PARSE_CUSTOM_TAGS);
        $expected = new TaggedValue('user', 'barbara');
        $this->assertEquals($expected, $data);
    }

    public function testTaggedValueTopLevelAssocInline() {
        $yml = '!user { name: barbara }';

        #$yml = '!user {name: barbara}';
        $data = $this->parser->parse($yml, Yaml::PARSE_CUSTOM_TAGS);
        $expected = new TaggedValue('user', ['name' => 'barbara']);
        $this->assertEquals($expected, $data);
    }

    public function testTaggedValueTopLevelAssoc() {
        $yml = <<<'YAML'
!user
name: barbara
YAML;

        $data = $this->parser->parse($yml, Yaml::PARSE_CUSTOM_TAGS);
        $expected = new TaggedValue('user', ['name' => 'barbara']);
        $this->assertEquals($expected, $data);
    }

    public function testTaggedValueTopLevelList() {
        $yml = <<<'YAML'
!users
- barbara
YAML;

        $data = $this->parser->parse($yml, Yaml::PARSE_CUSTOM_TAGS);
        $expected = new TaggedValue('users', ['barbara']);
        $this->assertEquals($expected, $data);
    }

Possible Solution

I'd say fix it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions