From d0bc9dc66bb7d99224e9278a1f9b53b75b4102aa Mon Sep 17 00:00:00 2001 From: Nick McKinney Date: Fri, 18 Oct 2024 13:21:03 -0400 Subject: [PATCH] =?UTF-8?q?adding=20a=20test=20case=20for=20MigrateWebMvcT?= =?UTF-8?q?agsToObservationConvention=20for=20t=E2=80=A6=20(#607)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * adding a test case for MigrateWebMvcTagsToObservationConvention for the case where the iterable passed to `Tags.and(...)` is *not* an identifier, and partially fixing it, BUT this commit still has a strange test failure with cyclically duplicating http request/response variables * adjusting precondition of MigrateWebMvcTagsToObservationConvention per @Laurens-W suggestion --- .../MigrateWebMvcTagsToObservationConvention.java | 6 +++--- ...MigrateWebMvcTagsToObservationConventionTest.java | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConvention.java b/src/main/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConvention.java index 1d9be451f..1d292fa3b 100644 --- a/src/main/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConvention.java +++ b/src/main/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConvention.java @@ -24,7 +24,7 @@ import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.MethodMatcher; -import org.openrewrite.java.search.UsesType; +import org.openrewrite.java.search.FindImplementations; import org.openrewrite.java.tree.*; import java.util.ArrayList; @@ -68,7 +68,7 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>(WEBMVCTAGSPROVIDER_FQ, true), new JavaVisitor() { + return Preconditions.check(new FindImplementations(WEBMVCTAGSPROVIDER_FQ), new JavaVisitor() { @Override public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { J.MethodDeclaration getTagsMethod = null; @@ -251,7 +251,7 @@ private Statement refactorTagsUsage(ExecutionContext ctx, org.openrewrite.java.t } return getMultiKeyValueStatement(ctx, coords, args, returnIdentifier); } else if (TAGS_AND_TAG_ITERABLE.matches(init) || TAGS_OF_TAG_ITERABLE.matches(init)) { - J.Identifier iterable = (J.Identifier) init.getArguments().get(0); + Expression iterable = init.getArguments().get(0); String template = "for (Tag tag : #{any()}) {\n" + " #{any()}.and(KeyValue.of(tag.getKey(), tag.getValue()));\n" + "}\n"; diff --git a/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConventionTest.java b/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConventionTest.java index a2f2dd0bb..ab6faf033 100644 --- a/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConventionTest.java +++ b/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConventionTest.java @@ -182,8 +182,13 @@ public Iterable getTags(HttpServletRequest request, HttpServletResponse res tags = tags.and("a", "b", "c", "d"); tags = Tags.and(Tag.of("a", "b"), staticTag); tags = tags.and(staticTags); + tags = tags.and(methodTags()); return tags; } + + private Tags methodTags() { + return staticTags; + } } """, """ @@ -219,8 +224,15 @@ public KeyValues getHighCardinalityKeyValues(ServerRequestObservationContext con for (Tag tag : staticTags) { values.and(KeyValue.of(tag.getKey(), tag.getValue())); } + for (Tag tag : methodTags()) { + values.and(KeyValue.of(tag.getKey(), tag.getValue())); + } return values; } + + private Tags methodTags() { + return staticTags; + } } """ )