Skip to content

Commit dc38bb0

Browse files
bug #59671 [Yaml] Yaml::DUMP_COMPACT_NESTED_MAPPING does not apply when dumping sequences (xabbuh)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Yaml] Yaml::DUMP_COMPACT_NESTED_MAPPING does not apply when dumping sequences | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT following #59315, the flag should only affect how maps are dumped but shouldn't influence dumping sequences (see also symfony/symfony-docs#20510 (comment)) Commits ------- 0c124a9 [Yaml] Yaml::DUMP_COMPACT_NESTED_MAPPING does not apply when dumping sequences
2 parents 5a09d52 + 0c124a9 commit dc38bb0

File tree

2 files changed

+117
-37
lines changed

2 files changed

+117
-37
lines changed

src/Symfony/Component/Yaml/Dumper.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ private function doDump(mixed $input, int $inline = 0, int $indent = 0, int $fla
135135
$output .= \sprintf('%s%s%s%s',
136136
$prefix,
137137
$dumpAsMap ? Inline::dump($key, $flags).':' : '-',
138-
$willBeInlined || ($compactNestedMapping && \is_array($value)) ? ' ' : "\n",
139-
$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)
138+
$willBeInlined || ($compactNestedMapping && \is_array($value) && Inline::isHash($value)) ? ' ' : "\n",
139+
$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)
140140
).($willBeInlined ? "\n" : '');
141141
}
142142
}

src/Symfony/Component/Yaml/Tests/DumperTest.php

+115-35
Original file line numberDiff line numberDiff line change
@@ -1085,38 +1085,122 @@ public static function getDumpCompactNestedMapping()
10851085
],
10861086
],
10871087
];
1088-
$expected = <<<YAML
1088+
1089+
yield 'Compact nested mapping 1' => [
1090+
$data,
1091+
<<<YAML
10891092
planets:
1090-
\t- name: Mercury
1091-
\t distance: 57910000
1092-
\t properties:
1093-
\t\t - name: size
1094-
\t\t value: 4879
1095-
\t\t - name: moons
1096-
\t\t value: 0
1097-
\t\t - - - { }
1098-
\t- name: Jupiter
1099-
\t distance: 778500000
1100-
\t properties:
1101-
\t\t - name: size
1102-
\t\t value: 139820
1103-
\t\t - name: moons
1104-
\t\t value: 79
1105-
\t\t - - { }
1093+
- name: Mercury
1094+
distance: 57910000
1095+
properties:
1096+
- name: size
1097+
value: 4879
1098+
- name: moons
1099+
value: 0
1100+
-
1101+
-
1102+
- { }
1103+
- name: Jupiter
1104+
distance: 778500000
1105+
properties:
1106+
- name: size
1107+
value: 139820
1108+
- name: moons
1109+
value: 79
1110+
-
1111+
- { }
11061112
1107-
YAML;
1113+
YAML,
1114+
1,
1115+
];
11081116

1109-
for ($indentation = 1; $indentation < 5; ++$indentation) {
1110-
yield \sprintf('Compact nested mapping %d', $indentation) => [
1111-
$data,
1112-
strtr($expected, ["\t" => str_repeat(' ', $indentation)]),
1113-
$indentation,
1114-
];
1115-
}
1117+
yield 'Compact nested mapping 2' => [
1118+
$data,
1119+
<<<YAML
1120+
planets:
1121+
- name: Mercury
1122+
distance: 57910000
1123+
properties:
1124+
- name: size
1125+
value: 4879
1126+
- name: moons
1127+
value: 0
1128+
-
1129+
-
1130+
- { }
1131+
- name: Jupiter
1132+
distance: 778500000
1133+
properties:
1134+
- name: size
1135+
value: 139820
1136+
- name: moons
1137+
value: 79
1138+
-
1139+
- { }
1140+
1141+
YAML,
1142+
2,
1143+
];
11161144

1117-
$indentation = 2;
1118-
$inline = 4;
1119-
$expected = <<<YAML
1145+
yield 'Compact nested mapping 3' => [
1146+
$data,
1147+
<<<YAML
1148+
planets:
1149+
- name: Mercury
1150+
distance: 57910000
1151+
properties:
1152+
- name: size
1153+
value: 4879
1154+
- name: moons
1155+
value: 0
1156+
-
1157+
-
1158+
- { }
1159+
- name: Jupiter
1160+
distance: 778500000
1161+
properties:
1162+
- name: size
1163+
value: 139820
1164+
- name: moons
1165+
value: 79
1166+
-
1167+
- { }
1168+
1169+
YAML,
1170+
3,
1171+
];
1172+
1173+
yield 'Compact nested mapping 4' => [
1174+
$data,
1175+
<<<YAML
1176+
planets:
1177+
- name: Mercury
1178+
distance: 57910000
1179+
properties:
1180+
- name: size
1181+
value: 4879
1182+
- name: moons
1183+
value: 0
1184+
-
1185+
-
1186+
- { }
1187+
- name: Jupiter
1188+
distance: 778500000
1189+
properties:
1190+
- name: size
1191+
value: 139820
1192+
- name: moons
1193+
value: 79
1194+
-
1195+
- { }
1196+
1197+
YAML,
1198+
4,
1199+
];
1200+
1201+
yield 'Compact nested mapping 2 and inline 4' => [
1202+
$data,
1203+
<<<YAML
11201204
planets:
11211205
- name: Mercury
11221206
distance: 57910000
@@ -1131,13 +1215,9 @@ public static function getDumpCompactNestedMapping()
11311215
- { name: moons, value: 79 }
11321216
- [{ }]
11331217
1134-
YAML;
1135-
1136-
yield \sprintf('Compact nested mapping %d and inline %d', $indentation, $inline) => [
1137-
$data,
1138-
$expected,
1139-
$indentation,
1140-
$inline,
1218+
YAML,
1219+
2,
1220+
4,
11411221
];
11421222
}
11431223

0 commit comments

Comments
 (0)