Skip to content

[Yaml Parser] Fix edge cases when parsing multiple documents #38228

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

Merged
merged 1 commit into from
Sep 18, 2020

Conversation

digilist
Copy link
Contributor

Q A
Branch? 4.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets
License MIT
Doc PR

I identified some edge cases when parsing multiple YAML documents with the same parser instance, because the totalNumberOfLines was not reset and so any subsequent parsing considered the number of lines of the first document.

Consider this document:

a:
    b: |
        row
        row2
c: d

Normally, a.b would be parsed as row\nrow2\n. But if the parser parsed a shorter document before, the \n after row2 was missing, as the parser considered it as the end of the file (that's why the c: d at the end is important).

So this fix resets the totalNumberOfLines in the YAML parser to null so that any subsequent parsing will initialize the value for the new document and does not use the file length of the first parsed document.

I stumbled upon this because of a flickering unit test that was using the translation component. Sometimes the translated string contained a trailing \n and sometimes not. In the end it was based on this bug, as the translation files were not loaded in the same order every time (not really sure why. It's somehow related to the cache state, but even with a warm cache it was not totally deterministic).

@xabbuh
Copy link
Member

xabbuh commented Sep 18, 2020

Thank you @digilist. That's indeed a good catch. 👍 Doesn't this also apply to the 3.4 branch?

@digilist digilist force-pushed the yaml-parser-new-line-edge-case branch from 280797c to a02c0a9 Compare September 18, 2020 09:18
@digilist digilist changed the base branch from 4.4 to 3.4 September 18, 2020 09:18
@digilist
Copy link
Contributor Author

@xabbuh yes, makes sense, I adjusted the changes for 3.4 and rebased to 3.4

@digilist digilist force-pushed the yaml-parser-new-line-edge-case branch from a02c0a9 to 012ee4f Compare September 18, 2020 09:21
@xabbuh xabbuh added this to the 3.4 milestone Sep 18, 2020
@fabpot
Copy link
Member

fabpot commented Sep 18, 2020

Thank you @digilist.

@fabpot fabpot merged commit 2eeb75d into symfony:3.4 Sep 18, 2020
$expected = ['a' => ['b' => "row\nrow2\n"], 'c' => 'd'];

// The parser was not used before, so there is a new line after row2
$this->assertSame($expected, $this->parser->parse($longDocument));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't look needed to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added this to show that this is the actual result. But yes, it can be removed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand though I think that's rather confusing when reading the test and trying to understand what is going on IMO.

// The parser was not used before, so there is a new line after row2
$this->assertSame($expected, $this->parser->parse($longDocument));

$parser = new Parser();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as I can see we can simply use $this->parser when parsing the two documents

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #38235

xabbuh added a commit that referenced this pull request Sep 18, 2020
This PR was merged into the 3.4 branch.

Discussion
----------

[Yaml] simplify the test

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | #38228 (comment)
| License       | MIT
| Doc PR        |

Commits
-------

bb64fc9 [Yaml] simplify the test
This was referenced Sep 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants