RestKit/RestKit — pull request #697 by humblehacker (2012-04-23 23:25:23+00:00)
Title: Add indexes to URL encoded arrays

Currently, when form encoding dictionaries, RestKit encodes array elements with empty brackets. For example, given an array of the following object:

@interface Blob
@property NSString *foo;
@property NSString *bar;
@end

might be encoded as:

blobs[][foo]=alpha&blobs[][bar]=beta&blobs[][bar]=gamma&blobs[][foo]=delta

which in Rails evaluates to:

{"blobs"=>[{"foo"=>"alpha", "bar"=>"beta"}, {"bar"=>"gamma", "foo"=>"delta"}]}

---

Here is some [prior discussion][prior]. The judgement there seemed to be that Rails does it this way, so RestKit should follow. But sometimes this breaks down. Consider the case where some fields are optional:

blobs[][foo]=alpha&blobs[][bar]=gamma&blobs[][foo]=delta

which in Rails evaluates to:

{"blobs"=>[{"foo"=>"alpha", "bar"=>"gamma"}, {"foo"=>"delta"}]}

when I was expecting:

{"blobs"=>[{"foo"=>"alpha"}, {"bar"=>"gamma", "foo"=>"delta"}]}

[prior]: https://github.com/RestKit/RestKit/commit/d6e26a6f9dc0242e670833ee27b010f8bd554f9f

---

If we add indexes to the parameters, the proper grouping can be maintained:

blobs[0][foo]=alpha&blobs[1][bar]=gamma&blobs[1][foo]=delta

evaluates to:

{"blobs"=>{"0"=>{"foo"=>"alpha"}, "1"=>{"bar"=>"gamma", "foo"=>"delta"}}}

---

I realize this change may not be appealing to everyone. I'm open to suggestions on alternative solutions. Given that this feature is implemented in a category on NSDictionary, there's no easy way to override it without changing RestKit.