Skip to content

Commit 1aef8e6

Browse files
polysantiagoPablo Santiago
authored andcommitted
Fixed issues with wrong path variables names
1 parent cf4fa66 commit 1aef8e6

File tree

2 files changed

+96
-20
lines changed

2 files changed

+96
-20
lines changed

src/main/java/io/github/polysantiago/spring/rest/RestClientInterceptorHelper.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
package io.github.polysantiago.spring.rest;
22

3+
import static java.util.Collections.emptyMap;
4+
import static java.util.Collections.singletonList;
5+
import static java.util.stream.Collectors.toList;
6+
import static java.util.stream.Collectors.toMap;
7+
import static org.apache.commons.lang3.ArrayUtils.isNotEmpty;
8+
import static org.apache.commons.lang3.StringUtils.substringAfter;
9+
import static org.apache.commons.lang3.StringUtils.substringBefore;
10+
import static org.springframework.http.MediaType.parseMediaType;
11+
import static org.springframework.util.CollectionUtils.isEmpty;
12+
313
import io.github.polysantiago.spring.rest.support.MethodParameters;
14+
import java.lang.reflect.Method;
15+
import java.net.URI;
16+
import java.util.Collection;
17+
import java.util.List;
18+
import java.util.Map;
19+
import java.util.stream.Stream;
420
import lombok.NonNull;
521
import lombok.Setter;
622
import lombok.experimental.Accessors;
@@ -16,24 +32,14 @@
1632
import org.springframework.http.RequestEntity.BodyBuilder;
1733
import org.springframework.util.LinkedMultiValueMap;
1834
import org.springframework.util.MultiValueMap;
19-
import org.springframework.web.bind.annotation.*;
35+
import org.springframework.web.bind.annotation.PathVariable;
36+
import org.springframework.web.bind.annotation.RequestBody;
37+
import org.springframework.web.bind.annotation.RequestHeader;
38+
import org.springframework.web.bind.annotation.RequestMapping;
39+
import org.springframework.web.bind.annotation.RequestMethod;
40+
import org.springframework.web.bind.annotation.RequestParam;
2041
import org.springframework.web.util.UriComponentsBuilder;
2142

22-
import java.lang.reflect.Method;
23-
import java.net.URI;
24-
import java.util.Collection;
25-
import java.util.List;
26-
import java.util.stream.Stream;
27-
28-
import static java.util.Collections.singletonList;
29-
import static java.util.stream.Collectors.toList;
30-
import static java.util.stream.Collectors.toMap;
31-
import static org.apache.commons.lang3.ArrayUtils.isNotEmpty;
32-
import static org.apache.commons.lang3.StringUtils.substringAfter;
33-
import static org.apache.commons.lang3.StringUtils.substringBefore;
34-
import static org.springframework.http.MediaType.parseMediaType;
35-
import static org.springframework.util.CollectionUtils.isEmpty;
36-
3743
class RestClientInterceptorHelper {
3844

3945
private static final TypeDescriptor STRING_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(String.class);
@@ -121,14 +127,15 @@ private String convertToString(TypeDescriptor sourceType, Object value) {
121127
return value.toString();
122128
}
123129

124-
private Object[] getPathParameters(List<MethodParameter> parameters, Object[] arguments) {
130+
private Map<String, Object> getPathParameters(List<MethodParameter> parameters, Object[] arguments) {
125131
if (!isEmpty(parameters)) {
126132
return parameters.stream()
127133
.filter(parameter -> parameter.hasParameterAnnotation(PathVariable.class))
128-
.map(parameter -> arguments[parameter.getParameterIndex()])
129-
.toArray(Object[]::new);
134+
.collect(toMap(
135+
parameter -> parameter.getParameterAnnotation(PathVariable.class).value(),
136+
parameter -> arguments[parameter.getParameterIndex()]));
130137
}
131-
return new Object[]{};
138+
return emptyMap();
132139
}
133140

134141
private static HttpMethod toHttpMethod(RequestMethod requestMethod) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package io.github.polysantiago.spring.rest;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
5+
6+
import java.lang.reflect.AccessibleObject;
7+
import java.lang.reflect.Method;
8+
import java.net.URI;
9+
import lombok.Getter;
10+
import lombok.RequiredArgsConstructor;
11+
import org.aopalliance.intercept.MethodInvocation;
12+
import org.junit.Test;
13+
import org.springframework.web.bind.annotation.GetMapping;
14+
import org.springframework.web.bind.annotation.PathVariable;
15+
16+
public class RestClientInterceptorHelperTest {
17+
18+
private static final URI ANY_URL = URI.create("http://example.com");
19+
20+
@Test
21+
public void testCorrectPathVariableName() throws Exception {
22+
Method method = RestClientInterface.class.getMethod("correctPathVariable", String.class);
23+
MethodInvocation invocation = new MockMethodInvocation(method, new Object[]{"id"});
24+
RestClientInterceptorHelper helper = RestClientInterceptorHelper.from(invocation);
25+
assertThat(helper.buildRequest(ANY_URL)).isNotNull();
26+
}
27+
28+
@Test
29+
public void testWrongPathVariableName() throws Exception {
30+
Method method = RestClientInterface.class.getMethod("wrongPathVariable", String.class);
31+
MethodInvocation invocation = new MockMethodInvocation(method, new Object[]{"not-id"});
32+
RestClientInterceptorHelper helper = RestClientInterceptorHelper.from(invocation);
33+
assertThatIllegalArgumentException()
34+
.isThrownBy(() -> helper.buildRequest(ANY_URL))
35+
.withMessage("Map has no value for 'id'");
36+
}
37+
38+
interface RestClientInterface {
39+
40+
@GetMapping("/{id}")
41+
void correctPathVariable(@PathVariable("id") String id);
42+
43+
@GetMapping("/{id}")
44+
void wrongPathVariable(@PathVariable("not-id") String notId);
45+
46+
}
47+
48+
@Getter
49+
@RequiredArgsConstructor
50+
private class MockMethodInvocation implements MethodInvocation {
51+
private final Method method;
52+
private final Object[] arguments;
53+
54+
@Override
55+
public Object proceed() throws Throwable {
56+
return null;
57+
}
58+
59+
@Override
60+
public Object getThis() {
61+
return null;
62+
}
63+
64+
@Override
65+
public AccessibleObject getStaticPart() {
66+
return null;
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)