Skip to content

Commit

Permalink
Fix NoRequestMapping throwing an error when method argument is an array
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Mar 4, 2024
1 parent b1bcc24 commit 8fa0f82
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
import org.openrewrite.java.ChangeType;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.Space;

import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;

Expand Down Expand Up @@ -152,8 +154,16 @@ private String requestMethodType(@Nullable J.Assignment assignment) {
} else if (methodArgumentHasSingleType(assignment)) {
if(assignment.getAssignment() instanceof J.NewArray) {
J.NewArray newArray = (J.NewArray) assignment.getAssignment();
assert newArray.getInitializer() != null;
return ((J.FieldAccess) newArray.getInitializer().get(0)).getSimpleName();
List<Expression> initializer = newArray.getInitializer();
if(initializer == null || initializer.size() != 1) {
return null;
}
Expression methodName = initializer.get(0);
if(methodName instanceof J.Identifier) {
return ((J.Identifier)methodName).getSimpleName();
} else if(methodName instanceof J.FieldAccess) {
return ((J.FieldAccess) methodName).getSimpleName();
}
} else if(assignment.getAssignment() instanceof J.Identifier) {
return ((J.Identifier) assignment.getAssignment()).getSimpleName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,41 @@ public ResponseEntity<List<String>> getUsersPost() {
)
);
}

@Test
void methodInArray() {
//language=java
rewriteRun(
java(
"""
import java.util.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
public class UsersController {
@RequestMapping(value = { "/users" }, method = { POST }, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<List<String>> getUsersPost() {
return null;
}
}
""",
"""
import java.util.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
public class UsersController {
@PostMapping(value = { "/users" }, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<List<String>> getUsersPost() {
return null;
}
}
"""
)
);
}
}

0 comments on commit 8fa0f82

Please sign in to comment.