From bcc256449855f76dd3f05f20bd358ea19d0aa822 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Mon, 11 Jun 2018 08:52:53 +0300 Subject: [PATCH] default coercion format reads from Muuntaja keys --- modules/reitit-core/src/reitit/coercion.cljc | 10 ++++- test/cljc/reitit/ring_coercion_test.cljc | 43 +++++++++++++++++++- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/modules/reitit-core/src/reitit/coercion.cljc b/modules/reitit-core/src/reitit/coercion.cljc index badf1916..8389c79e 100644 --- a/modules/reitit-core/src/reitit/coercion.cljc +++ b/modules/reitit-core/src/reitit/coercion.cljc @@ -66,9 +66,12 @@ :request request :response response})))) +(defn extract-request-format-default [request] + (-> request :muuntaja/request :format)) + ;; TODO: support faster key walking, walk/keywordize-keys is quite slow... (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 (let [{:keys [keywordize? open? in style]} (ring-parameter-coercion type) transform (comp (if keywordize? walk/keywordize-keys identity) in) @@ -82,8 +85,11 @@ (request-coercion-failed! result coercion value in request) result)))))) +(defn extract-response-format-default [request response] + (-> request :muuntaja/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 (let [coercer (-response-coercer coercion body)] (fn [request response] diff --git a/test/cljc/reitit/ring_coercion_test.cljc b/test/cljc/reitit/ring_coercion_test.cljc index 11eb5141..eea50d6f 100644 --- a/test/cljc/reitit/ring_coercion_test.cljc +++ b/test/cljc/reitit/ring_coercion_test.cljc @@ -4,9 +4,13 @@ [reitit.ring :as ring] [reitit.ring.coercion :as rrc] [reitit.coercion.spec :as spec] - [reitit.coercion.schema :as schema]) + [reitit.coercion.schema :as schema] + #?(:clj + [muuntaja.middleware]) + [jsonista.core :as j]) #?(:clj - (:import (clojure.lang ExceptionInfo)))) + (:import (clojure.lang ExceptionInfo) + (java.io ByteArrayInputStream)))) (defn handler [{{{:keys [a]} :query {:keys [b]} :body @@ -145,3 +149,38 @@ (testing "invalid response" (let [{:keys [status]} (app invalid-request2)] (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))))))))