Skip to content

Keep indexed query and form params in numeric order - #1141

Merged
bblimke merged 1 commit into
bblimke:masterfrom
afurm:fix-indexed-params-numeric-order
Nov 18, 2026
Merged

bblimke merged 1 commit into
bblimke:masterfrom
afurm:fix-indexed-params-numeric-order

Conversation

@afurm

@afurm afurm commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

When a form body or query string uses explicit indexes (items[0][name]=...&items[1][name]=...), WebMock turns the numeric keys into an array, but it sorted them as strings. With 11 or more entries items[10] ends up right after items[1], so the parsed array is out of order and a stub declared with the same data doesn't match:

items = (0..10).map { |i| {"name" => "item#{i}"} }
stub_request(:post, "http://example.com/").with(body: {"items" => items})

body = items.each_with_index.map { |h, i| "items[#{i}][name]=#{h["name"]}" }.join("&")
Net::HTTP.post(URI("http://example.com/"), body, "Content-Type" => "application/x-www-form-urlencoded")
# => WebMock::NetConnectNotAllowedError

WebMock::Util::QueryMapper.query_to_values(body)["items"] returns item0, item1, item10, item2, .... Indexed params like this are common with Rails nested attributes and other form encoders, and the same parsing is used for with(query: ...).

The fix sorts the keys by their integer value in dehash. The keys are already known to be all digits at that point.

I added specs to query_mapper_spec.rb for indexed arrays and indexed hashes, and a request pattern spec for the body case. They fail without the change, and bundle exec rspec spec/unit passes (749 examples).

QueryMapper.dehash turned hashes with numeric keys into arrays by sorting
the keys as strings, so a[10] came before a[2]. A stub with an array of
more than ten elements in with(body:) or with(query:) didn't match a
request that sent the same values with explicit indexes.
@bblimke
bblimke merged commit 536c0c4 into bblimke:master Nov 18, 2026
8 checks passed
bblimke added a commit that referenced this pull request Nov 18, 2026
Sort indexed keys by number and then by text, so "01" and "1" always
come out in the same order, as they did with the old text sort.
sort_by is not stable, so the order of such keys depended on the
order of the query. Check for digit keys with \A and \z, so a key with
digits on just one of its lines stays a hash key instead of becoming
index 0.

Drop the redundant defined?(HTTP) from the http_rb adapter guard, use
map in dehash, and add specs for indexed hashes in a query, for keys
with leading zeros and for a key spanning lines.

Document both PRs in the CHANGELOG, including what to do with stubs
that list indexed values in the old order.
@bblimke

bblimke commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thank you @afurm! This is released in 3.35.0.

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.

2 participants