Skip to content
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 encoding of arrays that are sent in query strings #612

Merged
merged 1 commit into from
Dec 7, 2017

Conversation

brandur
Copy link
Contributor

@brandur brandur commented Dec 7, 2017

As discussed in #608, we currently have a problem where Faraday
deconstructs a query string that we encode and strips out any of the
array index numbers that we added. It's not too clear on why it does
this, but it appears to be built in at a pretty low level and hard to
change.

I spent a little time on this and it turns out that we can avoid the bad
code by depending on Faraday's params accessor on a request instead of
trying to do the encoding ourselves. Webmock has a pretty hard time
detecting the difference, but you can see some before and after encoding
here.

Before:

I, [2017-12-06T17:41:23.083942 #36737]  INFO -- : get
http://localhost:12111/v1/invoices/upcoming?customer=cus_123&subscription_items%5B%5D%5Bplan%5D=gold&subscription_items%5B%5D%5Bquantity%5D=2

After:

I, [2017-12-06T17:42:12.727752 #37158]  INFO -- : get
http://localhost:12111/v1/invoices/upcoming?customer=cus_123&subscription_items%5B0%5D%5Bplan%5D=gold&subscription_items%5B0%5D%5Bquantity%5D=2

Honestly, some of this can still use a lot of cleanup: it's weird that
we manually encode parameters ourselves for bodies, but not for queries;
but I think this'll fix the problem for now.

Fixes #608.

Copy link
Contributor

@ob-stripe ob-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find! I did look a little at Faraday's internals but missed this.

I agree that it's definitely weird that we'd encode the parameters ourselves or not depending on the type of the request, but this looks like a decent short-term fix.

test/stripe/invoice_test.rb Outdated Show resolved Hide resolved
As discussed in #608, we currently have a problem where Faraday
deconstructs a query string that we encode and strips out any of the
array index numbers that we added. It's not too clear on why it does
this, but it appears to be built in at a pretty low level and hard to
change.

I spent a little time on this and it turns out that we can avoid the bad
code by depending on Faraday's `params` accessor on a request instead of
trying to do the encoding ourselves. Webmock has a pretty hard time
detecting the difference, but you can see some before and after encoding
here.

Before:

```
I, [2017-12-06T17:41:23.083942 #36737]  INFO -- : get
http://localhost:12111/v1/invoices/upcoming?customer=cus_123&subscription_items%5B%5D%5Bplan%5D=gold&subscription_items%5B%5D%5Bquantity%5D=2
```

After:

```
I, [2017-12-06T17:42:12.727752 #37158]  INFO -- : get
http://localhost:12111/v1/invoices/upcoming?customer=cus_123&subscription_items%5B0%5D%5Bplan%5D=gold&subscription_items%5B0%5D%5Bquantity%5D=2
```

Honestly, some of this can still use a lot of cleanup: it's weird that
we manually encode parameters ourselves for bodies, but not for queries;
but I think this'll fix the problem for now.

Fixes #608.
@brandur-stripe brandur-stripe force-pushed the brandur-fix-array-encoding-in-non-post branch from c06b258 to 2bc471d Compare December 7, 2017 18:51
@brandur-stripe
Copy link
Contributor

I agree that it's definitely weird that we'd encode the parameters ourselves or not depending on the type of the request, but this looks like a decent short-term fix.

Yeah totally. I think it should be relatively easy to clean this up too, and I could potentially take a stab on a follow up pull request.

@brandur-stripe
Copy link
Contributor

r? @ob-stripe Thanks for taking a look OB! Mind doing one more review pass? Thanks!

@ob-stripe
Copy link
Contributor

lgtm 👍

@brandur-stripe brandur-stripe merged commit e149379 into master Dec 7, 2017
@brandur-stripe brandur-stripe deleted the brandur-fix-array-encoding-in-non-post branch December 7, 2017 20:54
@joebartels
Copy link

joebartels commented Mar 10, 2018

FYI. this saved my life! It was really frustrated seeing that the same exact request params coming from the Stripe dashboard (while editing a subscription's items) wouldn't work went sent from my code ( using v. 3.8.0).

Upgrading fixed it. 💥 Thanks guys

@brandur-stripe
Copy link
Contributor

@joebartels No worries! Glad it helped :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Faraday strips integer-indexed maps for query strings (any GET requests like upcoming invoices)
5 participants