Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Yaml] [Dumper] Dumper does not use block indentation indicator when multiline value begins with newline followed by spaces. #50065

Closed
@bradtreloar

Description

@bradtreloar

Symfony version(s) affected

>=3.4.5

Description

\Symfony\Component\Yaml\Dumper::dump only checks for leading spaces when deciding whether to add a block indentation indicator. If the value begins with a newline followed by spaces then the indicator isn't added.

https://github.com/symfony/symfony/blob/6.3/src/Symfony/Component/Yaml/Dumper.php#L76

How to reproduce

// Create dumper with 2-space indentation.
$dumper = new \Symfony\Component\Yaml\Dumper(2);

// Prepare value beginning with newline followed by four spaces.
$data = ['key' => "\n    multiline value\nwith leading newline\nfollowed by spaces"];

// Dump the value to YAML.
$dumper->dump($data2, 1, 0, \Symfony\Component\Yaml\Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);

Result:

key: |-

      multiline value
  with leading newline
  followed by spaces

Expected result:

key: |2-

      multiline value
  with leading newline
  followed by spaces

Possible Solution

Remove leading newlines when determining the value of $blockIndentationIndicator.

Additional Context

No response

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