Skip to content

Decode indexed nested arrays in numeric order - #1723

Merged
iMacTia merged 1 commit into
lostisland:mainfrom
afurm:fix-nested-decode-array-index-order
Oct 18, 2026
Merged

iMacTia merged 1 commit into
lostisland:mainfrom
afurm:fix-nested-decode-array-index-order

Conversation

@afurm

@afurm afurm commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Description

NestedParamsEncoder.decode turns a hash with only numeric keys into an array, but it sorted the keys as strings. Once an array has 10 or more indexed entries, "10" lands before "2", so the elements come back out of order:

Faraday::NestedParamsEncoder.decode((0..11).map { |i| "a[#{i}]=v#{i}" }.join('&'))
# => {"a"=>["v0", "v1", "v10", "v11", "v2", "v3", ...]}

You hit this in normal requests too, because a query string in the request path goes through decode and then encode again:

conn = Faraday.new('http://example.com')
conn.build_url("/items?#{(0..10).map { |i| "ids[#{i}]=#{i}" }.join('&')}")
# => .../items?ids%5B%5D=0&ids%5B%5D=1&ids%5B%5D=10&ids%5B%5D=2&...

so the server gets ids[] in a different order than the caller sent. Encoding with array_indices = true and decoding the result doesn't round-trip either.

I changed dehash to sort the indices numerically.

Todos

  • Tests: decoding 12 indexed entries, and an array_indices encode/decode round trip. Both fail without the change.
  • Documentation (not needed)

Additional Notes

bundle exec rake (659 examples, 0 failures), rubocop and yard-junk pass locally.

@iMacTia
iMacTia self-requested a review October 18, 2026 09:28

@iMacTia iMacTia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for spotting and fixing this πŸ™‡

@iMacTia
iMacTia merged commit 7ee0e0f into lostisland:main Oct 18, 2026
9 checks passed
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