Skip to content

Commit

Permalink
Solidify iterable iteration.
Browse files Browse the repository at this point in the history
Casting to Object[] will throw when a primitive array is used. Use the utilities provided by Array to iterate in a type-safe way.
  • Loading branch information
JakeWharton committed Feb 1, 2014
1 parent 2a5da1f commit 9c056e0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
11 changes: 10 additions & 1 deletion retrofit/src/main/java/retrofit/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package retrofit;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -207,7 +208,8 @@ void setArguments(Object[] args) {
}
}
} else if (value.getClass().isArray()) {
for (Object arrayValue : (Object[]) value) {
for (int x = 0, arrayLength = Array.getLength(value); x < arrayLength; x++) {
Object arrayValue = Array.get(value, x);
if (arrayValue != null) { // Skip null values
addQueryParam(name, arrayValue.toString(), urlEncodeValue);
}
Expand Down Expand Up @@ -242,6 +244,13 @@ void setArguments(Object[] args) {
formBody.addField(name, iterableValue.toString());
}
}
} else if (value.getClass().isArray()) {
for (int x = 0, arrayLength = Array.getLength(value); x < arrayLength; x++) {
Object arrayValue = Array.get(value, x);
if (arrayValue != null) { // Skip null values.
formBody.addField(name, arrayValue.toString());
}
}
} else {
formBody.addField(name, value.toString());
}
Expand Down
62 changes: 50 additions & 12 deletions retrofit/src/test/java/retrofit/RequestBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public class RequestBuilderTest {
}

@Test public void getWithQueryParamList() throws Exception {
List<Object> values = Arrays.<Object>asList(1, 2, "three");
List<Object> values = Arrays.<Object>asList(1, 2, null, "three");

Request request = new Helper() //
.setMethod("GET") //
Expand All @@ -319,7 +319,7 @@ public class RequestBuilderTest {
}

@Test public void getWithQueryParamArray() throws Exception {
Object[] values = { 1, 2, "three" };
Object[] values = { 1, 2, null, "three" };

Request request = new Helper() //
.setMethod("GET") //
Expand All @@ -333,6 +333,21 @@ public class RequestBuilderTest {
assertThat(request.getBody()).isNull();
}

@Test public void getWithQueryParamPrimitiveArray() throws Exception {
int[] values = { 1, 2, 3 };

Request request = new Helper() //
.setMethod("GET") //
.setUrl("http://example.com") //
.setPath("/foo/bar/") //
.addQueryParam("key", values) //
.build();
assertThat(request.getMethod()).isEqualTo("GET");
assertThat(request.getHeaders()).isEmpty();
assertThat(request.getUrl()).isEqualTo("http://example.com/foo/bar/?key=1&key=2&key=3");
assertThat(request.getBody()).isNull();
}

@Test public void getWithQueryParamMap() throws Exception {
Map<String, Object> params = new LinkedHashMap<String, Object>();
params.put("kit", "kat");
Expand Down Expand Up @@ -536,20 +551,50 @@ public class RequestBuilderTest {
}

@Test public void formEncodedFieldList() throws Exception {
List<Object> values = new ArrayList<Object>(Arrays.asList("foo", "bar", null, 3));
List<Object> values = Arrays.<Object>asList("foo", "bar", null, 3);

Request request = new Helper() //
.setMethod("POST") //
.setHasBody() //
.setUrl("http://example.com") //
.setPath("/foo") //
.setFormEncoded() //
.addFieldList("foo", values) //
.addField("foo", values) //
.addField("kit", "kat") //
.build();
assertTypedBytes(request.getBody(), "foo=foo&foo=bar&foo=3&kit=kat");
}

@Test public void formEncodedFieldArray() throws Exception {
Object[] values = { 1, 2, null, "three" };

Request request = new Helper() //
.setMethod("POST") //
.setHasBody() //
.setUrl("http://example.com") //
.setPath("/foo") //
.setFormEncoded() //
.addField("foo", values) //
.addField("kit", "kat") //
.build();
assertTypedBytes(request.getBody(), "foo=1&foo=2&foo=three&kit=kat");
}

@Test public void formEncodedFieldPrimitiveArray() throws Exception {
int[] values = { 1, 2, 3 };

Request request = new Helper() //
.setMethod("POST") //
.setHasBody() //
.setUrl("http://example.com") //
.setPath("/foo") //
.setFormEncoded() //
.addField("foo", values) //
.addField("kit", "kat") //
.build();
assertTypedBytes(request.getBody(), "foo=1&foo=2&foo=3&kit=kat");
}

@Test public void formEncodedFieldMap() throws Exception {
Map<String, Object> params = new LinkedHashMap<String, Object>();
params.put("kit", "kat");
Expand Down Expand Up @@ -758,20 +803,13 @@ Helper addEncodedQueryMapParams(String name, Map<String, Object> values) {
return this;
}

Helper addField(String name, String value) {
Helper addField(String name, Object value) {
paramNames.add(name);
paramUsages.add(FIELD);
args.add(value);
return this;
}

Helper addFieldList(String name, List<Object> values) {
paramNames.add(name);
paramUsages.add(FIELD);
args.add(values);
return this;
}

Helper addFieldMap(String name, Map<String, Object> values) {
paramNames.add(name);
paramUsages.add(FIELD_MAP);
Expand Down

0 comments on commit 9c056e0

Please sign in to comment.