Skip to content

Commit b0f88bc

Browse files
authored
Fix MergeYaml Document.End fallback inserting extra blank lines (#7311)
The Document.End prefix fallback introduced in #7291 would copy trailing whitespace (e.g. a bare newline) as a "comment" prefix onto newly merged entries, causing an extra blank line in the output. Only use the Document.End prefix when it actually contains a YAML comment (has a `#` character). Plain trailing whitespace should not be treated as a comment to preserve. Fixes the `SeparateApplicationYamlByProfileTest.mergeIntoExistingProfileFilePreservesFormat` failure in openrewrite/rewrite-spring.
1 parent 5a79346 commit b0f88bc

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,13 @@ private Yaml.Mapping mergeMapping(Yaml.Mapping m1, Yaml.Mapping m2, P p, Cursor
256256
if (docCursor.getValue() instanceof Yaml.Document) {
257257
Yaml.Document doc = docCursor.getValue();
258258
if (!preserveDocumentSeparator(doc)) {
259-
comment = doc.getEnd().getPrefix();
260-
c = docCursor;
259+
String endPrefix = doc.getEnd().getPrefix();
260+
// Only use Document.End prefix if it contains a comment;
261+
// plain trailing whitespace should not be copied as a comment
262+
if (endPrefix != null && endPrefix.contains("#")) {
263+
comment = endPrefix;
264+
c = docCursor;
265+
}
261266
}
262267
}
263268
}

0 commit comments

Comments
 (0)