Skip to content

Commit

Permalink
Merge pull request square#1125 from square/jw/head-must-be-void
Browse files Browse the repository at this point in the history
HEAD requests must use Void response type.
  • Loading branch information
JakeWharton committed Sep 25, 2015
2 parents 650126c + afef3c5 commit 4b969e9
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 108 deletions.
2 changes: 1 addition & 1 deletion retrofit/src/main/java/retrofit/MethodHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static MethodHandler<?> create(Retrofit retrofit, Method method) {
Type responseType = callAdapter.responseType();
Converter<ResponseBody, Object> responseConverter =
(Converter<ResponseBody, Object>) createResponseConverter(method, retrofit, responseType);
RequestFactory requestFactory = RequestFactoryParser.parse(method, retrofit);
RequestFactory requestFactory = RequestFactoryParser.parse(method, responseType, retrofit);
return new MethodHandler<>(retrofit, requestFactory, callAdapter, responseConverter);
}

Expand Down
9 changes: 6 additions & 3 deletions retrofit/src/main/java/retrofit/RequestFactoryParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ final class RequestFactoryParser {
private static final Pattern PARAM_NAME_REGEX = Pattern.compile(PARAM);
private static final Pattern PARAM_URL_REGEX = Pattern.compile("\\{(" + PARAM + ")\\}");

static RequestFactory parse(Method method, Retrofit retrofit) {
static RequestFactory parse(Method method, Type responseType, Retrofit retrofit) {
RequestFactoryParser parser = new RequestFactoryParser(method);
parser.parseMethodAnnotations();
parser.parseMethodAnnotations(responseType);
parser.parseParameters(retrofit);
return parser.toRequestFactory(retrofit.baseUrl());
}
Expand Down Expand Up @@ -92,14 +92,17 @@ private RuntimeException parameterError(int index, String message, Object... arg
return methodError(method, message + " (parameter #" + (index + 1) + ")", args);
}

private void parseMethodAnnotations() {
private void parseMethodAnnotations(Type responseType) {
for (Annotation annotation : method.getAnnotations()) {
if (annotation instanceof DELETE) {
parseHttpMethodAndPath("DELETE", ((DELETE) annotation).value(), false);
} else if (annotation instanceof GET) {
parseHttpMethodAndPath("GET", ((GET) annotation).value(), false);
} else if (annotation instanceof HEAD) {
parseHttpMethodAndPath("HEAD", ((HEAD) annotation).value(), false);
if (!Void.class.equals(responseType)) {
throw methodError(method, "HEAD method must use Void as response type.");
}
} else if (annotation instanceof PATCH) {
parseHttpMethodAndPath("PATCH", ((PATCH) annotation).value(), true);
} else if (annotation instanceof POST) {
Expand Down
6 changes: 5 additions & 1 deletion retrofit/src/main/java/retrofit/http/HEAD.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/** Make a HEAD request to a REST path relative to base URL. */
/**
* Make a HEAD request to a REST path relative to base URL.
* <p>
* Note: HEAD requests must use {@link Void} as the response type since there is no body content.
*/
@Documented
@Target(METHOD)
@Retention(RUNTIME)
Expand Down
Loading

0 comments on commit 4b969e9

Please sign in to comment.