Skip to content

Commit

Permalink
Fix @RequestMapping migration when there is more than one annotation …
Browse files Browse the repository at this point in the history
…parameter.
  • Loading branch information
jkschneider committed Aug 7, 2020
1 parent 6b2275d commit 32b3956
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

import javax.swing.text.html.Option;
import java.util.Optional;

import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -52,17 +53,22 @@ public J visitAnnotation(J.Annotation annotation) {
} else {
toAnnotationType = args.getArgs().stream().
map(arg -> arg.whenType(J.Assign.class)
.flatMap(assign -> assign.getVariable().whenType(J.Ident.class)
.flatMap(assign -> assign.getVariable()
.whenType(J.Ident.class)
.filter(key -> key.getSimpleName().equals("method"))
.flatMap(key -> Optional.ofNullable(assign.getAssignment().whenType(J.Ident.class)
.orElseGet(() -> assign.getAssignment().whenType(J.FieldAccess.class)
.map(J.FieldAccess::getName)
.orElse(null)))
.map(methodEnum -> {
maybeRemoveImport("org.springframework.web.bind.annotation.RequestMethod");
return methodEnum.getSimpleName().substring(0, 1) + methodEnum.getSimpleName().substring(1).toLowerCase() + "Mapping";
})))
.orElse("GetMapping"))
.flatMap(key ->
Optional.ofNullable(assign.getAssignment()
.whenType(J.Ident.class)
.orElseGet(() -> assign.getAssignment().whenType(J.FieldAccess.class)
.map(J.FieldAccess::getName)
.orElse(null)))
.map(methodEnum -> {
maybeRemoveImport("org.springframework.web.bind.annotation.RequestMethod");
return methodEnum.getSimpleName().substring(0, 1) + methodEnum.getSimpleName().substring(1).toLowerCase() + "Mapping";
}))
))
.filter(Optional::isPresent)
.map(Optional::get)
.findAny()
.orElse("GetMapping");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,71 @@ class NoRequestMappingAnnotationTest() : RefactorVisitorTestForParser<J.Compilat
"""
)

@Test
fun postMapping() = assertRefactored(
before = """
import java.util.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
@RestController
@RequestMapping("/users")
public class UsersController {
@RequestMapping(method = POST)
public ResponseEntity<List<String>> getUsersPost() {
return null;
}
}
""",
after = """
import java.util.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
public class UsersController {
@PostMapping
public ResponseEntity<List<String>> getUsersPost() {
return null;
}
}
"""
)

@Test
fun hasValueParameter() = assertRefactored(
before = """
import java.util.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
public class UsersController {
@RequestMapping(value = "/user/{userId}/edit", method = RequestMethod.POST)
public ResponseEntity<List<String>> getUsersPost(String userId) {
return null;
}
}
""",
after = """
import java.util.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
public class UsersController {
@PostMapping("/user/{userId}/edit")
public ResponseEntity<List<String>> getUsersPost(String userId) {
return null;
}
}
"""
)

@Issue("#3")
@Test
fun requestMappingWithMultipleMethods() = assertUnchanged(
Expand Down

0 comments on commit 32b3956

Please sign in to comment.