mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +00:00
default coercion format reads from Muuntaja keys
This commit is contained in:
parent
73a5bd2d3d
commit
bcc2564498
2 changed files with 49 additions and 4 deletions
|
|
@ -66,9 +66,12 @@
|
||||||
:request request
|
:request request
|
||||||
:response response}))))
|
:response response}))))
|
||||||
|
|
||||||
|
(defn extract-request-format-default [request]
|
||||||
|
(-> request :muuntaja/request :format))
|
||||||
|
|
||||||
;; TODO: support faster key walking, walk/keywordize-keys is quite slow...
|
;; TODO: support faster key walking, walk/keywordize-keys is quite slow...
|
||||||
(defn request-coercer [coercion type model {:keys [extract-request-format]
|
(defn request-coercer [coercion type model {:keys [extract-request-format]
|
||||||
:or {extract-request-format (constantly nil)}}]
|
:or {extract-request-format extract-request-format-default}}]
|
||||||
(if coercion
|
(if coercion
|
||||||
(let [{:keys [keywordize? open? in style]} (ring-parameter-coercion type)
|
(let [{:keys [keywordize? open? in style]} (ring-parameter-coercion type)
|
||||||
transform (comp (if keywordize? walk/keywordize-keys identity) in)
|
transform (comp (if keywordize? walk/keywordize-keys identity) in)
|
||||||
|
|
@ -82,8 +85,11 @@
|
||||||
(request-coercion-failed! result coercion value in request)
|
(request-coercion-failed! result coercion value in request)
|
||||||
result))))))
|
result))))))
|
||||||
|
|
||||||
|
(defn extract-response-format-default [request response]
|
||||||
|
(-> request :muuntaja/response :format))
|
||||||
|
|
||||||
(defn response-coercer [coercion body {:keys [extract-response-format]
|
(defn response-coercer [coercion body {:keys [extract-response-format]
|
||||||
:or {extract-response-format (constantly nil)}}]
|
:or {extract-response-format extract-response-format-default}}]
|
||||||
(if coercion
|
(if coercion
|
||||||
(let [coercer (-response-coercer coercion body)]
|
(let [coercer (-response-coercer coercion body)]
|
||||||
(fn [request response]
|
(fn [request response]
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,13 @@
|
||||||
[reitit.ring :as ring]
|
[reitit.ring :as ring]
|
||||||
[reitit.ring.coercion :as rrc]
|
[reitit.ring.coercion :as rrc]
|
||||||
[reitit.coercion.spec :as spec]
|
[reitit.coercion.spec :as spec]
|
||||||
[reitit.coercion.schema :as schema])
|
[reitit.coercion.schema :as schema]
|
||||||
|
#?(:clj
|
||||||
|
[muuntaja.middleware])
|
||||||
|
[jsonista.core :as j])
|
||||||
#?(:clj
|
#?(:clj
|
||||||
(:import (clojure.lang ExceptionInfo))))
|
(:import (clojure.lang ExceptionInfo)
|
||||||
|
(java.io ByteArrayInputStream))))
|
||||||
|
|
||||||
(defn handler [{{{:keys [a]} :query
|
(defn handler [{{{:keys [a]} :query
|
||||||
{:keys [b]} :body
|
{:keys [b]} :body
|
||||||
|
|
@ -145,3 +149,38 @@
|
||||||
(testing "invalid response"
|
(testing "invalid response"
|
||||||
(let [{:keys [status]} (app invalid-request2)]
|
(let [{:keys [status]} (app invalid-request2)]
|
||||||
(is (= 500 status))))))))))
|
(is (= 500 status))))))))))
|
||||||
|
|
||||||
|
#?(:clj
|
||||||
|
(deftest muuntaja-test
|
||||||
|
(let [app (ring/ring-handler
|
||||||
|
(ring/router
|
||||||
|
["/api"
|
||||||
|
["/plus"
|
||||||
|
{:post {:parameters {:body {:int int?, :keyword keyword?}}
|
||||||
|
:responses {200 {:body {:int int?, :keyword keyword?}}}
|
||||||
|
:handler (fn [{{:keys [body]} :parameters}]
|
||||||
|
{:status 200
|
||||||
|
:body body})}}]]
|
||||||
|
{:data {:middleware [muuntaja.middleware/wrap-format
|
||||||
|
rrc/coerce-request-middleware
|
||||||
|
rrc/coerce-response-middleware]
|
||||||
|
:coercion spec/coercion}}))
|
||||||
|
request (fn [content-type body]
|
||||||
|
(-> {:request-method :post
|
||||||
|
:headers {"content-type" content-type, "accept" content-type}
|
||||||
|
:uri "/api/plus"
|
||||||
|
:body body}))
|
||||||
|
data-edn {:int 1 :keyword :kikka}
|
||||||
|
data-json {:int 1 :keyword "kikka"}]
|
||||||
|
|
||||||
|
(testing "json coercion"
|
||||||
|
(let [e2e #(-> (request "application/json" (ByteArrayInputStream. (j/write-value-as-bytes %)))
|
||||||
|
(app) :body (slurp) (j/read-value (j/object-mapper {:decode-key-fn true})))]
|
||||||
|
(is (= data-json (e2e data-edn)))
|
||||||
|
(is (= data-json (e2e data-json)))))
|
||||||
|
|
||||||
|
(testing "edn coercion"
|
||||||
|
(let [e2e #(-> (request "application/edn" (pr-str %))
|
||||||
|
(app) :body slurp (read-string))]
|
||||||
|
(is (= data-edn (e2e data-edn)))
|
||||||
|
(is (thrown? ExceptionInfo (e2e data-json))))))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue