Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Correct map parameter type detection for query and field.

  • Loading branch information...
commit dfe5d9eb51352d6cad8ac618e19577525adcab87 1 parent 6833233
Jake Wharton JakeWharton authored
7 CHANGELOG.md
View
@@ -1,6 +1,13 @@
Change Log
==========
+Version 1.4.1 *(2014-02-01)*
+----------------------------
+
+ * Fix: `@QueryMap`, `@EncodedFieldMap`, and `@FieldMap` now correctly detect `Map`-based parameter
+ types.
+
+
Version 1.4.0 *(2014-01-31)*
----------------------------
8 retrofit/src/main/java/retrofit/RestMethodInfo.java
View
@@ -372,17 +372,17 @@ private void parseParameters() {
paramNames[i] = name;
paramUsage[i] = ParamUsage.ENCODED_QUERY;
} else if (annotationType == QueryMap.class) {
- if (!parameterType.isInstance(Map.class)) {
+ if (!Map.class.isAssignableFrom(parameterType)) {
throw new IllegalStateException("@QueryMap parameter type must be Map.");
}
paramUsage[i] = ParamUsage.QUERY_MAP;
} else if (annotationType == EncodedQueryMap.class) {
- if (!parameterType.isInstance(Map.class)) {
+ if (!Map.class.isAssignableFrom(parameterType)) {
throw new IllegalStateException("@EncodedQueryMap parameter type must be Map.");
}
- paramUsage[i] = ParamUsage.ENCODED_QUERY;
+ paramUsage[i] = ParamUsage.ENCODED_QUERY_MAP;
} else if (annotationType == Header.class) {
String name = ((Header) parameterAnnotation).value();
if (parameterType != String.class) {
@@ -407,7 +407,7 @@ private void parseParameters() {
throw new IllegalStateException(
"@Field parameters can only be used with form encoding.");
}
- if (!parameterType.isInstance(Map.class)) {
+ if (!Map.class.isAssignableFrom(parameterType)) {
throw new IllegalStateException("@FieldMap parameter type must be Map.");
}
52 retrofit/src/test/java/retrofit/RestMethodInfoTest.java
View
@@ -42,9 +42,13 @@
import static retrofit.RestMethodInfo.ParamUsage.BODY;
import static retrofit.RestMethodInfo.ParamUsage.ENCODED_PATH;
import static retrofit.RestMethodInfo.ParamUsage.ENCODED_QUERY;
+import static retrofit.RestMethodInfo.ParamUsage.ENCODED_QUERY_MAP;
+import static retrofit.RestMethodInfo.ParamUsage.FIELD_MAP;
import static retrofit.RestMethodInfo.ParamUsage.HEADER;
import static retrofit.RestMethodInfo.ParamUsage.PATH;
import static retrofit.RestMethodInfo.ParamUsage.QUERY;
+import static retrofit.RestMethodInfo.ParamUsage.QUERY_MAP;
+import static retrofit.RestMethodInfo.RequestType.FORM_URL_ENCODED;
import static retrofit.RestMethodInfo.RequestType.MULTIPART;
import static retrofit.RestMethodInfo.RequestType.SIMPLE;
@@ -537,6 +541,22 @@ Response a() {
assertThat(methodInfo.requestType).isEqualTo(SIMPLE);
}
+ @Test public void queryMap() {
+ class Example {
+ @GET("/") Response a(@QueryMap Map<String, String> a) {
+ return null;
+ }
+ }
+
+ Method method = TestingUtils.getMethod(Example.class, "a");
+ RestMethodInfo methodInfo = new RestMethodInfo(method);
+ methodInfo.init();
+
+ assertThat(methodInfo.requestParamNames).hasSize(1).containsNull();
+ assertThat(methodInfo.requestParamUsage).hasSize(1).containsExactly(QUERY_MAP);
+ assertThat(methodInfo.requestType).isEqualTo(SIMPLE);
+ }
+
@Test public void queryMapMustBeAMap() {
class Example {
@GET("/") Response a(@QueryMap List<String> a) {
@@ -554,6 +574,22 @@ Response a() {
}
}
+ @Test public void encodedQueryMap() {
+ class Example {
+ @GET("/") Response a(@EncodedQueryMap Map<String, String> a) {
+ return null;
+ }
+ }
+
+ Method method = TestingUtils.getMethod(Example.class, "a");
+ RestMethodInfo methodInfo = new RestMethodInfo(method);
+ methodInfo.init();
+
+ assertThat(methodInfo.requestParamNames).hasSize(1).containsNull();
+ assertThat(methodInfo.requestParamUsage).hasSize(1).containsExactly(ENCODED_QUERY_MAP);
+ assertThat(methodInfo.requestType).isEqualTo(SIMPLE);
+ }
+
@Test public void encodedQueryMapMustBeAMap() {
class Example {
@GET("/") Response a(@EncodedQueryMap List<String> a) {
@@ -571,6 +607,22 @@ Response a() {
}
}
+ @Test public void fieldMap() {
+ class Example {
+ @FormUrlEncoded @POST("/") Response a(@FieldMap Map<String, String> a) {
+ return null;
+ }
+ }
+
+ Method method = TestingUtils.getMethod(Example.class, "a");
+ RestMethodInfo methodInfo = new RestMethodInfo(method);
+ methodInfo.init();
+
+ assertThat(methodInfo.requestParamNames).hasSize(1).containsNull();
+ assertThat(methodInfo.requestParamUsage).hasSize(1).containsExactly(FIELD_MAP);
+ assertThat(methodInfo.requestType).isEqualTo(FORM_URL_ENCODED);
+ }
+
@Test public void fieldMapMustBeAMap() {
class Example {
@FormUrlEncoded @POST("/") Response a(@FieldMap List<String> a) {
Please sign in to comment.
Something went wrong with that request. Please try again.