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

Added functionality (+spec) to convert empty arrays. #125

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions lib/rack/test/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ module Utils # :nodoc:
def build_nested_query(value, prefix = nil)
case value
when Array
value.map do |v|
unless unescape(prefix) =~ /\[\]$/
prefix = "#{prefix}[]"
end
build_nested_query(v, "#{prefix}")
end.join("&")
if value.empty?
"#{prefix}=#{escape(value)}"
Copy link

@timfjord timfjord Jan 28, 2016

Choose a reason for hiding this comment

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

This didn't work in my case(rack will parse it to '[]')
Here is my working solution
"#{prefix}[]="

Copy link

Choose a reason for hiding this comment

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

👍

else
value.map do |v|
unless unescape(prefix) =~ /\[\]$/
prefix = "#{prefix}[]"
end
build_nested_query(v, "#{prefix}")
end.join("&")
end
when Hash
value.map do |k, v|
build_nested_query(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k))
Expand Down
4 changes: 4 additions & 0 deletions spec/rack/test/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
it "converts arrays of hashes" do
build_nested_query(:a => [{ :b => 2}, { :c => 3}]).should == "a[][b]=2&a[][c]=3"
end

it "converts empty arrays" do
build_nested_query(:a => []).should == "a=%5B%5D"
end
end

describe "build_multipart" do
Expand Down