Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested Javascript arrays don't decode well #84

Closed
mrocklin opened this issue Aug 20, 2013 · 4 comments
Closed

Nested Javascript arrays don't decode well #84

mrocklin opened this issue Aug 20, 2013 · 4 comments

Comments

@mrocklin
Copy link

Nested JavaScript arrays in AJAX call data to a ring application fail to parse. I think I can reproduce the error as far down as form-decode.

Disclaimer: I'm new to Ring and web programming in general. Please correct me if this ticket should be filed elsewhere.

AJAX call

$( document ).ready( function() {
    $.ajax({
        url: "/query/",
        data: {want: "C", constraints: [["A", "B"]]},
        success: function(data, aStatus, dummy){
            alert(data);
        }
    });
});

Result on Server

{:scheme :http, :request-method :get, 
 :query-string want=C&constraints%5B0%5D%5B%5D=A&constraints%5B0%5D%5B%5D=B, 
 :route-params {}, :content-type nil, :params {}, 
 :body #<HttpInput org.eclipse.jetty.server.HttpInput@1bd06bf>
 ....}

(clojure.pprint/pprint (-> result :query-string form-decode))

{"constraints[0][]" ["A" "B"], "want" "C"}

Desired Result

{"constraints" [["A" "B"]], "want" "C"}
@weavejester
Copy link
Member

You don't seem to be applying the wrap-nested-params middleware. Nested parameters are not something inherent to HTTP; they're simply a convention adopted by certain frameworks. In Ring you have the option of using this syntax via the wrap-nested-params middleware.

However, the desired result you want won't work with the input you provide, whether in Ring:

user=> (nested-params-request {:params {"contraints[0][]" ["A" "B"], "want" "C"}})
{:params {"want" "C", "contraints" {"0" ["A" "B"]}}}

Or in Rails:

irb> Rack::Utils.parse_nested_query("constraints[0][]=A&constraints[0][]=B&want=C")
=> {"constraints"=>{"0"=>["A", "B"]}, "want"=>"C"}

@eigenhombre
Copy link

@weavejester Is the conversion of {constraints: [["A", "B"]]} into {"constraints[0][]" ["A" "B"]} done on the jQuery side, or the Ring side? Because nested arrays serialize just fine into JSON in general:

>  python
Python 2.6.8 (unknown, Nov 17 2012, 21:23:35) 
>>> import json
>>> json.dumps({"want": "C", "constraints": [["A","B"]]})
'{"want": "C", "constraints": [["A", "B"]]}'

@mrocklin You don't state in the ticket if this gives the same result with a POST as it does with GET. Does it? Because GETs have to stuff their parameters into the HTTP URL arguments, I can understand why this would go sideways -- but maybe POSTs are more robust (since you can just pass data, unmunged...?).

I guess I'm confused exactly where this simple thing is breaking down. And, what people usually do about it. Thanks!

@mrocklin
Copy link
Author

Post does better because it does actually decode the query-string and push the result into the :params key (EDIT: this is probably because I started wrapping my routes with compojure.handler/api). However it also fails to a nested array. I suspect that this still depends on form-decode.

$.post("/query/", {want: "C", constraints: [["A", "B"]]},
    function(data, aStatus, dummy){
        alert(data);
    });
{:scheme :http,
 :query-params {},
 :form-params {"constraints[0][]" ["A" "B"], "want" "C"},
 :request-method :post,
 :query-string nil,
 :params {:want "C", :constraints {"0" ["A" "B"]}},
 :body #<HttpInput org.eclipse.jetty.server.HttpInput@16535bf>}

From what @weavejester writes it seems that my issue is really what I should expect from any system. It seems like the best path may be to kludge together a local array-from-map function.

@weavejester
Copy link
Member

@weavejester Is the conversion of {constraints: [["A", "B"]]} into {"constraints[0][]" ["A" "B"]} done on the jQuery side, or the Ring side?

On the jQuery side, since that's where the request is coming from.

From what @weavejester writes it seems that my issue is really what I should expect from any system. It seems like the best path may be to kludge together a local array-from-map function.

Or just use JSON, instead of trying to encode a data structure in form parameters, which aren't really designed for that.

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

No branches or pull requests

3 participants