ring-clojure / ring-json

Ring middleware for handling JSON
313 stars 47 forks source link

Support for non-object JSON body #41

Closed eryi closed 8 years ago

eryi commented 8 years ago

On line 48 of json.cli :

(defn- assoc-json-params [request json]
  (if (map? json)
    (-> request
        (assoc :json-params json)
        (update-in [:params] merge json))
    request))

The map? mysteriously filters out perfectly valid JSON strings and arrays. Why is this so?

weavejester commented 8 years ago

Because the wrap-json-params middleware treats JSON request bodies as a map of parameters. If the request body isn't a JSON object, it can't treat it as a map of parameters.

You'll note that wrap-json-body will accept any valid JSON data.

eryi commented 8 years ago

I guess the confusing part is that while malformed JSON throws an error, properly formatted non-objects JSON gets silently discarded. Would it be a good idea for wrap-json-params to treat non-objects as malformed and throw errors?

weavejester commented 8 years ago

wrap-json-params works the same way as wrap-params, in that it only assocs valid parameters.

In general, consider wrap-json-params as a version of wrap-params that works with JSON. Most of the time you'll want to use wrap-json-body instead. wrap-json-params might even be deprecated in future versions.