diff --git a/modules/reitit-middleware/src/reitit/ring/middleware/muuntaja.clj b/modules/reitit-middleware/src/reitit/ring/middleware/muuntaja.clj index 6192a4ef..21c0548a 100644 --- a/modules/reitit-middleware/src/reitit/ring/middleware/muuntaja.clj +++ b/modules/reitit-middleware/src/reitit/ring/middleware/muuntaja.clj @@ -4,13 +4,36 @@ [clojure.spec.alpha :as s])) (s/def ::muuntaja (partial instance? m/Muuntaja)) +(s/def ::spec (s/keys :opt-un [::muuntaja])) (def format-middleware {:name ::formats - :spec (s/keys :opt-un [::muuntaja]) + :spec ::spec :compile (fn [{:keys [muuntaja]} _] (if muuntaja {:data {:swagger {:produces (m/encodes muuntaja) :consumes (m/decodes muuntaja)}} - :wrap (fn [handler] - (muuntaja.middleware/wrap-format handler muuntaja))}))}) + :wrap #(muuntaja.middleware/wrap-format % muuntaja)}))}) + +(def format-negotiate-middleware + {:name ::formats + :spec ::spec + :compile (fn [{:keys [muuntaja]} _] + (if muuntaja + {:wrap #(muuntaja.middleware/wrap-format-negotiate % muuntaja)}))}) + +(def format-request-middleware + {:name ::formats + :spec ::spec + :compile (fn [{:keys [muuntaja]} _] + (if muuntaja + {:data {:swagger {:consumes (m/decodes muuntaja)}} + :wrap #(muuntaja.middleware/wrap-format-request % muuntaja)}))}) + +(def format-response-middleware + {:name ::formats + :spec ::spec + :compile (fn [{:keys [muuntaja]} _] + (if muuntaja + {:data {:swagger {:produces (m/encodes muuntaja)}} + :wrap #(muuntaja.middleware/wrap-format-response % muuntaja)}))}) diff --git a/test/clj/reitit/ring/middleware/muuntaja_test.clj b/test/clj/reitit/ring/middleware/muuntaja_test.clj index 690f7a5c..f3d7fa31 100644 --- a/test/clj/reitit/ring/middleware/muuntaja_test.clj +++ b/test/clj/reitit/ring/middleware/muuntaja_test.clj @@ -83,3 +83,61 @@ (is (= #{"application/edn"} (produces path) (consumes path))))))) + +(deftest muuntaja-swagger-parts-test + (let [app (ring/ring-handler + (ring/router + [["/request" + {:middleware [muuntaja/format-negotiate-middleware + muuntaja/format-request-middleware] + :get identity}] + ["/response" + {:middleware [muuntaja/format-negotiate-middleware + muuntaja/format-response-middleware] + :get identity}] + ["/both" + {:middleware [muuntaja/format-negotiate-middleware + muuntaja/format-response-middleware + muuntaja/format-request-middleware] + :get identity}] + + ["/swagger.json" + {:get {:no-doc true + :handler (swagger/create-swagger-handler)}}]] + {:data {:muuntaja m/instance}})) + spec (fn [path] + (-> {:request-method :get :uri "/swagger.json"} + (app) :body :paths (get path) :get)) + produces (comp :produces spec) + consumes (comp :consumes spec)] + + (testing "just request formatting" + (let [path "/request"] + (is (nil? (produces path))) + (is (= #{"application/json" + "application/transit+msgpack" + "application/transit+json" + "application/edn"} + (consumes path))))) + + (testing "just response formatting" + (let [path "/response"] + (is (= #{"application/json" + "application/transit+msgpack" + "application/transit+json" + "application/edn"} + (produces path))) + (is (nil? (consumes path))))) + + (testing "just response formatting" + (let [path "/both"] + (is (= #{"application/json" + "application/transit+msgpack" + "application/transit+json" + "application/edn"} + (produces path))) + (is (= #{"application/json" + "application/transit+msgpack" + "application/transit+json" + "application/edn"} + (consumes path)))))))