Skip to content

Commit ae7e00e

Browse files
author
Adrian Cole
committed
Provides a nicer error when a user omits the http method
Before this change, if someone accidentally left out the HTTP method in a `@RequestLine` annotation, they'd get an undecipherable error like: "Body parameters cannot be used with form parameters." from Contract.java. This makes the omission easier to find by changing the message to: "RequestLine annotation didn't start with an HTTP verb..." Fixes OpenFeign#310
1 parent 8c1d6e7 commit ae7e00e

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

core/src/main/java/feign/Contract.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodA
189189
checkState(emptyToNull(requestLine) != null,
190190
"RequestLine annotation was empty on method %s.", method.getName());
191191
if (requestLine.indexOf(' ') == -1) {
192+
checkState(requestLine.indexOf('/') == -1,
193+
"RequestLine annotation didn't start with an HTTP verb on method %s.",
194+
method.getName());
192195
data.template().method(requestLine);
193196
return;
194197
}

core/src/main/java/feign/RequestTemplate.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import static feign.Util.CONTENT_LENGTH;
3434
import static feign.Util.UTF_8;
35+
import static feign.Util.checkArgument;
3536
import static feign.Util.checkNotNull;
3637
import static feign.Util.emptyToNull;
3738
import static feign.Util.toArray;
@@ -249,6 +250,7 @@ public Request request() {
249250
/* @see Request#method() */
250251
public RequestTemplate method(String method) {
251252
this.method = checkNotNull(method, "method");
253+
checkArgument(method.matches("^[A-Z]+$"), "Invalid HTTP Method: %s", method);
252254
return this;
253255
}
254256

core/src/test/java/feign/DefaultContractTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,4 +578,18 @@ private MethodMetadata parseAndValidateMetadata(Class<?> targetType, String meth
578578
return contract.parseAndValidateMetadata(targetType,
579579
targetType.getMethod(method, parameterTypes));
580580
}
581+
582+
interface MissingMethod {
583+
@RequestLine("/path?queryParam={queryParam}")
584+
Response updateSharing(@Param("queryParam") long queryParam, String bodyParam);
585+
}
586+
587+
/** Let's help folks not lose time when they mistake request line for a URI! */
588+
@Test
589+
public void missingMethod() throws Exception {
590+
thrown.expect(IllegalStateException.class);
591+
thrown.expectMessage("RequestLine annotation didn't start with an HTTP verb on method updateSharing");
592+
593+
contract.parseAndValidatateMetadata(MissingMethod.class);
594+
}
581595
}

core/src/test/java/feign/RequestTemplateTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
*/
1616
package feign;
1717

18+
import org.junit.Rule;
1819
import org.junit.Test;
1920

2021
import java.util.Arrays;
2122
import java.util.LinkedHashMap;
2223
import java.util.Map;
24+
import org.junit.rules.ExpectedException;
2325

2426
import static feign.RequestTemplate.expand;
2527
import static feign.assertj.FeignAssertions.assertThat;
@@ -28,6 +30,9 @@
2830

2931
public class RequestTemplateTest {
3032

33+
@Rule
34+
public final ExpectedException thrown = ExpectedException.none();
35+
3136
/**
3237
* Avoid depending on guava solely for map literals.
3338
*/
@@ -273,4 +278,13 @@ public void encodeSlashTest() throws Exception {
273278
assertThat(template)
274279
.hasUrl("/api/%2F");
275280
}
281+
282+
/** Implementations have a bug if they pass junk as the http method. */
283+
@Test
284+
public void uriStuffedIntoMethod() throws Exception {
285+
thrown.expect(IllegalArgumentException.class);
286+
thrown.expectMessage("Invalid HTTP Method: /path?queryParam={queryParam}");
287+
288+
new RequestTemplate().method("/path?queryParam={queryParam}");
289+
}
276290
}

0 commit comments

Comments
 (0)