Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public Yaml.Mapping visitMapping(Yaml.Mapping mapping, P p) {
Yaml.Mapping valueMapping = (Yaml.Mapping) entry.getValue();
if (valueMapping.getEntries().size() == 1) {
Yaml.Mapping.Entry subEntry = valueMapping.getEntries().get(0);
if (!subEntry.getPrefix().contains("#") && !isExcluded(entry, subEntry) && isApplied(entry)) {
if (!subEntry.getPrefix().contains("#") && !isExcluded(entry, subEntry) && isApplied(entry)
&& !containsBlockScalar(subEntry.getValue())) {
int indentToUse = findIndent.getMostCommonIndent() > 0 ? findIndent.getMostCommonIndent() : 4;
doAfterVisit(new ShiftFormatLeftVisitor<>(subEntry.getValue(), indentToUse));

Expand All @@ -75,6 +76,28 @@ public Yaml.Mapping visitMapping(Yaml.Mapping mapping, P p) {
}));
}

private static boolean containsBlockScalar(Yaml.Block block) {
if (block instanceof Yaml.Scalar) {
Yaml.Scalar.Style style = ((Yaml.Scalar) block).getStyle();
return style == Yaml.Scalar.Style.FOLDED || style == Yaml.Scalar.Style.LITERAL;
}
if (block instanceof Yaml.Mapping) {
for (Yaml.Mapping.Entry e : ((Yaml.Mapping) block).getEntries()) {
if (containsBlockScalar(e.getValue())) {
return true;
}
}
}
if (block instanceof Yaml.Sequence) {
for (Yaml.Sequence.Entry e : ((Yaml.Sequence) block).getEntries()) {
if (containsBlockScalar(e.getBlock())) {
return true;
}
}
}
return false;
}

private boolean isExcluded(Yaml.Mapping.Entry entry, Yaml.Mapping.Entry subEntry) {
if (exclusionMatchers.isEmpty()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.openrewrite.style.GeneralFormatStyle;
import org.openrewrite.style.Style;
import org.openrewrite.yaml.MergeYaml.InsertMode;
import org.openrewrite.yaml.internal.BlockScalarUtils;
import org.openrewrite.yaml.tree.Yaml;

import java.util.ArrayList;
Expand Down Expand Up @@ -380,9 +381,9 @@ private Yaml.Sequence mergeSequence(Yaml.Sequence s1, Yaml.Sequence s2, P p, Cur
}

private Yaml.Scalar mergeScalar(Yaml.Scalar y1, Yaml.Scalar y2) {
String s1 = y1.getValue();
String s2 = y2.getValue();
return !s1.equals(s2) && !acceptTheirs ? y1.withValue(s2) : y1;
String s1 = BlockScalarUtils.getBody(y1);
String s2 = BlockScalarUtils.getBody(y2);
return !s1.equals(s2) && !acceptTheirs ? BlockScalarUtils.withBody(y1, s2) : y1;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,52 @@ void applyToWithExclusion() {
)
);
}

@Test
void skipsCoalesceWhenInnerMappingContainsBlockScalar() {
rewriteRun(
spec -> spec.recipe(new CoalesceProperties(null, null)),
yaml(
"""
outer:
inner:
java_opts: >-
-Da=1
-Db=2
after: tail
"""
)
);
}

@Test
void skipsCoalesceWhenInnerValueIsBlockScalar() {
rewriteRun(
spec -> spec.recipe(new CoalesceProperties(null, null)),
yaml(
"""
outer:
inner: |
line one
line two
"""
)
);
}

@Test
void skipsCoalesceWhenBlockScalarNestedDeeperInSubEntry() {
rewriteRun(
spec -> spec.recipe(new CoalesceProperties(null, null)),
yaml(
"""
outer:
inner:
details:
script: |
echo hello
"""
)
);
}
}
41 changes: 41 additions & 0 deletions rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3752,4 +3752,45 @@ void mergeYamlPreservesInlineCommentOnDocumentEnd() {
);
}

@Test
void overwriteFoldedStripBlockScalarPreservesEnvelope() {
rewriteRun(
spec -> spec.recipe(new MergeYaml("$", "key: replaced\n", false, null, null, null, null, null)),
yaml(
"""
key: >-
line one
line two
after: tail
""",
"""
key: >-
replaced
after: tail
"""
)
);
}

@Test
void overwriteLiteralKeepBlockScalarPreservesEnvelope() {
rewriteRun(
spec -> spec.recipe(new MergeYaml("$", "key: replaced\n", false, null, null, null, null, null)),
yaml(
"""
key: |+
line one
line two

after: tail
""",
"""
key: |+
replaced

after: tail
"""
)
);
}
}