Skip to content

Parse arrays nested inside arrays of hashes in query params - #1144

Merged
bblimke merged 1 commit into
bblimke:masterfrom
ihamzatarar:fix-nested-array-normalisation
Nov 20, 2026
Merged

bblimke merged 1 commit into
bblimke:masterfrom
ihamzatarar:fix-nested-array-normalisation

Conversation

@ihamzatarar

Copy link
Copy Markdown
Contributor

Fixes #1053.

A query with an array nested inside an array of hashes does not match a stub declared with the equivalent hash:

stub_request(:get, /example\.com/).with(query: {"a" => [{"b" => [{"c" => "1"}]}]})
Net::HTTP.get(URI("http://example.com/?a[][b][][c]=1"))
# => WebMock::NetConnectNotAllowedError

One level works (query: {"a" => [{"b" => "1"}]} against a[][b]=1), two levels do not. The reported request URL is also not the one the client sent:

Unregistered request: GET http://example.com/?a%5B%5D%5Bb%5D%5B0%5D%5Bc%5D=1

The 0 is WebMock's own. WebMock::Util::URI.normalize_uri serialises the inner array with explicit indexes, and query_to_values cannot read them back:

qm = WebMock::Util::QueryMapper
qm.query_to_values("a[][b][][c]=1")
# => {"a" => [{"b" => [{"c" => "1"}]}]}
qm.query_to_values(qm.values_to_query(qm.query_to_values("a[][b][][c]=1")))
# => {"a" => [{"b" => {"0" => {"c" => "1"}}}]}

dehash turns a hash whose keys are all digits into an array, but normalize_query_hash only calls it when the top level value is a Hash, and dehash only recurses through hashes. An array nested inside an array element is never reached, so its numeric keys survive as a hash and the stub hash no longer compares equal.

This changes the parsing side only. The serialised form still uses indexes below the first level, so there is no change to what values_to_query emits and no change to the notation question raised in #764.

The same round trip was already lossy for a top level array of arrays, and that is fixed by the same change:

qm.query_to_values(qm.values_to_query({"a" => [["x", "y"], ["z"]]}))
# before => {"a" => [{"0" => "x", "1" => "y"}, {"0" => "z"}]}
# after  => {"a" => [["x", "y"], ["z"]]}

Three specs added: the parse case and the round trip in query_mapper_spec.rb, and the matching case above in request_pattern_spec.rb. All three fail without the change.

bundle exec rspec spec/unit passes, 749 examples. The full bundle exec rspec spec gives 5688 examples and 144 failures, all in spec/acceptance and all present unchanged on master here, where they need outbound network access.

@ihamzatarar

Copy link
Copy Markdown
Contributor Author

The two failing specs are patron_spec.rb:39 and :45, which are unrelated to this change. Patron released 1.0.0 on 29 October and 1.1.0 on 4 November, both requiring Ruby >= 3.3, which is why only the Ruby 3.3, 3.4 and 4.0 jobs fail: the older Rubies still resolve to patron 0.14.0. The adapter has not caught up, which is what #1142 is for.

This is the first build on the repo since 1.1.0 shipped, so the green ticks on the other open PRs all predate it. Locally the same two specs fail on clean master as well as on this branch, with an otherwise identical failure list.

@bblimke
bblimke merged commit f8e234c into bblimke:master Nov 20, 2026
5 of 8 checks passed
bblimke added a commit that referenced this pull request Nov 20, 2026
Use transform_values! in dehash, add a spec for an indexed array nested
in an array of hashes in a form body, and document #1144 in the
CHANGELOG, including what to do with stubs that use the old shape with
"0", "1" keys, which WebMock's suggested stub printed.
@bblimke

bblimke commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thank you @ihamzatarar! This is released in 3.36.0. While testing it I found a related bug, which I fixed on top: a nested key repeated under [], as in items[][address][city]=X&items[][address][city]=Y, went into the first hash, so the query was read as one item. Each repeat now starts a new hash, as in Rack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue with working with arrays of hashes in query

2 participants