Skip to content

YAML parse error when unindented collections contain a comment #36558

@wdiesveld

Description

@wdiesveld

Symfony version(s) affected: 5.0.7

Description
In case there is a comment before the first item of an unindented collection there is a parse error:

You cannot define a mapping item when in a sequence at line ..

For example, parsing this yaml will generate the error:

test:
  item1:
  # comment
  - a
  item2:
  - b

The error will occur if there is a new dictionary item after the first item (in this case item2). It will occur only for unindented collections (when the dashes are on the same indent level as the key)

How to reproduce

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Symfony\Component\Yaml\Yaml;

$yaml = <<<END
test:
  item1:
  # comment
  - a
  item2:
  - b
END;

Yaml::parse($yaml);

Possible Solution

The problem is that due to the comment the parser regards this as a single embedded block:

  - a
  item2:
  - b

which is not a correct yaml block.

I have a fix for it; will create a pull request...

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