-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Yaml] Fix Yaml Parser with quote end in a new line #48022
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,6 +12,7 @@ | |||||
namespace Symfony\Component\Yaml\Tests; | ||||||
|
||||||
use PHPUnit\Framework\TestCase; | ||||||
use Symfony\Component\Yaml\Exception\ParseException; | ||||||
use Symfony\Component\Yaml\Yaml; | ||||||
|
||||||
class YamlTest extends TestCase | ||||||
|
@@ -24,6 +25,56 @@ public function testParseAndDump() | |||||
$this->assertEquals($data, $parsed); | ||||||
} | ||||||
|
||||||
public function testParseWithMultilineQuotes() | ||||||
{ | ||||||
$yaml = <<<YAML | ||||||
foo: | ||||||
bar: 'baz | ||||||
biz | ||||||
|
||||||
' | ||||||
baz: 'Lorem | ||||||
|
||||||
ipsum' | ||||||
error: Une erreur s'est produite. | ||||||
trialMode: 'période d''essai' | ||||||
double_line: 'Les utilisateurs sélectionnés | ||||||
n''ont pas d''email. | ||||||
|
||||||
' | ||||||
a: 'b'' | ||||||
c' | ||||||
empty: '' | ||||||
foo:bar: 'foobar' | ||||||
YAML; | ||||||
|
||||||
$this->assertSame(['foo' => [ | ||||||
'bar' => "baz biz\n", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the behavior you expect @xabbuh? Currently with this input we get this result without your suggestion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, as far as I know that should be the outcome. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the YAML spec I didn't find sth. for this case so it might be a very special case. This is the ChatGPT answer for that:
This is also the way it works in different online interpreters which are supporting this (like this one: http://www.yaml-online-parser.appspot.com/.
As So it seems to be not really clear how to deal with this and we should decide how to deal with it. My suggestion would be "keep it simple for this very special case" and keep it how it is atm in this PR with the |
||||||
'baz' => "Lorem\nipsum", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
'error' => "Une erreur s'est produite.", | ||||||
'trialMode' => "période d'essai", | ||||||
'double_line' => "Les utilisateurs sélectionnés n'ont pas d'email.\n", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
'a' => "b' c", | ||||||
'empty' => '', | ||||||
'foo:bar' => 'foobar', | ||||||
]], Yaml::parse($yaml)); | ||||||
} | ||||||
|
||||||
public function testParseWithMultilineQuotesExpectException() | ||||||
{ | ||||||
$yaml = <<<YAML | ||||||
foo: | ||||||
bar: 'baz | ||||||
|
||||||
' | ||||||
' | ||||||
YAML; | ||||||
|
||||||
$this->expectException(ParseException::class); | ||||||
$this->expectExceptionMessage('Unable to parse at line 5 (near "\'").'); | ||||||
Yaml::parse($yaml); | ||||||
} | ||||||
|
||||||
public function testZeroIndentationThrowsException() | ||||||
{ | ||||||
$this->expectException(\InvalidArgumentException::class); | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not valid YAML AFAIU. The quote must be indented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then we should close this PR and the issue with "Won't fix", because this is exactly the case, what is requested to be fixed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I am not mistaken, this is indeed valid YAML.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @xabbuh and @fabpot,
I have taken some time to read up on this YAML topic. In the internet are two kinds of YAML parser for this topic. Either the content must be indented even if it is in quotes or everything in quotes can be indented free, because the part in quotes is simply considered as a text block.
What I want to say is that the PR should not be merged as it is and we should decide here how to proceed.
Solution A: everything should be how it is and we only support indented quotes, seems to be how it's described in the spec https://yaml.org/spec/1.2.2/#8111-block-indentation-indicator.
Solution B: be a bit more generous and allow what requested in the issue and how a lot of online parsers/validators see the quoted part as a string and allow stuff like that:
Based on that I'd prefer solution A and keep it how it is as @fabpot mentioned. What do you think?