mirror of
https://github.com/metosin/reitit.git
synced 2025-12-16 16:01:11 +00:00
feat: use request Content-Type or :muuntaja/content-type to coerce
Previously, `extract-response-format-default` was only looking at (-> request :muuntaja/response :format). This led to wrong behaviour when there were separate schemas for separate response content types and an explicitly picked content-type for the response.
This commit is contained in:
parent
f4da07c222
commit
7fb9c27e46
3 changed files with 239 additions and 93 deletions
|
|
@ -202,9 +202,11 @@ is:
|
|||
"application/edn" {:schema {:x s/Int}}
|
||||
:default {:schema {:ww s/Int}}}}}
|
||||
:handler ...}}]]
|
||||
{:data {:middleware [rrc/coerce-exceptions-middleware
|
||||
rrc/coerce-request-middleware
|
||||
rrc/coerce-response-middleware]}})))
|
||||
{:data {:muuntaja muuntaja.core/instance
|
||||
:middleware [reitit.ring.middleware.muuntaja/format-middleware
|
||||
reitit.ring.coercion/coerce-exceptions-middleware
|
||||
reitit.ring.coercion/coerce-request-middleware
|
||||
reitit.ring.coercion/coerce-response-middleware]}})))
|
||||
```
|
||||
|
||||
The resolution logic for response coercers is:
|
||||
|
|
@ -215,6 +217,17 @@ The resolution logic for response coercers is:
|
|||
3. `:body`
|
||||
3. If nothing was found, do not coerce
|
||||
|
||||
To select the response content-type, you can either:
|
||||
1. Let muuntaja pick the content-type based on things like the request Accept header
|
||||
- This is what most users want
|
||||
2. Set `:muuntaja/content-type` in the response to pick an explicit content type
|
||||
3. Set the `"Content-Type"` header in the response
|
||||
- This disables muuntaja, so you need to encode your response body in some other way!
|
||||
- This is not compatible with response schema checking, since coercion won't know what to do with the already-encoded response body.
|
||||
4. Use the `:extract-response-format` option to inject your own logic. See `reitit.coercion/extract-response-format-default` for the default.
|
||||
|
||||
See also the [muuntaja content negotiation](./content_negotiation.md) docs.
|
||||
|
||||
## Pretty printing spec errors
|
||||
|
||||
Spec problems are exposed as is in request & response coercion errors. Pretty-printers like [expound](https://github.com/bhb/expound) can be enabled like this:
|
||||
|
|
|
|||
|
|
@ -152,8 +152,10 @@
|
|||
rcs (request-coercers coercion parameters (cond-> opts route-request (assoc ::skip #{:body})))]
|
||||
(if (and crc rcs) (into crc (vec rcs)) (or crc rcs)))))
|
||||
|
||||
(defn extract-response-format-default [request _]
|
||||
(-> request :muuntaja/response :format))
|
||||
(defn extract-response-format-default [request response]
|
||||
(or (get-in response [:headers "Content-Type"])
|
||||
(:muuntaja/content-type response)
|
||||
(-> request :muuntaja/response :format)))
|
||||
|
||||
(defn -format->coercer [coercion {:keys [content body]} _opts]
|
||||
(->> (concat (when body
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
(ns reitit.ring-coercion-test
|
||||
(:require [clojure.test :refer [deftest is testing]]
|
||||
[malli.experimental.lite :as l]
|
||||
#?@(:clj [[muuntaja.middleware]
|
||||
[jsonista.core :as j]])
|
||||
#?@(:clj [[muuntaja.core]
|
||||
[muuntaja.middleware]
|
||||
[jsonista.core :as j]
|
||||
[reitit.ring.middleware.muuntaja]])
|
||||
[malli.core :as m]
|
||||
[malli.util :as mu]
|
||||
[meta-merge.core :refer [meta-merge]]
|
||||
|
|
@ -585,6 +587,8 @@
|
|||
|
||||
#?(:clj
|
||||
(deftest per-content-type-test
|
||||
(let [normalize-json (fn [resp]
|
||||
(update resp :body #(-> % j/write-value-as-string (j/read-value j/keyword-keys-object-mapper))))]
|
||||
(doseq [[coercion json-request edn-request default-request json-response edn-response default-response]
|
||||
[[malli/coercion
|
||||
[:map [:request [:enum :json]] [:response any?]]
|
||||
|
|
@ -654,9 +658,7 @@
|
|||
:uri "/foo"
|
||||
:muuntaja/request {:format request-format}
|
||||
:muuntaja/response {:format response-format}
|
||||
:body-params body})
|
||||
normalize-json (fn[body]
|
||||
(-> body j/write-value-as-string (j/read-value j/keyword-keys-object-mapper)))]
|
||||
:body-params body})]
|
||||
(testing "succesful call"
|
||||
(is (= {:status 200 :body {:request "json", :response "json"}}
|
||||
(normalize-json (call (request "application/json" "application/json" {:request :json :response :json})))))
|
||||
|
|
@ -677,7 +679,46 @@
|
|||
(is (= {:type :reitit.coercion/response-coercion :in [:response :body]}
|
||||
(call (request "application/json" "application/edn" {:request :json :response :json}))))
|
||||
(is (= {:type :reitit.coercion/response-coercion :in [:response :body]}
|
||||
(call (request "application/json" "application/transit" {:request :json :response :json}))))))))))))
|
||||
(call (request "application/json" "application/transit" {:request :json :response :json}))))))))
|
||||
(testing "explicit response content type"
|
||||
(let [response (atom nil)
|
||||
app (ring/ring-handler
|
||||
(ring/router
|
||||
["/foo" {:post {:responses {200 {:content {"application/json" {:schema json-response}
|
||||
"application/edn" {:schema edn-response}
|
||||
:default {:schema default-response}}}}
|
||||
:handler (fn [req]
|
||||
@response)}}]
|
||||
{:validate reitit.ring.spec/validate
|
||||
:data {:middleware [rrc/coerce-request-middleware
|
||||
rrc/coerce-response-middleware]
|
||||
:coercion coercion}}))
|
||||
call (fn [request]
|
||||
(try
|
||||
(app request)
|
||||
(catch ExceptionInfo e
|
||||
#_(ex-data e)
|
||||
(select-keys (ex-data e) [:type :in]))))
|
||||
request (fn [request-format body resp]
|
||||
(reset! response resp)
|
||||
{:request-method :post
|
||||
:uri "/foo"
|
||||
:muuntaja/request {:format request-format}
|
||||
:body-params body})]
|
||||
(testing "via :headers \"Content-Type\""
|
||||
(is (= {:status 200 :body {:request "json" :response "json"} :headers {"Content-Type" "application/json"}}
|
||||
(normalize-json (call (request "application/json" {:request :json :response :json} {:status 200 :body {:request :json :response :json} :headers {"Content-Type" "application/json"}}))))
|
||||
"valid reponse")
|
||||
(is (= {:in [:response :body] :type :reitit.coercion/response-coercion}
|
||||
(call (request "application/json" {:request :json :response :json} {:status 200 :body {:request :json :response :invalid} :headers {"Content-Type" "application/json"}})))
|
||||
"invalid reponse"))
|
||||
(testing "via :muuntaja/content-type"
|
||||
(is (= {:status 200 :body {:request "json" :response "json"} :muuntaja/content-type "application/json"}
|
||||
(normalize-json (call (request "application/json" {:request :json :response :json} {:status 200 :body {:request :json :response :json} :muuntaja/content-type "application/json"}))))
|
||||
"valid reponse")
|
||||
(is (= {:in [:response :body] :type :reitit.coercion/response-coercion}
|
||||
(call (request "application/json" {:request :json :response :json} {:status 200 :body {:request :json :response :invalid} :muuntaja/content-type "application/json"})))
|
||||
"invalid reponse")))))))))
|
||||
|
||||
|
||||
#?(:clj
|
||||
|
|
@ -801,3 +842,93 @@
|
|||
(app) :body slurp (read-string))]
|
||||
(is (= data-edn (e2e (assoc data-edn :EXTRA "VALUE"))))
|
||||
(is (thrown? ExceptionInfo (e2e data-json))))))))
|
||||
|
||||
#?(:clj
|
||||
(deftest muuntaja-per-content-type-coercion-test
|
||||
;; Test integration between per-content-type coercion and muuntaja.
|
||||
;; Malli-only for now.
|
||||
(let [response (atom nil)
|
||||
app (ring/ring-handler
|
||||
(ring/router
|
||||
["/foo" {:post {:request {:content {"application/json" {:schema [:map [:request [:enum :json]]]}
|
||||
"application/edn" {:schema [:map [:request [:enum :edn]]]}
|
||||
:default {:schema [:map [:request [:enum :default]]]}}}
|
||||
:responses {200 {:content {"application/json" {:schema [:map [:response [:enum :json]]]}
|
||||
"application/edn" {:schema [:map [:response [:enum :edn]]]}
|
||||
:default {}}}}
|
||||
:handler (fn [req] @response)}}]
|
||||
{:data {:middleware [reitit.ring.middleware.muuntaja/format-middleware
|
||||
rrc/coerce-request-middleware
|
||||
rrc/coerce-response-middleware]
|
||||
:muuntaja muuntaja.core/instance
|
||||
:coercion malli/coercion}}))
|
||||
maybe-slurp #(if (instance? java.io.InputStream %)
|
||||
(slurp %)
|
||||
%)
|
||||
call (fn [request resp]
|
||||
(reset! response resp)
|
||||
(try
|
||||
(-> (merge {:request-method :post :uri "/foo"} request)
|
||||
(update :body #(ByteArrayInputStream. (.getBytes % "UTF-8")))
|
||||
(app) :body (maybe-slurp))
|
||||
(catch ExceptionInfo e
|
||||
#_(ex-data e)
|
||||
(select-keys (ex-data e) [:in :type]))))
|
||||
read-json #(j/read-value % (j/object-mapper {:decode-key-fn true}))]
|
||||
(testing "response content-type defaults to json"
|
||||
(is (= {:response "json"}
|
||||
(read-json
|
||||
(call {:headers {"content-type" "application/json"}
|
||||
:body (j/write-value-as-string {:request :json})}
|
||||
{:status 200
|
||||
:body {:response :json}}))))
|
||||
(is (= {:response "json"}
|
||||
(read-json
|
||||
(call {:headers {"content-type" "application/edn"}
|
||||
:body (pr-str {:request :edn})}
|
||||
{:status 200
|
||||
:body {:response :json}}))))
|
||||
(is (= {:in [:response :body] :type :reitit.coercion/response-coercion}
|
||||
(call {:headers {"content-type" "application/json"}
|
||||
:body (j/write-value-as-string {:request :json})}
|
||||
{:status 200
|
||||
:body {:response :invalid}}))))
|
||||
(testing "response content-type negotiated via accept header"
|
||||
(is (= {:response "json"}
|
||||
(read-json
|
||||
(call {:headers {"content-type" "application/json" "accept" "application/json"}
|
||||
:body (j/write-value-as-string {:request :json})}
|
||||
{:status 200
|
||||
:body {:response :json}}))))
|
||||
(is (= {:response :edn}
|
||||
(read-string
|
||||
(call {:headers {"content-type" "application/json" "accept" "application/edn"}
|
||||
:body (j/write-value-as-string {:request :json})}
|
||||
{:status 200
|
||||
:body {:response :edn}}))))
|
||||
(is (= {:in [:response :body] :type :reitit.coercion/response-coercion}
|
||||
(call {:headers {"content-type" "application/json" "accept" "application/edn"}
|
||||
:body (j/write-value-as-string {:request :json})}
|
||||
{:status 200
|
||||
:body {:response :invalid}}))))
|
||||
(testing "response content-type set via :muuntaja/content-type"
|
||||
(is (= {:response :edn}
|
||||
(read-string
|
||||
(call {:headers {"content-type" "application/json" "accept" "application/json"}
|
||||
:body (j/write-value-as-string {:request :json})}
|
||||
{:status 200
|
||||
:muuntaja/content-type "application/edn"
|
||||
:body {:response :edn}}))))
|
||||
(is (= {:in [:response :body] :type :reitit.coercion/response-coercion}
|
||||
(call {:headers {"content-type" "application/json" "accept" "application/json"}
|
||||
:body (j/write-value-as-string {:request :json})}
|
||||
{:status 200
|
||||
:muuntaja/content-type "application/edn"
|
||||
:body {:response :invalid}}))))
|
||||
(testing "response content-type set via Content-Type header. muuntaja disabled for response."
|
||||
(is (= "custom data"
|
||||
(call {:headers {"content-type" "application/json" "accept" "application/json"}
|
||||
:body (j/write-value-as-string {:request :json})}
|
||||
{:status 200
|
||||
:headers {"Content-Type" "application/custom"}
|
||||
:body "custom data"})))))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue