Skip to content

Commit

Permalink
Monkeypatch to pass array params without an index
Browse files Browse the repository at this point in the history
typhoeus/typhoeus#320

We were seeing array params tranformed into the following:
/event?groups[0]=open-forum&groups[1]=armory-films

but we need the client to send array params to various API
services in the following (valid) format:
/event?groups[]=open-forum&groups[]=armory-films
  • Loading branch information
dylanfareed committed Mar 4, 2014
1 parent 22f6116 commit 548033a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/ethon/easy/queryable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def recursively_generate_pairs(h, prefix, pairs)
end
when Array
h.each_with_index do |v, i|
key = "#{prefix}[#{i}]"
key = "#{prefix}[]"
pairs_for(v, key, pairs)
end
end
Expand Down
10 changes: 5 additions & 5 deletions spec/ethon/easy/queryable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@

it "transforms" do
expect(pairs).to include([:a, 1])
expect(pairs).to include(["b[0]", 2])
expect(pairs).to include(["b[1]", 3])
expect(pairs).to include(["b[]", 2])
expect(pairs).to include(["b[]", 3])
end
end

Expand All @@ -118,15 +118,15 @@
let(:hash) { {:a => {:b => ["hello", "world"]}} }

it "transforms" do
expect(pairs).to eq([["a[b][0]", "hello"], ["a[b][1]", "world"]])
expect(pairs).to eq([["a[b][]", "hello"], ["a[b][]", "world"]])
end
end

context "when hash" do
let(:hash) { {:a => {:b => [{:c =>1}, {:d => 2}]}} }

it "transforms" do
expect(pairs).to eq([["a[b][0][c]", 1], ["a[b][1][d]", 2]])
expect(pairs).to eq([["a[b][][c]", 1], ["a[b][][d]", 2]])
end
end

Expand All @@ -136,7 +136,7 @@
let(:hash) { {:a => {:b => [file]}} }

it "transforms" do
expect(pairs).to eq([["a[b][0]", file_info]])
expect(pairs).to eq([["a[b][]", file_info]])
end
end
end
Expand Down

5 comments on commit 548033a

@dblock
Copy link

@dblock dblock commented on 548033a Feb 6, 2018

Choose a reason for hiding this comment

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

@dylanfareed Do you remember this? What was the reason not to PR this upstream to typhoeus?

@dylanfareed
Copy link
Author

Choose a reason for hiding this comment

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

I don't remember fully (4 years!), but I agree that, if they'd accept the change upstream in Ethon (maybe using a config/opt to define the array encoding strategy), that's a better path forward.

The source commit here references the issue on their end(s):
#52 (comment)
typhoeus/typhoeus#320

@dblock
Copy link

@dblock dblock commented on 548033a Feb 8, 2018

Choose a reason for hiding this comment

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

So ethon now has params_encoding and takes :rack to make it this way, see https://github.com/typhoeus/ethon/blob/master/lib/ethon/easy/queryable.rb#L101. This was added in #104.

Still trying to track down where we actually need this change, what API and from where is being used that causes a failure.

@dylanfareed
Copy link
Author

Choose a reason for hiding this comment

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

Awesome. That's great that that is now available in ethon. If I recall correctly, the monkeypatch was introduced so that the rails adapter library we were writing (whose name escapes me now) could encode array params for sub resources like "events" associated with a "show" in a manner that Gravity's API could parse natively. I'm not sure if that's what you are asking...

@dylanfareed
Copy link
Author

Choose a reason for hiding this comment

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

The commit comment here provides some clues as to which Gravity endpoints we were concerned about

image

Please sign in to comment.