Skip to content

Commit 32b3956

Browse files
committed
Fix @RequestMapping migration when there is more than one annotation parameter.
1 parent 6b2275d commit 32b3956

File tree

2 files changed

+81
-10
lines changed

2 files changed

+81
-10
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.openrewrite.java.tree.J;
2222
import org.openrewrite.java.tree.JavaType;
2323

24+
import javax.swing.text.html.Option;
2425
import java.util.Optional;
2526

2627
import static java.util.stream.Collectors.toList;
@@ -52,17 +53,22 @@ public J visitAnnotation(J.Annotation annotation) {
5253
} else {
5354
toAnnotationType = args.getArgs().stream().
5455
map(arg -> arg.whenType(J.Assign.class)
55-
.flatMap(assign -> assign.getVariable().whenType(J.Ident.class)
56+
.flatMap(assign -> assign.getVariable()
57+
.whenType(J.Ident.class)
5658
.filter(key -> key.getSimpleName().equals("method"))
57-
.flatMap(key -> Optional.ofNullable(assign.getAssignment().whenType(J.Ident.class)
58-
.orElseGet(() -> assign.getAssignment().whenType(J.FieldAccess.class)
59-
.map(J.FieldAccess::getName)
60-
.orElse(null)))
61-
.map(methodEnum -> {
62-
maybeRemoveImport("org.springframework.web.bind.annotation.RequestMethod");
63-
return methodEnum.getSimpleName().substring(0, 1) + methodEnum.getSimpleName().substring(1).toLowerCase() + "Mapping";
64-
})))
65-
.orElse("GetMapping"))
59+
.flatMap(key ->
60+
Optional.ofNullable(assign.getAssignment()
61+
.whenType(J.Ident.class)
62+
.orElseGet(() -> assign.getAssignment().whenType(J.FieldAccess.class)
63+
.map(J.FieldAccess::getName)
64+
.orElse(null)))
65+
.map(methodEnum -> {
66+
maybeRemoveImport("org.springframework.web.bind.annotation.RequestMethod");
67+
return methodEnum.getSimpleName().substring(0, 1) + methodEnum.getSimpleName().substring(1).toLowerCase() + "Mapping";
68+
}))
69+
))
70+
.filter(Optional::isPresent)
71+
.map(Optional::get)
6672
.findAny()
6773
.orElse("GetMapping");
6874

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,71 @@ class NoRequestMappingAnnotationTest() : RefactorVisitorTestForParser<J.Compilat
9393
"""
9494
)
9595

96+
@Test
97+
fun postMapping() = assertRefactored(
98+
before = """
99+
import java.util.*;
100+
import org.springframework.http.ResponseEntity;
101+
import org.springframework.web.bind.annotation.*;
102+
import static org.springframework.web.bind.annotation.RequestMethod.POST;
103+
104+
@RestController
105+
@RequestMapping("/users")
106+
public class UsersController {
107+
@RequestMapping(method = POST)
108+
public ResponseEntity<List<String>> getUsersPost() {
109+
return null;
110+
}
111+
}
112+
""",
113+
after = """
114+
import java.util.*;
115+
import org.springframework.http.ResponseEntity;
116+
import org.springframework.web.bind.annotation.*;
117+
118+
@RestController
119+
@RequestMapping("/users")
120+
public class UsersController {
121+
@PostMapping
122+
public ResponseEntity<List<String>> getUsersPost() {
123+
return null;
124+
}
125+
}
126+
"""
127+
)
128+
129+
@Test
130+
fun hasValueParameter() = assertRefactored(
131+
before = """
132+
import java.util.*;
133+
import org.springframework.http.ResponseEntity;
134+
import org.springframework.web.bind.annotation.*;
135+
136+
@RestController
137+
@RequestMapping("/users")
138+
public class UsersController {
139+
@RequestMapping(value = "/user/{userId}/edit", method = RequestMethod.POST)
140+
public ResponseEntity<List<String>> getUsersPost(String userId) {
141+
return null;
142+
}
143+
}
144+
""",
145+
after = """
146+
import java.util.*;
147+
import org.springframework.http.ResponseEntity;
148+
import org.springframework.web.bind.annotation.*;
149+
150+
@RestController
151+
@RequestMapping("/users")
152+
public class UsersController {
153+
@PostMapping("/user/{userId}/edit")
154+
public ResponseEntity<List<String>> getUsersPost(String userId) {
155+
return null;
156+
}
157+
}
158+
"""
159+
)
160+
96161
@Issue("#3")
97162
@Test
98163
fun requestMappingWithMultipleMethods() = assertUnchanged(

0 commit comments

Comments
 (0)