-
Notifications
You must be signed in to change notification settings - Fork 368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix flattening of array params #572
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Exciting seeing another client library getting this resolved.
Nice! The one problem may be that traditionally, we've actually required both the integer-indexed and non-indexed versions of arrays, depending on the endpoint. Certain special-cased ones can support the integer-indexed style (e.g., update account), but others couldn't. We're now fully converted over to the new AbstractAPIMethod, which in theory should give any endpoint the ability to tolerate integer indexes, but we should probably do a little more in the way of testing before fully assuming that. If you look at how this is implemented in other SDKs, we usually have special encoding calls in places we know we'll need them. e.g.: module Stripe
class Subscription < APIResource
def self.update(id, params = {}, opts = {})
params[:items] = Util.array_to_hash(params[:items]) if params[:items]
super(id, params, opts)
end
...
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a minor comment, but otherwise LGTM. Thanks again @stevene-stripe!
ptal @stevene-stripe
Iterator<?> it = ((List<?>) params).iterator(); | ||
String newPrefix = String.format("%s[]", keyPrefix); | ||
|
||
ListIterator<?> it = ((List<?>) params).listIterator(); | ||
// Because application/x-www-form-urlencoded cannot represent an empty | ||
// list, convention is to take the list parameter and just set it to an | ||
// empty string. (e.g. A regular list might look like `a[]=1&b[]=2`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind updating this comment to something like this (now that we expect to have integer indexes everywhere):
// Because application/x-www-form-urlencoded cannot represent an empty
// list, convention is to take the list parameter and just set it to an
// empty string. (e.g. A regular list might look like `a[]=1&b[]=2` or
// `a[0]=1&b[1]=2` when encoded with Stripe's style. Emptying it would look
// like `a=`.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's also a duplicate comment in flattenParamsArray
that should be updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed!
@@ -297,7 +297,6 @@ static String createQuery(Map<String, Object> params) | |||
private static List<Parameter> flattenParamsArray(Object[] params, String keyPrefix) | |||
throws InvalidRequestException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can fix this in a different PR, but just wanted to flag that having both flattenParamsArray
and flattenParamsList
is pretty silly — these should be merged into one method that just takes an Iterator
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sigh agreed.
And just to address my comment above: we discussed and tested this offline, and it seems that we're now safe to use integer-indexed arrays everywhere, so ignore my previous objection. |
Thanks! LGTM. |
Released as 6.4.0. |
@brandur-stripe can we get this version pushed to maven central? |
@sedouard Sorry, something must have gone wrong with the release process. I've manually pruned Sontatype and confirmed a successful release. It should bubble out to Maven Central within the next few hours. |
Consider the following Java snippet using the Stripe java SDK:
This code outputs the url query parameters:
When in actuality Stripe's API cannot interpret the hashes within the array elements correctly without index numbers assigned to each empty
[]
. Instead with this PR the output looks like:I've modified the existing tests to accommodate for this change however new tests do not seem to be required.