Skip to content

Commit

Permalink
bug: yaml: DeleteProperty: do not remove unrelated empty sequences (#…
Browse files Browse the repository at this point in the history
…4205)

fixes #4204
  • Loading branch information
aleksandrserbin authored May 22, 2024
1 parent fbce584 commit a0c2940
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ private DeletePropertyVisitor(Yaml.Mapping.Entry scope) {
public Yaml visitSequence(Yaml.Sequence sequence, P p) {
sequence = (Yaml.Sequence) super.visitSequence(sequence, p);
List<Yaml.Sequence.Entry> entries = sequence.getEntries();
if (entries.isEmpty()) {
return sequence;
}

entries = ListUtils.map(entries, entry -> ToBeRemoved.hasMarker(entry) ? null : entry);
return entries.isEmpty() ? ToBeRemoved.withMarker(sequence) : sequence.withEntries(entries);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,21 @@ void preservesOriginalIndentStructureOfExistingHierarchy() {
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/4204")
@Test
void preserveEmptySequencesWithOtherKeys() {
rewriteRun(
spec -> spec.recipe(new DeleteProperty("my.key", false, null)),
yaml(
"""
my.key: qwe
seq: []
""",
"""
seq: []
"""
)
);
}
}

0 comments on commit a0c2940

Please sign in to comment.