Skip to content

MaybeAddJakartaServletApi - does not actually respect its DoesNotIncludeDependency precondition #254

@nmck257

Description

@nmck257

What version of OpenRewrite are you using?

I am using

  • OpenRewrite v8.1.5
  • Maven/Gradle plugin v1.2.3 RewriteTest
  • rewrite-migrate-java v2.0.6

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

import org.junit.jupiter.api.Test;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.mavenProject;
import static org.openrewrite.java.Assertions.srcMainJava;
import static org.openrewrite.maven.Assertions.pomXml;

class MaybeAddJakartaServletApiTest implements RewriteTest {
    @Override
    public void defaults(RecipeSpec spec) {
        spec.recipe(new MaybeAddJakartaServletApi())
                .parser(JavaParser.fromJavaVersion().dependsOn("package javax.servlet;\npublic class Filter {}")));
    }

    @Test
    void hasSpringBootStarterWeb() {
        rewriteRun(
                mavenProject("my-project",
                        srcMainJava(java("""
                        import javax.servlet.Filter;
                        class A {
                            Filter foo = null;
                        }
                        """)),
                        pomXml("""
                        <?xml version="1.0" encoding="UTF-8"?>
                        <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
                          <modelVersion>4.0.0</modelVersion>
                          <groupId>org.sample</groupId>
                          <artifactId>sample</artifactId>
                          <version>1.0.0</version>

                          <dependencies>
                            <dependency>
                              <groupId>org.springframework.boot</groupId>
                              <artifactId>spring-boot-starter-web</artifactId>
                              <version>2.7.0</version>
                            </dependency>
                          </dependencies>

                        </project>
                        """)
                )
        );
    }

    @Test
    void doesNotHaveSpringBootStarterWeb() {
        rewriteRun(
                mavenProject("my-project",
                        srcMainJava(java("""
                        import javax.servlet.Filter;
                        class A {
                            Filter foo = null;
                        }
                        """)),
                        pomXml("""
                        <?xml version="1.0" encoding="UTF-8"?>
                        <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
                          <modelVersion>4.0.0</modelVersion>
                          <groupId>org.sample</groupId>
                          <artifactId>sample</artifactId>
                          <version>1.0.0</version>

                          <dependencies>
                          </dependencies>

                        </project>
                                """,
                        """
                        <?xml version="1.0" encoding="UTF-8"?>
                        <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
                          <modelVersion>4.0.0</modelVersion>
                          <groupId>org.sample</groupId>
                          <artifactId>sample</artifactId>
                          <version>1.0.0</version>

                          <dependencies>
                            <dependency>
                              <groupId>jakarta.servlet</groupId>
                              <artifactId>jakarta.servlet-api</artifactId>
                              <version>6.0.0</version>
                            </dependency>
                          </dependencies>

                        </project>
                        """)
                )
        );
    }
}

What did you expect to see?

Passing tests

What did you see instead?

hasSpringBootStarterWeb fails, having added jakarta.servlet-api

Are you interested in contributing a fix to OpenRewrite?

Yeah. I thought it would be trivial (moving the AddDependency invocation from getRecipeList into the getVisitor), but, that actually doesn't work, because AddDependency is a ScanningRecipe. So, looks like it needs a bigger refactor to turn MaybeAddJakartaServletApi into a ScanningRecipe which composes with AddDependency. Still planning to contribute that (larger) fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions