Skip to content

[Yaml] Yaml::DUMP_COMPACT_NESTED_MAPPING does not apply when dumping sequences #59671

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

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Symfony/Component/Yaml/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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" : '');
}
}
Expand Down
150 changes: 115 additions & 35 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1085,38 +1085,122 @@ public static function getDumpCompactNestedMapping()
],
],
];
$expected = <<<YAML

yield 'Compact nested mapping 1' => [
$data,
<<<YAML
planets:
\t- name: Mercury
\t distance: 57910000
\t properties:
\t\t - name: size
\t\t value: 4879
\t\t - name: moons
\t\t value: 0
\t\t - - - { }
\t- name: Jupiter
\t distance: 778500000
\t properties:
\t\t - name: size
\t\t value: 139820
\t\t - name: moons
\t\t value: 79
\t\t - - { }
- name: Mercury
distance: 57910000
properties:
- name: size
value: 4879
- name: moons
value: 0
-
-
- { }
- name: Jupiter
distance: 778500000
properties:
- name: size
value: 139820
- name: moons
value: 79
-
- { }

YAML;
YAML,
1,
];

for ($indentation = 1; $indentation < 5; ++$indentation) {
yield \sprintf('Compact nested mapping %d', $indentation) => [
$data,
strtr($expected, ["\t" => str_repeat(' ', $indentation)]),
$indentation,
];
}
yield 'Compact nested mapping 2' => [
$data,
<<<YAML
planets:
- name: Mercury
distance: 57910000
properties:
- name: size
value: 4879
- name: moons
value: 0
-
-
- { }
- name: Jupiter
distance: 778500000
properties:
- name: size
value: 139820
- name: moons
value: 79
-
- { }

YAML,
2,
];

$indentation = 2;
$inline = 4;
$expected = <<<YAML
yield 'Compact nested mapping 3' => [
$data,
<<<YAML
planets:
- name: Mercury
distance: 57910000
properties:
- name: size
value: 4879
- name: moons
value: 0
-
-
- { }
- name: Jupiter
distance: 778500000
properties:
- name: size
value: 139820
- name: moons
value: 79
-
- { }

YAML,
3,
];

yield 'Compact nested mapping 4' => [
$data,
<<<YAML
planets:
- name: Mercury
distance: 57910000
properties:
- name: size
value: 4879
- name: moons
value: 0
-
-
- { }
- name: Jupiter
distance: 778500000
properties:
- name: size
value: 139820
- name: moons
value: 79
-
- { }

YAML,
4,
];

yield 'Compact nested mapping 2 and inline 4' => [
$data,
<<<YAML
planets:
- name: Mercury
distance: 57910000
Expand All @@ -1131,13 +1215,9 @@ public static function getDumpCompactNestedMapping()
- { name: moons, value: 79 }
- [{ }]

YAML;

yield \sprintf('Compact nested mapping %d and inline %d', $indentation, $inline) => [
$data,
$expected,
$indentation,
$inline,
YAML,
2,
4,
];
}

Expand Down
Loading