Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnnecessarySpringExtension recipe removes too much - Only SpringExtension.class should be removed #678

Open
maecval opened this issue Feb 14, 2025 · 2 comments
Labels
bug Something isn't working test provided

Comments

@maecval
Copy link

maecval commented Feb 14, 2025

What version of OpenRewrite are you using?

I am using

  • OpenRewrite 8.45.4
  • Maven plugin 6.1.3 (rewrite-maven-plugin:6.1.3)
  • rewrite-spring 6.1.0

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project. Command used to run it:

mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-spring:RELEASE -Drewrite.activeRecipes=org.openrewrite.java.spring.boot2.UnnecessarySpringExtension -Drewrite.exportDatatables=true

What is the smallest, simplest way to reproduce the problem?

If a SpringBootTest class has an additional ExtendWith annotation which contains additional test extension classes the recipe removes the whole line istsead of just removing the SpringExtension.class and keeping the rest.
Status before execution of recipe:

@ExtendWith({ SpringExtension.class, PactConsumerTestExt.class})
@SpringBootTest(...)
public class AnyTest {
    ...
}

What did you expect to see?

expected status after execution:

@ExtendWith({ PactConsumerTestExt.class})
@SpringBootTest(...)
public class AnyTest {
    ...
}

What did you see instead?

status after execution of recipe:

@SpringBootTest(...)
public class AnyTest {
    ...
}

What is the full stack trace of any errors you encountered?

Are you interested in contributing a fix to OpenRewrite?

Maybe :-)

@maecval maecval added the bug Something isn't working label Feb 14, 2025
@timtebeek
Copy link
Contributor

hi @maecval ; Thanks for the report! Can indeed confirm the issue with this unit test added to

@Issue("/~https://github.com/openrewrite/rewrite-spring/issues/678")
@Test
void retainSecondary() {
    //language=java
    rewriteRun(
      java(
        """
          import org.junit.jupiter.api.extension.BeforeAllCallback;
          import org.junit.jupiter.api.extension.ExtensionContext;
          class AnotherExtension implements BeforeAllCallback {
              void beforeAll(ExtensionContext context) {
              }
          }
          """,
        SourceSpec::skip
      ),
      java(
        """
          import org.junit.jupiter.api.extension.ExtendWith;
          import org.springframework.boot.test.context.SpringBootTest;
          import org.springframework.test.context.junit.jupiter.SpringExtension;

          @SpringBootTest
          @ExtendWith({SpringExtension.class, AnotherExtension.class})
          class Test {
          }
          """,
        """
          import org.springframework.boot.test.context.SpringBootTest;

          @SpringBootTest
          @ExtendWith({AnotherExtension.class})
          class Test {
          }
          """
      )
    );
}

@timtebeek
Copy link
Contributor

Would welcome a draft PR that adds that test, and then whatever you can do to explore a fix. I imagine we'll want to explore the ExtendsWith annotation here:

if (!FindAnnotations.find(c, EXTEND_WITH_SPRING_EXTENSION_ANNOTATION_PATTERN).isEmpty()) {

As a simplest case we could not make any change at all when there is more than one argument.
If you want to go more advanced you can remove the target argument from the annotation.

@timtebeek timtebeek moved this to Backlog in OpenRewrite Feb 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working test provided
Projects
Status: Backlog
Development

No branches or pull requests

2 participants