#66326·symfony

Yaml: TaggedValue + DUMP_MULTI_LINE_LITERAL_BLOCK causes extra line break in round trip

Author: donquixoteCreated Sep 25, 2026Updated Sep 25, 2026
LabelsBugStatus: Needs Review

Symfony version(s) affected

8.2 (e53e3c0f)

Description

The Yaml parser, when faced with a TaggedValue with multi-line string, will insert a trailing line break.

Yaml:

- !Tag |
    one line without trailing newline

The parser will add a trailing line break at the end of the string.

How to reproduce

This can be shown in a phpunit test. I am adding more examples to show the conditions when does or does not happen, and also how it behaves in a round trip.

php
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Tag\TaggedValue;
use Symfony\Component\Yaml\Yaml as SymfonyYaml;
[...]

  public function testYamlParse(): void {
    $yaml = <<<'YAML'
- !Tag |
    one line
- !Tag |
    one line with trailing newline

- !Tag |
    two
    lines
- !Tag |
    two lines
    with trailing newline

- !Tag "regular string\nwith line break"

YAML;
    $parser = new Parser();
    $parsed = $parser->parse($yaml, SymfonyYaml::PARSE_EXCEPTION_ON_INVALID_TYPE | SymfonyYaml::PARSE_CUSTOM_TAGS);
    $this->assertEquals([
      // Unexpected trailing line break is inserted.
      new TaggedValue('Tag', "one line\n"),
      new TaggedValue('Tag', "one line with trailing newline\n"),
      // Unexpected trailing line break is inserted.
      new TaggedValue('Tag', "two\nlines\n"),
      new TaggedValue('Tag', "two lines\nwith trailing newline\n"),
      new TaggedValue('Tag', "regular string\nwith line break"),
    ], $parsed);
  }

  public function testYamlDumpAndParse(): void {
    $original_data = [
      new TaggedValue('Tag', "one line"),
      new TaggedValue('Tag', "two\nlines"),
      new TaggedValue('Tag', "two lines\nwith trailing space\n"),
      new TaggedValue('Tag', ["two lines in array\nwith trailing space\n"]),
    ];
    $dumper = new Dumper();
    $yaml = $dumper->dump(
      $original_data,
      PHP_INT_MAX,
      0,
      SymfonyYaml::DUMP_EXCEPTION_ON_INVALID_TYPE | SymfonyYaml::DUMP_MULTI_LINE_LITERAL_BLOCK,
    );
    $parser = new Parser();
    $parsed = $parser->parse(
      $yaml,
      SymfonyYaml::PARSE_EXCEPTION_ON_INVALID_TYPE | SymfonyYaml::PARSE_CUSTOM_TAGS
    );
    $instead_data = $original_data;
    $instead_data[1] = new TaggedValue('Tag', "two\nlines\n");
    $this->assertEquals($instead_data, $parsed);  // Passes.
    $this->assertEquals($original_data, $parsed, $yaml);  // Fails.
  }

Possible Solution

Should be fixed in the parser..

Additional Context

No response