Skip to content

Commit 0c866ba

Browse files
authored
Fix ChangePropertyKey whitespace quirks when moving subproperties (#7283)
Fixes #2608
1 parent 445d977 commit 0c866ba

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

rewrite-yaml/src/main/java/org/openrewrite/yaml/ChangePropertyKey.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ public Yaml.Mapping visitMapping(Yaml.Mapping mapping, P p) {
350350
}
351351

352352
if (entries.size() == 1) {
353-
entries = ListUtils.map(entries, e -> e.withPrefix(""));
353+
String firstEntryPrefix = m.getEntries().get(0).getPrefix();
354+
entries = ListUtils.map(entries, e -> e.withPrefix(firstEntryPrefix));
354355
}
355356

356357
if (changed) {

rewrite-yaml/src/test/java/org/openrewrite/yaml/ChangePropertyKeyTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,53 @@ void singleEntry() {
5151
);
5252
}
5353

54+
@Issue("https://github.com/openrewrite/rewrite/issues/2608")
55+
@Test
56+
void moveSubpropertyOut() {
57+
rewriteRun(
58+
spec -> spec.recipe(new ChangePropertyKey("foo.bar", "bar", null, null, null)),
59+
yaml(
60+
"""
61+
foo:
62+
bar:
63+
prop: val
64+
other: other-val
65+
""",
66+
"""
67+
foo:
68+
other: other-val
69+
bar:
70+
prop: val
71+
"""
72+
)
73+
);
74+
}
75+
76+
@Issue("https://github.com/openrewrite/rewrite/issues/2608")
77+
@Test
78+
void multiDocument() {
79+
rewriteRun(
80+
spec -> spec.recipe(new ChangePropertyKey("foo.bar", "bar", null, null, null)),
81+
yaml(
82+
"""
83+
a: b
84+
---
85+
foo:
86+
bar:
87+
prop: val
88+
other: other-val
89+
""",
90+
"""
91+
a: b
92+
---
93+
other: other-val
94+
bar:
95+
prop: val
96+
"""
97+
)
98+
);
99+
}
100+
54101
@Issue("https://github.com/openrewrite/rewrite/issues/1873")
55102
@Test
56103
void shorterNewKeyWithIndentedConfig() {

0 commit comments

Comments
 (0)