Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

fix(ngResource): Add suport for nested params #1640

Closed

Conversation

cakesifu
Copy link
Contributor

@cakesifu cakesifu commented Dec 3, 2012

Allow ngResource to properly construct a query string for params which
have objects or arrays as values.

Allow ngResource to properly construct a query string for params which
have objects or arrays as values.
@marknadig
Copy link
Contributor

Just ran into this today and agree it would be great if it worked like jQuery.param method. I am hitting a Rails service and just trying to pass a simple array as a parameter. Note: This PR currently will encode arrays w/ index; e.g.
foo=['bar', 'baz']
is encoded as
foo[0]=bar&foo[1]=baz

parity with jQuery would be
foo[]=bar&foo[]=baz

(by supplying index - Rails parses the parameter into a hash using the index as a key rather than an array). Parity w/ jQuery would exclude the index for array.

@marknadig
Copy link
Contributor

following up - here's the issue related to $http where added ability to encode arrays that Igor referenced:
#1363

I agree keeping it consistent would be best. Actually, has me wondering - why even have this encoding logic in angular-resource's Route.url() ... perhaps a better approach would be Route.url only deals with urlParams and pass along any remaining params on to $http to keep it DRY.

@marknadig
Copy link
Contributor

@IgorMinar I was working on a PR to have resource delegate encoding of params to $http. The code is pretty straight forward and I think is a good change to DRY things up. However, I have one failing test related to this spec: 'should not encode @ in url params' with this comment:
//encodeURIComponent is too agressive and doesn't follow http://www.ietf.org/rfc/rfc3986.txt
//with regards to the character set (pchar) allowed in path segments
//so we need this test to make sure that we don't over-encode the params and break stuff like
//buzz api which uses @self

With the encoding of the params handled by http, this test fails which has me wondering why the fix for over aggressive encoding isn't in $http to begin with.

@Phantoms
Copy link

It is a much needed (critical, i would say) feature, array serialization to x[]=one&x[]=two.

Also, not sure if related, it is now impossible to set your path to something like "?q=text&x[]=one&x[]=two" at all. It gets changed to "?q=text&x[]=two". Could not yet find the responsible piece of code, though. Happens if $location is injected.

@pkozlowski-opensource
Copy link
Member

Since there are different back-ends and encoding schemas maybe the way to move forward would be to add a configurable strategy (on the $httpProvider-level?)

@Phantoms
Copy link

Being brought up by php/rails/jquery i always assumed that "?x[]=one&x[]=two" was the only desired serialization approach. And now i'm very confused to find out that there are other approaches, entirely blocking me from using angularjs with rails backend :( I had tremendous fun learning the framework up until this point though.

I've looked into $httpProvider code briefly, but couldn't quite figure how to approach the 'configurable strategy' solution. May i ask for some tips on where to look?

Also, how does $location link into this behavior? I guess i could work around this problem if i could keep $location from mangling the incoming query string.

Would a separate issue for this be more helpful, since i'm not committing any code yet?

@marknadig
Copy link
Contributor

@Phantoms this PR was landed 2a21234 and should provide what you are expecting w/ array serlialization. The fix was to have $resource delegate the encoding to $http which had a separate fix for array serialization. Does that help?

@Phantoms
Copy link

I'll give it a try, thanks!
Doesn't it serialize into ?x=one&x=two, though? It is not quite the same as ?x[]=one&x[]=two.

@marknadig
Copy link
Contributor

Andrei, it should serialize as you expect. (I'm also using rails).

@Phantoms
Copy link

Thank you so much for your fix, Mark. It does indeed work as expected.
There is one more issue though, probably not directly related.
If you inject $location, and try to enter a path like ?x[]=one&x[]=two&x[]=three, it'll be changed into ?x[]=three. No clue how it works.

@ghost ghost assigned IgorMinar Apr 11, 2013
@petebacondarwin
Copy link
Member

It appears that this functionality has been merged now. Can we close this PR then?

@Phantoms - you mention another issue? If this is still the case, can you create a new issue/PR for it?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants