From 0c124a99e911d42c1d08af07fe7bea52ff081e42 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sun, 5 Jan 2025 22:01:08 +0100 Subject: [PATCH] [Yaml] Yaml::DUMP_COMPACT_NESTED_MAPPING does not apply when dumping sequences --- src/Symfony/Component/Yaml/Dumper.php | 4 +- .../Component/Yaml/Tests/DumperTest.php | 150 ++++++++++++++---- 2 files changed, 117 insertions(+), 37 deletions(-) diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index 13f21442025ba..cd5a1f6cb3dd8 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -135,8 +135,8 @@ private function doDump(mixed $input, int $inline = 0, int $indent = 0, int $fla $output .= \sprintf('%s%s%s%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', - $willBeInlined || ($compactNestedMapping && \is_array($value)) ? ' ' : "\n", - $compactNestedMapping && \is_array($value) ? substr($this->doDump($value, $inline - 1, $indent + 2, $flags, $nestingLevel + 1), $indent + 2) : $this->doDump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags, $nestingLevel + 1) + $willBeInlined || ($compactNestedMapping && \is_array($value) && Inline::isHash($value)) ? ' ' : "\n", + $compactNestedMapping && \is_array($value) && Inline::isHash($value) ? substr($this->doDump($value, $inline - 1, $indent + 2, $flags, $nestingLevel + 1), $indent + 2) : $this->doDump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags, $nestingLevel + 1) ).($willBeInlined ? "\n" : ''); } } diff --git a/src/Symfony/Component/Yaml/Tests/DumperTest.php b/src/Symfony/Component/Yaml/Tests/DumperTest.php index 0128fa4c63eb8..fe07e10a71130 100644 --- a/src/Symfony/Component/Yaml/Tests/DumperTest.php +++ b/src/Symfony/Component/Yaml/Tests/DumperTest.php @@ -1085,38 +1085,122 @@ public static function getDumpCompactNestedMapping() ], ], ]; - $expected = << [ + $data, + << [ - $data, - strtr($expected, ["\t" => str_repeat(' ', $indentation)]), - $indentation, - ]; - } + yield 'Compact nested mapping 2' => [ + $data, + << [ + $data, + << [ + $data, + << [ + $data, + << [ - $data, - $expected, - $indentation, - $inline, +YAML, + 2, + 4, ]; }