JavaxWebXmlToJakartaWebXml uses FindAndReplace which partially breaks Jakarta EE 10 migration
What version of OpenRewrite are you using?
Rewrite-Recipe-Bom 3.27.1
How are you running OpenRewrite?
JUnit-Tests/Maven Plugin
What is the smallest, simplest way to reproduce the problem?
Call the recipe org.openrewrite.java.migrate.jakarta.JakartaEE10 on a Maven project with a web.xml within src/main/webapp/WEB-INF/web.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>example-project</display-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
</web-app>
What did you expect to see?
XML should properly be converted to version 6.0:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
version="6.0">
<display-name>example-project</display-name>
<context-param>
<param-name>jakarta.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
</web-app>
What did you see instead?
XML got converted to Version 5.0:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
<display-name>example-project</display-name>
<context-param>
<param-name>jakarta.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
</web-app>
What is the cause of the issue?
From what I can tell, the issue lies within the recipe itself.
- The order of calls is JakartaEE10 -> JavaxMigrationToJakarta -> Faces2xMigrationToJakartaFaces3x -> JavaxWebXmlToJakartaWebXml
- JavaxWebXmlToJakartaWebXml then calls ChangeTagAttribute and FindAndReplace
- Later, the JakartaEE10 calls Faces3xMigrationToFaces4x which in turn calls JakartaWebXml6
- However, if FindAndReplace replaced even a single instance of
javaxwithjakarta, theweb.xmlis now treated as Plain Text and JakartaWebXml6 cannot properly update theweb.xmlby applying its own ChangeTagAttribute-Calls
Are you interested in [contributing a fix to OpenRewrite]
I can provide a test, but that's as far my skills go I'm afraid.
@Test
void correctlySetsVersionIfReplacementHappendBefore() {
rewriteRun(
spec -> spec.recipeFromResources("org.openrewrite.java.migrate.jakarta.JakartaEE10"),
xml(
"""
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>example-project</display-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
</web-app>
""",
"""
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
version="6.0">
<display-name>example-project</display-name>
<context-param>
<param-name>jakarta.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
</web-app>
""",
spec -> spec.path("src/main/webapp/WEB-INF/web.xml")));
}
Thanks for the analysis! That needs a proper fix indeed.
Hi, I’d like to work on this issue.
I’ll first add the provided regression test to JavaxWebXmlToJakartaWebXmlTest and confirm that running JakartaEE10 leaves the XML at version 5.0.
I’ll then investigate replacing the plain-text FindAndReplace step with an XML-aware transformation so that the source remains an XML LST and the later JakartaWebXml6 recipe can update it correctly.
Please let me know if this approach sounds appropriate.
hi! Thanks again for offering to help, and sharing your proposed plan up front. Sounds good to me, and I look forward to your work.