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 @@ -42,6 +42,17 @@ public class DeleteSpringProperty extends Recipe {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
org.openrewrite.yaml.DeleteProperty yamlDeleteProperty =
new org.openrewrite.yaml.DeleteProperty(propertyKey, false, true, null);
DeleteProperty propertiesDeleteProperty =
new DeleteProperty(propertyKey, true);

String descendantPropertyKey = containsGlob(propertyKey) ? null : propertyKey + ".*";
org.openrewrite.yaml.DeleteProperty yamlDeleteDescendants =
descendantPropertyKey == null ? null : new org.openrewrite.yaml.DeleteProperty(descendantPropertyKey, false, true, null);
DeleteProperty propertiesDeleteDescendants =
descendantPropertyKey == null ? null : new DeleteProperty(descendantPropertyKey, true);

return Preconditions.check(new IsPossibleSpringConfigFile(), new TreeVisitor<Tree, ExecutionContext>() {
@Override
public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
Expand All @@ -51,14 +62,24 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
@Override
public @Nullable Tree visit(@Nullable Tree t, ExecutionContext ctx) {
if (t instanceof Yaml.Documents) {
t = new org.openrewrite.yaml.DeleteProperty(propertyKey, false, true, null)
.getVisitor().visitNonNull(t, ctx);
Tree deleted = yamlDeleteProperty.getVisitor().visitNonNull(t, ctx);
if (yamlDeleteDescendants != null) {
deleted = yamlDeleteDescendants.getVisitor().visitNonNull(deleted, ctx);
}
t = deleted;
} else if (t instanceof Properties.File) {
t = new DeleteProperty(propertyKey, true)
.getVisitor().visitNonNull(t, ctx);
Tree deleted = propertiesDeleteProperty.getVisitor().visitNonNull(t, ctx);
if (propertiesDeleteDescendants != null) {
deleted = propertiesDeleteDescendants.getVisitor().visitNonNull(deleted, ctx);
}
t = deleted;
}
return t;
}
});
}

private static boolean containsGlob(String propertyKey) {
return propertyKey.indexOf('*') >= 0 || propertyKey.indexOf('?') >= 0 || propertyKey.indexOf('[') >= 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,130 @@ void deleteLastKey() {
);
}

@Test
void deleteParentPropertyInProperties() {
rewriteRun(
spec -> spec.recipe(new DeleteSpringProperty("server.servlet.session.cookie")),
mavenProject("project",
srcMainResources(
properties(
"""
some=value
server.servlet.session.cookie.name=fred
server.servlet.session.cookie.path=/cookie-monster
other=value
""",
"""
some=value
other=value
"""
)
)
)
);
}

@Test
void deleteParentPropertyInYaml() {
rewriteRun(
spec -> spec.recipe(new DeleteSpringProperty("server.servlet.session.cookie")),
mavenProject("project",
srcMainResources(
yaml(
"""
some: value
server:
servlet:
session:
cookie:
name: fred
path: /cookie-monster
other: value
""",
"""
some: value
other: value
"""
)
)
)
);
}

@Test
void deleteParentPropertyInCoalescedYaml() {
rewriteRun(
spec -> spec.recipe(new DeleteSpringProperty("server.servlet.session.cookie")),
mavenProject("project",
srcMainResources(
yaml(
"""
some: value
server.servlet.session.cookie.name: fred
server.servlet.session.cookie.path: /cookie-monster
other: value
""",
"""
some: value
other: value
"""
)
)
)
);
}

@Test
void deleteParentPropertyViaGlobAcrossFormats() {
rewriteRun(
spec -> spec.recipe(new DeleteSpringProperty("server.servlet.session.cookie.*")),
mavenProject("project",
srcMainResources(
properties(
"""
some=value
server.servlet.session.cookie.name=fred
server.servlet.session.cookie.path=/cookie-monster
other=value
""",
"""
some=value
other=value
"""
),
yaml(
"""
some: value
server:
servlet:
session:
cookie:
name: fred
path: /cookie-monster
other: value
""",
"""
some: value
other: value
"""
),
yaml(
"""
some: value
server.servlet.session.cookie.name: fred
server.servlet.session.cookie.path: /cookie-monster
other: value
""",
"""
some: value
other: value
"""
)
)
)
);
}

@Test
void doesNotModifyNonSpringYamlFiles() {
rewriteRun(
Expand Down
Loading