Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YAML parse error when unindented collections contain a comment #36558

Closed
wdiesveld opened this issue Apr 24, 2020 · 2 comments
Closed

YAML parse error when unindented collections contain a comment #36558

wdiesveld opened this issue Apr 24, 2020 · 2 comments

Comments

@wdiesveld
Copy link
Contributor

wdiesveld commented Apr 24, 2020

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...

@wdiesveld wdiesveld added the Bug label Apr 24, 2020
@wdiesveld wdiesveld changed the title Parse error when YAML comments are in unindented collections YAML parse error when unindented collections contain a comment Apr 24, 2020
@xabbuh xabbuh added the Yaml label Apr 24, 2020
@Nyholm
Copy link
Member

Nyholm commented May 3, 2020

I can confirm this bug on 5.1. Yes, please make a PR. I would be happy to review it.

@wdiesveld
Copy link
Contributor Author

@Nyholm ok thanks for checking - just added a PR: #36683

@fabpot fabpot closed this as completed May 4, 2020
fabpot added a commit that referenced this issue May 4, 2020
… a comment (wdiesveld)

This PR was squashed before being merged into the 3.4 branch.

Discussion
----------

[Yaml] fix parse error when unindented collections contain a comment

| Q             | A
| ------------- | ---
| Branch?       | 5.0
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36558
| License       | MIT

### Problem
The method `Parser::getNextEmbedBlock` did not determine the yaml-block correctly when there was a comment before the first unindented collection item. This was caused by the fact that the check for unindented collection items was done for the _first line of the block only_. So in case this first line is a comment, this check will result in _false_, while in fact the parser is in an unindented collection.

### Solution
In the solution I implemented the parser will check for comment lines as well. As long as the loop encounters a comment line, it will check (in the next iteration) whether the line is an unindented collection item. So this check will be done until all comments before the first uncommented item are parsed.

Commits
-------

58bb2c5 [Yaml] fix parse error when unindented collections contain a comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants