Opened 3 years ago

Last modified 23 months ago

#12747 new defect

Nested object serialization with objectToQuery

Reported by: lyle Owned by: BryanForbes
Priority: high Milestone: tbd
Component: IO Version: 1.6.0
Keywords: dojo xhr Cc:
Blocked by: Blocking:

Description

Apparently dojo has no ability by default to serialize regular nested objects via dojo.objectToQuery.

Right now doing this:

dojo.objectToQuery({user: {username: 'lyle', password: 'testing'}});

will generate the query:

user=[Object object]

This is useless to everybody. The generally accepted solution is the following:

user[username]=lyle&user[password]=testing

This is how PHP and Rails expect nested objects, and it's how Zend and Rails build their forms by default. This is also how jQuery serializes nested objects.

This may not be the perfect solution, but you shouldn't get garbage when you put nested objects into objectToQuery.

Also, I noticed that in an older version of node.js this syntax was expected for nested objects, but at some point it has changed to no longer accept it in the way I've outlined. I don't know why that is but I'm plenty curious.

Change History (4)

comment:1 Changed 3 years ago by neonstalwart

the problem is that there is no standard, or arguably even a general way to represent deep objects in a querystring. if you were using struts for example then the query for your object should be:

user.username=lyle&user.password=testing

which is different to the example you give but maybe just as intuitive and widespread.

dojo is used to make requests from many different backends and without a standard to follow, its difficult to justify implementing one particular option over another. the best that can be done is to give you tools to build what you need and make it trivial for you to implement your specific option. a couple of years ago i wrote a custom objectToQuery for struts in ~20 lines of code - follow the current implementation of objectToQuery and for each property in the object, check if it's an array or object and then recurse if needed.

comment:2 Changed 3 years ago by lyle

I definitely don't disagree that it should be easy to override this behavior. In fact, I feel it would be easier to override nested objectToQuery serialization behavior if there were a sensible hook implemented and maybe even a default implementation of nested object serialization. I really don't care which one. This just feels broken by default to me.

comment:3 Changed 2 years ago by thomasbachem

+1.

jQuery's $.param() method does support nested parameters with this syntax, see http://benalman.com/news/2009/12/jquery-14-param-demystified/.

The current behaviour (putting stuff like "[Object object]" into the query string) makes no sense at all, so every other solution - if there's a standard or not - would probably be better :).

comment:4 Changed 23 months ago by bill

  • Component changed from Core to IO
  • Owner set to BryanForbes

You can close as wontfix if this is addressed with the new dojo/request code. Or is this not in the IO category?

Note: See TracTickets for help on using tickets.