Skip to content

Commit e042a67

Browse files
committed
Preserve GString/StringTemplate structure in ChangeDependency when version is unchanged
When `ChangeDependency` renames a dependency that uses a GString or Kotlin StringTemplate (e.g. `"group:artifact:${version}"`), and the version is not being changed, the GString/StringTemplate structure is now preserved instead of being collapsed to a literal with a pinned version. Previously, the GString and Kotlin StringTemplate code paths unconditionally applied `newVersion` without checking the `overrideManagedVersion` guard that the literal path already had. This caused recipes like the Spring Boot 4 migration to pin hardcoded versions on dependencies that used property references for their version (e.g. `${springBootVersion}`). The fix adds the same `overrideManagedVersion` guard to both the GString and Kotlin StringTemplate paths, and when only group/artifact changes (not version), updates only the literal prefix while preserving the interpolated structure.
1 parent 93bf91a commit e042a67

2 files changed

Lines changed: 38 additions & 12 deletions

File tree

rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte
302302
if (!StringUtils.isBlank(newArtifactId) && !updated.getArtifactId().equals(newArtifactId)) {
303303
updated = updated.withGav(updated.getGav().withArtifactId(newArtifactId));
304304
}
305-
if (!StringUtils.isBlank(newVersion)) {
305+
if (!StringUtils.isBlank(newVersion) && (!StringUtils.isBlank(original.getVersion()) || Boolean.TRUE.equals(overrideManagedVersion))) {
306306
String resolvedVersion;
307307
try {
308308
resolvedVersion = new DependencyVersionSelector(metadataFailures, gradleProject, null)
@@ -315,10 +315,23 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte
315315
}
316316
}
317317
if (original != updated) {
318-
String replacement = DependencyNotation.toStringNotation(updated);
319-
J.Literal newLiteral = literal.withValue(replacement)
320-
.withValueSource(gstring.getDelimiter() + replacement + gstring.getDelimiter());
321-
m = m.withArguments(singletonList(newLiteral));
318+
if (Objects.equals(original.getVersion(), updated.getVersion())) {
319+
// Version unchanged: preserve GString structure, only update the literal prefix
320+
String oldGav = original.getGroupId() + ":" + original.getArtifactId();
321+
String newGav = updated.getGroupId() + ":" + updated.getArtifactId();
322+
String oldValue = (String) literal.getValue();
323+
String updatedValue = oldValue.replace(oldGav, newGav);
324+
J.Literal updatedLiteral = literal.withValue(updatedValue).withValueSource(updatedValue);
325+
m = m.withArguments(singletonList(
326+
gstring.withStrings(ListUtils.mapFirst(strings, s -> updatedLiteral))
327+
));
328+
} else {
329+
// Version changed: collapse GString to a literal
330+
String replacement = DependencyNotation.toStringNotation(updated);
331+
J.Literal newLiteral = literal.withValue(replacement)
332+
.withValueSource(gstring.getDelimiter() + replacement + gstring.getDelimiter());
333+
m = m.withArguments(singletonList(newLiteral));
334+
}
322335
}
323336
}
324337
}
@@ -595,7 +608,7 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte
595608
if (!StringUtils.isBlank(newArtifactId) && !updated.getArtifactId().equals(newArtifactId)) {
596609
updated = updated.withGav(updated.getGav().withArtifactId(newArtifactId));
597610
}
598-
if (!StringUtils.isBlank(newVersion)) {
611+
if (!StringUtils.isBlank(newVersion) && (!StringUtils.isBlank(original.getVersion()) || Boolean.TRUE.equals(overrideManagedVersion))) {
599612
String resolvedVersion;
600613
try {
601614
resolvedVersion = new DependencyVersionSelector(metadataFailures, gradleProject, null)
@@ -608,10 +621,23 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte
608621
}
609622
}
610623
if (original != updated) {
611-
String replacement = DependencyNotation.toStringNotation(updated);
612-
J.Literal newLiteral = literal.withValue(replacement)
613-
.withValueSource(template.getDelimiter() + replacement + template.getDelimiter());
614-
m = m.withArguments(singletonList(newLiteral));
624+
if (Objects.equals(original.getVersion(), updated.getVersion())) {
625+
// Version unchanged: preserve StringTemplate structure, only update the literal prefix
626+
String oldGav = original.getGroupId() + ":" + original.getArtifactId();
627+
String newGav = updated.getGroupId() + ":" + updated.getArtifactId();
628+
String oldValue = (String) literal.getValue();
629+
String updatedValue = oldValue.replace(oldGav, newGav);
630+
J.Literal updatedLiteral = literal.withValue(updatedValue).withValueSource(updatedValue);
631+
m = m.withArguments(singletonList(
632+
template.withStrings(ListUtils.mapFirst(strings, s -> updatedLiteral))
633+
));
634+
} else {
635+
// Version changed: collapse StringTemplate to a literal
636+
String replacement = DependencyNotation.toStringNotation(updated);
637+
J.Literal newLiteral = literal.withValue(replacement)
638+
.withValueSource(template.getDelimiter() + replacement + template.getDelimiter());
639+
m = m.withArguments(singletonList(newLiteral));
640+
}
615641
}
616642
}
617643
}

rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ implementation platform("commons-lang:commons-lang:${version}")
264264
265265
def version = '2.6'
266266
dependencies {
267-
implementation platform("org.apache.commons:commons-lang3:3.11")
267+
implementation platform("org.apache.commons:commons-lang3:${version}")
268268
}
269269
"""
270270
)
@@ -574,7 +574,7 @@ void kotlinDslStringInterpolation() {
574574
575575
dependencies {
576576
val commonsLangVersion = "2.6"
577-
implementation("org.apache.commons:commons-lang3:3.11")
577+
implementation("org.apache.commons:commons-lang3:${commonsLangVersion}")
578578
}
579579
"""
580580
)

0 commit comments

Comments
 (0)