Skip to content

[YAML] Comments are stripped in code blocks when configuring an array #8779

@hhamon

Description

@hhamon

Hi,

The previous issue (#8145) I opened 2 months ago was fixed by @jfsimon.

Unfortunately the fix only works for a special case but not all.

The current YAML parser in version 2.3.3 works for the following case (as fixed by @jfsimon):

# Comment to be removed
-
    # Comment to be removed
    content: |
        Some header

            # index.html
            <body>
                <h1>My title</h1>
            </body>

        Some footer   # Comment to be removed
    title: An HTML tutorial

Comments to be stripped are correctly stripped and those to be kept are correctly kept as expected. But if you inverse the associative keys in the yaml string (title first and then content instead of content first and title second) all comments are stripped!

This behavior looks weird but I was able to prove it with a failing unit test as shown below:

<?php
// ...
class ParserTest extends \PHPUnit_Framework_TestCase
{
    /...
    // This test passes
    public function testArrayWithEmbeddedCommentedBlock()
    {
        $yaml = '# comment 1
header

    # comment 2
    <body>
        <h1>title</h1>
    </body>

footer # comment3
';

        $this->assertEquals(array(array(
            'content' => $yaml,
            'title' => 'some title',
        )), Yaml::parse(<<<EOF
-
    content: |
        # comment 1
        header

            # comment 2
            <body>
                <h1>title</h1>
            </body>

        footer # comment3
    title: some title
EOF
        ));
    }

    // This same test fails
    public function testArrayWithEmbeddedCommentedBlock2()
    {
        $yaml = '# comment 1
header

    # comment 2
    <body>
        <h1>title</h1>
    </body>

footer # comment3
';

        $this->assertEquals(array(array(
            'title' => 'some title',
            'content' => $yaml,
        )), Yaml::parse(<<<EOF
-
    title: some title
    content: |
        # comment 1
        header

            # comment 2
            <body>
                <h1>title</h1>
            </body>

        footer # comment3
EOF
        ));
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions