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

[Dumper] Trim leading newlines when checking if value begins with a space #50066

Merged
merged 1 commit into from
Apr 23, 2023
Merged

[Dumper] Trim leading newlines when checking if value begins with a space #50066

merged 1 commit into from
Apr 23, 2023

Conversation

bradtreloar
Copy link
Contributor

@bradtreloar bradtreloar commented Apr 20, 2023

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #50065
License MIT

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.3 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@bradtreloar bradtreloar changed the title Trim leading newlines when checking if value begins with a space [Yaml] [Dumper] Trim leading newlines when checking if value begins with a space Apr 20, 2023
@@ -69,9 +69,9 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
}

if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r")) {
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
// If the first non-empty line starts with a space character, the spec requires a blockIndicationIndicator
Copy link
Member

Choose a reason for hiding this comment

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

Is that true? Or what happens when we change the fixture from your test to something like this?

$data = [
    'data' => [
        'multi_line' => "\n    \n    the first line has leading newline and spaces\nThe second line does not.",
    ],
];

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The parser treats any line containing only spaces as an empty line, even if the number of space exceeds the indentation.

So it isn't enough to trim off the first newline before looking for a leading space, the dumper needs to find the first line that is neither empty nor contains only spaces.

I've added an additional test to check that the indicator is not added if the value begins with a line containing only spaces.

The indentation indicator check is now more than a single-line operation, so I've moved it into a private function.

@xabbuh
Copy link
Member

xabbuh commented Apr 23, 2023

The fix itself looks good to me now. Can you take a look at the things fabbot reported though please?

@carsonbot carsonbot changed the title [Yaml] [Dumper] Trim leading newlines when checking if value begins with a space [Dumper] Trim leading newlines when checking if value begins with a space Apr 23, 2023
@xabbuh
Copy link
Member

xabbuh commented Apr 23, 2023

👍 just one last minor comment

@fabpot
Copy link
Member

fabpot commented Apr 23, 2023

Thank you @bradtreloar.

@fabpot fabpot merged commit 30d5bd4 into symfony:5.4 Apr 23, 2023
This was referenced Apr 28, 2023
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.

None yet

4 participants