Skip to content

Commit

Permalink
Fix YAML JsonPathMatcher for filter expressions on child mappings (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden authored Feb 24, 2025
1 parent 8e85f01 commit bfa6f55
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ public Object visitLiteralExpression(JsonPathParser.LiteralExpressionContext ctx
}
}
// The filterExpression matched a result inside a Mapping. I.E. originalScope[?(filterExpression)]
if (originalScope instanceof Yaml.Mapping.Entry && ((Yaml.Mapping.Entry) originalScope).getValue() instanceof Yaml.Mapping) {
if (!matches.isEmpty() && originalScope instanceof Yaml.Mapping.Entry && ((Yaml.Mapping.Entry) originalScope).getValue() instanceof Yaml.Mapping) {
return originalScope;
}
return matches;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.openrewrite.yaml;

import org.junit.jupiter.api.Disabled;
import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.internal.StringUtils;
Expand Down Expand Up @@ -120,21 +120,21 @@ void complex() {
List.of(
//language=yaml
"""
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: application
spec:
template:
spec:
containers:
- image: nginx:latest
name: nginx
resources:
limits:
cpu: "64Mi"
"""
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: application
spec:
template:
spec:
containers:
- image: nginx:latest
name: nginx
resources:
limits:
cpu: "64Mi"
"""
),
List.of("cpu: \"64Mi\"")
);
Expand Down Expand Up @@ -816,10 +816,10 @@ void noMatchWithBinaryExpressions() {
);
}

@Disabled
@Test
void dontMatchThis() {
assertNotMatched("$..[?(@.task=='delete-this')]",
//language=yaml
List.of("""
foo:
bar:
Expand Down Expand Up @@ -860,16 +860,16 @@ void returnResultsWithVisitDocument() {
// assertThat(results).hasSize(1);
}

private void assertNotMatched(String jsonPath, List<String> before) {
private void assertNotMatched(@Language("jsonpath") String jsonPath, List<String> before) {
var results = visit(before, jsonPath, false);
assertThat(results).hasSize(0);
}

private void assertMatched(String jsonPath, List<String> before, List<String> after) {
private void assertMatched(@Language("jsonpath") String jsonPath, List<String> before, List<String> after) {
assertMatched(jsonPath, before, after, false);
}

private void assertMatched(String jsonPath, List<String> before, List<String> after,
private void assertMatched(@Language("jsonpath") String jsonPath, List<String> before, List<String> after,
@SuppressWarnings("SameParameterValue") boolean printMatches) {
var results = visit(before, jsonPath, printMatches);
assertThat(results).hasSize(after.size());
Expand All @@ -879,7 +879,7 @@ private void assertMatched(String jsonPath, List<String> before, List<String> af
}
}

private List<String> visit(List<String> before, String jsonPath, boolean printMatches) {
private List<String> visit(List<String> before, @Language("jsonpath") String jsonPath, boolean printMatches) {
var matcher = new JsonPathMatcher(jsonPath);
return new YamlVisitor<List<String>>() {
@Override
Expand Down

0 comments on commit bfa6f55

Please sign in to comment.