Skip to content

Commit b083cce

Browse files
committed
Fix #11 NoRequestMappingAnnotation produces broken annotations when there are multiple values
1 parent be9494b commit b083cce

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
package org.openrewrite.java.spring;
1717

1818
import org.openrewrite.AutoConfigure;
19+
import org.openrewrite.Formatting;
1920
import org.openrewrite.java.JavaRefactorVisitor;
2021
import org.openrewrite.java.tree.Expression;
2122
import org.openrewrite.java.tree.J;
2223
import org.openrewrite.java.tree.JavaType;
2324

2425
import java.util.Optional;
26+
import java.util.stream.Collectors;
2527

2628
import static java.util.stream.Collectors.toList;
2729
import static org.openrewrite.Formatting.EMPTY;
@@ -63,7 +65,7 @@ public J visitAnnotation(J.Annotation annotation) {
6365
.orElse(null)))
6466
.map(methodEnum -> {
6567
maybeRemoveImport("org.springframework.web.bind.annotation.RequestMethod");
66-
return methodEnum.getSimpleName().substring(0, 1) + methodEnum.getSimpleName().substring(1).toLowerCase() + "Mapping";
68+
return methodEnum.getSimpleName().charAt(0) + methodEnum.getSimpleName().substring(1).toLowerCase() + "Mapping";
6769
}))
6870
))
6971
.filter(Optional::isPresent)
@@ -89,6 +91,28 @@ public J visitAnnotation(J.Annotation annotation) {
8991
.orElse(arg))
9092
.collect(toList()));
9193

94+
// If more than one argument remains, ensure that there's a "value =" before the value argument
95+
if(args.getArgs().size() > 1) {
96+
args = args.withArgs(args.getArgs().stream()
97+
.map(it -> {
98+
if(it instanceof J.Literal) {
99+
return new J.Assign(
100+
randomId(),
101+
J.Ident.build(
102+
randomId(),
103+
"value",
104+
null,
105+
Formatting.format("", " ")),
106+
((J.Literal) it).withFormatting(Formatting.format(" ")),
107+
JavaType.Class.build("java.lang.String"),
108+
EMPTY);
109+
} else {
110+
return it;
111+
}
112+
})
113+
.collect(toList()));
114+
}
115+
92116
// remove the argument parentheses altogether
93117
if (args.getArgs().isEmpty()) {
94118
args = null;

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.openrewrite.java.spring
1717

1818
import org.junit.jupiter.api.Test
19-
import org.openrewrite.Issue
2019
import org.openrewrite.RefactorVisitor
2120
import org.openrewrite.RefactorVisitorTestForParser
2221
import org.openrewrite.java.JavaParser
@@ -159,7 +158,6 @@ class NoRequestMappingAnnotationTest : RefactorVisitorTestForParser<J.Compilatio
159158
"""
160159
)
161160

162-
@Issue("#3")
163161
@Test
164162
fun requestMappingWithMultipleMethods() = assertUnchanged(
165163
before = """
@@ -179,4 +177,38 @@ class NoRequestMappingAnnotationTest : RefactorVisitorTestForParser<J.Compilatio
179177
}
180178
"""
181179
)
180+
181+
@Test
182+
fun multipleParameters() = assertRefactored(
183+
before = """
184+
import java.util.*;
185+
import org.springframework.http.ResponseEntity;
186+
import org.springframework.web.bind.annotation.*;
187+
import org.springframework.http.MediaType;
188+
189+
@RestController
190+
@RequestMapping("/users")
191+
public class UsersController {
192+
@RequestMapping(value = "/user/{userId}/edit", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
193+
public ResponseEntity<List<String>> getUsersPost(String userId) {
194+
return null;
195+
}
196+
}
197+
""",
198+
after = """
199+
import java.util.*;
200+
import org.springframework.http.ResponseEntity;
201+
import org.springframework.web.bind.annotation.*;
202+
import org.springframework.http.MediaType;
203+
204+
@RestController
205+
@RequestMapping("/users")
206+
public class UsersController {
207+
@PostMapping(value = "/user/{userId}/edit", produces = { MediaType.APPLICATION_JSON_VALUE })
208+
public ResponseEntity<List<String>> getUsersPost(String userId) {
209+
return null;
210+
}
211+
}
212+
"""
213+
)
182214
}

0 commit comments

Comments
 (0)