Skip to content

Commit 7085968

Browse files
Merge pull request #572 from sedouard/fix-array-parameters
Fix flattening of array params
2 parents 32c2789 + 29e1331 commit 7085968

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

Diff for: src/main/java/com/stripe/net/LiveStripeResponseGetter.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.HashMap;
3939
import java.util.Iterator;
4040
import java.util.List;
41+
import java.util.ListIterator;
4142
import java.util.Map;
4243
import java.util.Scanner;
4344
import javax.net.ssl.HttpsURLConnection;
@@ -276,17 +277,16 @@ private static List<Parameter> flattenParams(Map<String, Object> params)
276277
private static List<Parameter> flattenParamsList(List<Object> params, String keyPrefix)
277278
throws InvalidRequestException {
278279
List<Parameter> flatParams = new ArrayList<Parameter>();
279-
Iterator<?> it = ((List<?>) params).iterator();
280-
String newPrefix = String.format("%s[]", keyPrefix);
281-
280+
ListIterator<?> it = ((List<?>) params).listIterator();
282281
// Because application/x-www-form-urlencoded cannot represent an empty
283282
// list, convention is to take the list parameter and just set it to an
284-
// empty string. (e.g. A regular list might look like `a[]=1&b[]=2`.
283+
// empty string. (e.g. A regular list might look like `a[0]=1&b[1]=2`.
285284
// Emptying it would look like `a=`.)
286285
if (params.isEmpty()) {
287286
flatParams.add(new Parameter(keyPrefix, ""));
288287
} else {
289288
while (it.hasNext()) {
289+
String newPrefix = String.format("%s[%d]", keyPrefix, it.nextIndex());
290290
flatParams.addAll(flattenParamsValue(it.next(), newPrefix));
291291
}
292292
}
@@ -297,17 +297,17 @@ private static List<Parameter> flattenParamsList(List<Object> params, String key
297297
private static List<Parameter> flattenParamsArray(Object[] params, String keyPrefix)
298298
throws InvalidRequestException {
299299
List<Parameter> flatParams = new ArrayList<Parameter>();
300-
String newPrefix = String.format("%s[]", keyPrefix);
301300

302301
// Because application/x-www-form-urlencoded cannot represent an empty
303302
// list, convention is to take the list parameter and just set it to an
304-
// empty string. (e.g. A regular list might look like `a[]=1&b[]=2`.
303+
// empty string. (e.g. A regular list might look like `a[0]=1&b[1]=2`.
305304
// Emptying it would look like `a=`.)
306305
if (params.length == 0) {
307306
flatParams.add(new Parameter(keyPrefix, ""));
308307
} else {
309-
for (Object item : params) {
310-
flatParams.addAll(flattenParamsValue(item, newPrefix));
308+
for (int i = 0; i < params.length; i++) {
309+
String newPrefix = String.format("%s[%d]", keyPrefix, i);
310+
flatParams.addAll(flattenParamsValue(params[i], newPrefix));
311311
}
312312
}
313313

@@ -637,7 +637,7 @@ private static StripeResponse getMultipartStripeResponse(
637637
"Must have read permissions on file for key "
638638
+ key + ".", null, null, null, 0, null);
639639
}
640-
multipartProcessor.addFileField(key, currentFile.getName(),
640+
multipartProcessor.addFileField(key, currentFile.getName(),
641641
new FileInputStream(currentFile));
642642
} else if (value instanceof InputStream) {
643643
InputStream inputStream = (InputStream) value;

Diff for: src/test/java/com/stripe/net/LiveStripeResponseGetterTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void testCreateQueryWithListParams() throws StripeException, UnsupportedE
6464
params.put("a", "b");
6565
params.put("c", "d");
6666

67-
assertEquals("nested[]=A&nested[]=B&nested[]=C&a=b&c=d",
67+
assertEquals("nested[0]=A&nested[1]=B&nested[2]=C&a=b&c=d",
6868
LiveStripeResponseGetter.createQuery(params));
6969
}
7070

@@ -80,7 +80,7 @@ public void testCreateQueryWithArrayParams() throws StripeException,
8080
params.put("a", "b");
8181
params.put("c", "d");
8282

83-
assertEquals("nested[]=A&nested[]=B&nested[]=C&a=b&c=d",
83+
assertEquals("nested[0]=A&nested[1]=B&nested[2]=C&a=b&c=d",
8484
LiveStripeResponseGetter.createQuery(params));
8585
}
8686

@@ -103,7 +103,7 @@ public void testCreateQueryWithListOfHashes() throws StripeException,
103103
final Map<String, Object> params = new LinkedHashMap<String, Object>();
104104
params.put("nested", nested);
105105

106-
assertEquals("nested[][A]=A-1&nested[][B]=B-1&nested[][A]=A-2&nested[][B]=B-2",
106+
assertEquals("nested[0][A]=A-1&nested[0][B]=B-1&nested[1][A]=A-2&nested[1][B]=B-2",
107107
LiveStripeResponseGetter.createQuery(params));
108108
}
109109

@@ -144,7 +144,7 @@ public void testIncorrectAdditionalOwners() throws StripeException, UnsupportedE
144144
final Map<String, Object> params = new HashMap<String, Object>();
145145
params.put("legal_entity", legalEntityParams);
146146

147-
assertEquals("legal_entity[additional_owners][][first_name]=Stripe",
147+
assertEquals("legal_entity[additional_owners][0][first_name]=Stripe",
148148
LiveStripeResponseGetter.createQuery(params));
149149
}
150150

0 commit comments

Comments
 (0)