From bfa6f55c8d07a9c27ea881d781c24f76687f6129 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Mon, 24 Feb 2025 09:37:07 +0100 Subject: [PATCH] Fix YAML `JsonPathMatcher` for filter expressions on child mappings (#5088) --- .../org/openrewrite/yaml/JsonPathMatcher.java | 2 +- .../openrewrite/yaml/JsonPathMatcherTest.java | 42 +++++++++---------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/rewrite-yaml/src/main/java/org/openrewrite/yaml/JsonPathMatcher.java b/rewrite-yaml/src/main/java/org/openrewrite/yaml/JsonPathMatcher.java index 5da735f9cdd..c2beab5d547 100644 --- a/rewrite-yaml/src/main/java/org/openrewrite/yaml/JsonPathMatcher.java +++ b/rewrite-yaml/src/main/java/org/openrewrite/yaml/JsonPathMatcher.java @@ -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; diff --git a/rewrite-yaml/src/test/java/org/openrewrite/yaml/JsonPathMatcherTest.java b/rewrite-yaml/src/test/java/org/openrewrite/yaml/JsonPathMatcherTest.java index e1f7728fd42..81d495075f5 100644 --- a/rewrite-yaml/src/test/java/org/openrewrite/yaml/JsonPathMatcherTest.java +++ b/rewrite-yaml/src/test/java/org/openrewrite/yaml/JsonPathMatcherTest.java @@ -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; @@ -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\"") ); @@ -816,10 +816,10 @@ void noMatchWithBinaryExpressions() { ); } - @Disabled @Test void dontMatchThis() { assertNotMatched("$..[?(@.task=='delete-this')]", + //language=yaml List.of(""" foo: bar: @@ -860,16 +860,16 @@ void returnResultsWithVisitDocument() { // assertThat(results).hasSize(1); } - private void assertNotMatched(String jsonPath, List before) { + private void assertNotMatched(@Language("jsonpath") String jsonPath, List before) { var results = visit(before, jsonPath, false); assertThat(results).hasSize(0); } - private void assertMatched(String jsonPath, List before, List after) { + private void assertMatched(@Language("jsonpath") String jsonPath, List before, List after) { assertMatched(jsonPath, before, after, false); } - private void assertMatched(String jsonPath, List before, List after, + private void assertMatched(@Language("jsonpath") String jsonPath, List before, List after, @SuppressWarnings("SameParameterValue") boolean printMatches) { var results = visit(before, jsonPath, printMatches); assertThat(results).hasSize(after.size()); @@ -879,7 +879,7 @@ private void assertMatched(String jsonPath, List before, List af } } - private List visit(List before, String jsonPath, boolean printMatches) { + private List visit(List before, @Language("jsonpath") String jsonPath, boolean printMatches) { var matcher = new JsonPathMatcher(jsonPath); return new YamlVisitor>() { @Override