Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure ring wrap-json-params messing up JSON arrays

I'm currently doing some REST API stuff in clojure, and I am using the ring.middleware.format library with compojure to transform JSON to and from clojure data structures.

I am having a huge issue, in that and JSON posted to the ring app will have all arrays replaced with the first item that was in the array. I.E. it will turn this JSON posted to it from

{
    "buyer":"Test Name",
    "items":[
        {"qty":1,"size":"S","product":"Red T-Shirt"},
        {"qty":1,"size":"M","product":"Green T-Shirt"}
    ],
    "address":"123 Fake St",
    "shipping":"express"
}

to this

{
    "buyer": "Test Name",
    "items": {
        "qty": 1,
        "size": "M",
        "product": "Green T-Shirt"
    },
    "address": "123 Fake St",
    "shipping": "express"
}

It does it for any arrays, including when an array is the root element.

I am using the following code in clojure to return the json:

(defroutes app-routes
  (GET "/"
       []
       {:body test-data})
  (POST "/"
        {data :params}
        {:body data}))
        ;{:body (str "Printing " (count (data :jobs)) " jobs")}))

(def app
  (-> (handler/api app-routes)
      (wrap-json-params)
      (wrap-json-response)))

The GET route has no issues with arrays and outputs properly, so it has to be either the way I am getting the data or the wrap-restful-params middleware.

Any ideas?

like image 672
Tom Brunoli Avatar asked Oct 31 '12 05:10

Tom Brunoli


3 Answers

I am having similar problem with ring-json-params. So I ended up using raw request body and parsing the JSON string myself.

I used the folliwng code:

(defroutes app-routes
(POST "/"
    {params :body}
    (slurp params)))

I use the clj-json.core library for parsing JSON.

Hope this helps. If you figured out a better way then please share. I am a Clojure/Compojure newbie!!!

like image 60
Dhruv Chandna Avatar answered Nov 15 '22 08:11

Dhruv Chandna


I ran into this problem recently and actually figured out what the problem is: it's that you have wrap-nested-params middleware getting evaluated after your wrap-json-params middleware, which causes objects stored in JSON arrays/Clojure vectors to be flattened by grabbing the first element contained within.

user=> bod-map
{:address "100 Bush Street", :contacts [{:name "Dudely Mcdooderson", :title "engineer", :tax_id "555"}], :name "Dudely Inc.", :tax_id "5234234"}
user=> (ring.middleware.nested-params/nested-params-request {:params bod-map})
{:params {"tax_id" "5234234", "name" "Dudely Inc.", "contacts" {:name "Dudely Mcdooderson", :title "engineer", :tax_id "555"}, "address" "100 Bush Street"}}

You can get around this by just making sure that wrap-nested-params is evaluated first in your middleware ordering.

like image 28
Venantius Avatar answered Nov 15 '22 08:11

Venantius


I know it's been some time but I did just stumble upon the same issue. The only way I got it to work was to use the ring.middleware.format/wrap-restful-format before the compojure.handler/api. I'm not sure why that is but if I put the compujure.handler/api wrapper first, it messes up the array parameteres

like image 41
ls4f Avatar answered Nov 15 '22 09:11

ls4f