Skip to content

Commit 610c949

Browse files
committed
Don't remove @ModelAttribute on methods
1 parent 1c94c24 commit 610c949

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/main/java/org/openrewrite/spring/ImplicitWebAnnotationNames.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public J visitAnnotation(J.Annotation annotation) {
5151
J.Annotation a = refactor(annotation, super::visitAnnotation);
5252

5353
if(PARAM_ANNOTATIONS.stream().anyMatch(annClass -> isOfClassType(annotation.getType(), annClass)) &&
54-
annotation.getArgs() != null && nameArgumentValue(annotation).isPresent()) {
54+
annotation.getArgs() != null && nameArgumentValue(annotation).isPresent() &&
55+
// ModelAttribute can be on methods as well as parameters
56+
getCursor().getParentOrThrow().getTree() instanceof J.VariableDecls) {
5557
J.Annotation.Arguments args = a.getArgs();
5658

5759
if (args == null) {
@@ -74,6 +76,7 @@ public J visitAnnotation(J.Annotation annotation) {
7476
args = null;
7577
}
7678

79+
7780
nameArgumentValue(a).ifPresent(value -> andThen(new RenameVariable(
7881
getCursor().getParentOrThrow().<J.VariableDecls>getTree().getVars().get(0),
7982
(String) value.getValue())));

src/test/kotlin/org/openrewrite/spring/ImplicitWebAnnotationNamesTest.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,33 @@ class ImplicitWebAnnotationNamesTest {
9999
}
100100
""")
101101
}
102+
103+
@Test
104+
fun dontRemoveModelAttributeOnMethods() {
105+
val controller = jp.parse("""
106+
import org.springframework.web.bind.annotation.*;
107+
import java.util.*;
108+
109+
public class UsersController {
110+
@ModelAttribute("types")
111+
public Collection<String> populateUserTypes() {
112+
return Arrays.asList("free", "premium");
113+
}
114+
}
115+
""".trimIndent())
116+
117+
val fixed = controller.refactor().visit(ImplicitWebAnnotationNames()).fix().fixed
118+
119+
assertRefactored(fixed, """
120+
import org.springframework.web.bind.annotation.*;
121+
import java.util.*;
122+
123+
public class UsersController {
124+
@ModelAttribute("types")
125+
public Collection<String> populateUserTypes() {
126+
return Arrays.asList("free", "premium");
127+
}
128+
}
129+
""")
130+
}
102131
}

0 commit comments

Comments
 (0)