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

Do not override version property placeholder in UpgradeDependencyVersion #4579

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,17 @@ private String resolveVersion(String version) {

public @Nullable TreeVisitor<Xml, ExecutionContext> upgradeVersion(ExecutionContext ctx, Xml.Tag tag, @Nullable String requestedVersion, String groupId, String artifactId, String version2) throws MavenDownloadingException {
String newerVersion = findNewerVersion(groupId, artifactId, version2, ctx);
String requestedGroupId = getRequestedGroupIdForArtifact(artifactId);
if (newerVersion == null) {
return null;
} else if (requestedVersion != null && requestedVersion.startsWith("${")) {
//noinspection unchecked
return (TreeVisitor<Xml, ExecutionContext>) new ChangePropertyValue(requestedVersion.substring(2, requestedVersion.length() - 1), newerVersion, overrideManagedVersion, false)
.getVisitor();
} else if (requestedVersion != null && requestedGroupId != null && requestedGroupId.startsWith("${")) {
//noinspection unchecked
return (TreeVisitor<Xml, ExecutionContext>) new ChangePropertyValue(requestedGroupId.substring(2, requestedGroupId.length() - 1), newerVersion, overrideManagedVersion, false)
.getVisitor();
} else {
Xml.Tag childVersionTag = tag.getChild("version").orElse(null);
if (childVersionTag != null) {
Expand All @@ -402,6 +407,23 @@ private String resolveVersion(String version) {
return MavenDependency.findNewerVersion(groupId, artifactId, version, getResolutionResult(), metadataFailures,
versionComparator, ctx);
}

private @Nullable String getRequestedGroupIdForArtifact(String artifactId) {
for (ManagedDependency managedDependency : getResolutionResult().getPom().getRequested().getDependencyManagement()) {
if (managedDependency instanceof ManagedDependency.Imported) {
ManagedDependency.Imported imported = (ManagedDependency.Imported) managedDependency;
if (artifactId.equals(imported.getGav().getArtifactId())) {
return imported.getGav().getVersion();
}
} else if (managedDependency instanceof ManagedDependency.Defined) {
ManagedDependency.Defined defined = (ManagedDependency.Defined) managedDependency;
if (artifactId.equals(defined.getGav().getArtifactId())) {
return defined.getGav().getVersion();
}
}
}
return null;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,82 @@

class UpgradeDependencyVersionTest implements RewriteTest {

@Test
@Issue("https://github.com/openrewrite/rewrite-spring/issues/474")
void upgradePropertyAdditionalCheck() {
rewriteRun(
spec -> spec.recipe(new UpgradeDependencyVersion("org.springframework", "*", "5.2.x", "",
false, null)).expectedCyclesThatMakeChanges(2),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check once more whether we can reduce the amount of cycles to 1?
Enforcing that all recipes complete in 1 cycle is something that's being looked at

mavenProject("project",
//language=xml
pomXml(
"""
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.openrewrite.example</groupId>
<artifactId>example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>example</name>
<description>Demo project for Spring Boot</description>
<properties>
<spring.boot.version>2.2.13.RELEASE</spring.boot.version>
<spring.jms.version>5.2.22.RELEASE</spring.jms.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.jms.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
""",
"""
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.openrewrite.example</groupId>
<artifactId>example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>example</name>
<description>Demo project for Spring Boot</description>
<properties>
<spring.boot.version>2.2.13.RELEASE</spring.boot.version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This version property should be updated too, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed in the ticket it expects to upgade also the boot dependency, but this should be done in the rewrite-spring repository in this file resources/META-INF/rewrite/spring-framework-53.yml file

In the rewrite-spring repository the recipe is invoked with the following properties
groupId: org.springframework
artifactId: "*"
newVersion: 5.3.x

I used the same for this test.
To upgrade the boot dependency, something like this should be added:

  • org.openrewrite.java.dependencies.UpgradeDependencyVersion:
    groupId: org.springframework.boot
    artifactId: "*"
    newVersion: 2.x

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, didn't read the group id properly, you are right :)

<spring.jms.version>5.2.25.RELEASE</spring.jms.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.jms.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
"""
)
)
);
}

@Test
void doNotOverrideImplicitProperty() {
rewriteRun(
Expand Down