ring-clojure / ring-json

Ring middleware for handling JSON
313 stars 47 forks source link

IANA json content types, JSON Arrays, etc. #18

Closed daviddpark closed 10 years ago

daviddpark commented 10 years ago

A recent change for #16 will now only provide the data in :json-params and :params when the JSON submitted with a request is an object.

I am working on a clojure implementation for JSON patch: and the specification states:

A JSON Patch document is a JSON [RFC4627] document that represents an array of objects. Each object represents a single operation to be applied to the target JSON document.

Of course, the media type is currently incompatible with the current regular expression for ring-json:

The "application/json-patch+json" media type is used to identify such patch documents.

(defn- json-request? [request]
  (if-let [type (:content-type request)]
    (not (empty? (re-find #"^application/(vnd.+)?json" type)))))

To that end, there are a number of JSON media types proposed to the IANA that are not compatible with that particular regular expression.

I would be happy to submit a pull request for a modified regular expression if that would be well-received.

I also would like to discuss a slight modification of the code introduced for #16 that will only merge maps into request :params, but will enable arrays to be passed through to :json-params, or perhaps introduce :json-body or :json-array for when the incoming JSON is not an object.

Thoughts?

weavejester commented 10 years ago

That sounds fine. There's also a application/schema+json that isn't covered by that regex. It could do with improving.

daviddpark commented 10 years ago

Would this be too greedy for your tastes, Jim?

#"^application/(.+\+)?json"
weavejester commented 10 years ago

I was thinking along similar lines. I can't think of any place where that wouldn't work. Certainly all of the official media types fit into that pattern.

daviddpark commented 10 years ago

Thanks for your consideration of the pull request, Jim!