diff --git a/modules/reitit-middleware/src/reitit/ring/middleware/alpha/muuntaja.cljc b/modules/reitit-middleware/src/reitit/ring/middleware/alpha/muuntaja.cljc index b45ba1ae..3cafdcd4 100644 --- a/modules/reitit-middleware/src/reitit/ring/middleware/alpha/muuntaja.cljc +++ b/modules/reitit-middleware/src/reitit/ring/middleware/alpha/muuntaja.cljc @@ -12,6 +12,6 @@ (if options (let [m (m/create options)] {:data {:swagger {:produces (m/encodes m) - :consumes (m/encodes m)}} + :consumes (m/decodes m)}} :wrap (fn [handler] (muuntaja.middleware/wrap-format handler m))}))))})) diff --git a/modules/reitit-swagger/src/reitit/swagger.cljc b/modules/reitit-swagger/src/reitit/swagger.cljc index a5e6511d..dc2eca78 100644 --- a/modules/reitit-swagger/src/reitit/swagger.cljc +++ b/modules/reitit-swagger/src/reitit/swagger.cljc @@ -81,10 +81,11 @@ (merge {:swagger "2.0" :x-id ids})) accept-route #(-> % second :swagger :id (or ::default) ->set (set/intersection ids) seq) - transform-endpoint (fn [[method {{:keys [coercion no-doc swagger] :as data} :data}]] + transform-endpoint (fn [[method {{:keys [coercion no-doc swagger] :as data} :data middleware :middleware}]] (if (and data (not no-doc)) [method (meta-merge + (apply meta-merge (keep (comp :swagger :data) middleware)) (if coercion (coercion/-get-apidocs coercion :swagger data)) (select-keys data [:tags :summary :description]) diff --git a/test/clj/reitit/ring/middleware/muuntaja_test.clj b/test/clj/reitit/ring/middleware/muuntaja_test.clj index 521da94c..ecda573d 100644 --- a/test/clj/reitit/ring/middleware/muuntaja_test.clj +++ b/test/clj/reitit/ring/middleware/muuntaja_test.clj @@ -2,7 +2,9 @@ (:require [clojure.test :refer [deftest testing is]] [reitit.ring :as ring] [reitit.ring.middleware.alpha.muuntaja :as muuntaja] - [muuntaja.core :as m])) + [muuntaja.core :as m] + [reitit.swagger :as swagger] + [reitit.core :as r])) (deftest muuntaja-test (let [data {:kikka "kukka"} @@ -15,4 +17,68 @@ :body (m/decode m/instance "application/json")))))) -;; TODO: test swagger! +(deftest muuntaja-swagger-test + (let [with-defaults m/instance + no-edn-decode (m/create (-> m/default-options (update-in [:formats "application/edn"] dissoc :decoder))) + just-edn (m/create (-> m/default-options (m/select-formats ["application/edn"]))) + app (ring/ring-handler + (ring/router + [["/defaults" + {:get identity}] + ["/explicit-defaults" + {:muuntaja with-defaults + :get identity}] + ["/no-edn-decode" + {:muuntaja no-edn-decode + :get identity}] + ["/just-edn" + {:muuntaja just-edn + :get identity}] + ["/swagger.json" + {:get {:no-doc true + :handler (swagger/create-swagger-handler)}}]] + {:data {:middleware [(muuntaja/create-format-middleware)]}})) + spec (fn [path] + (let [path (keyword path)] + (-> {:request-method :get :uri "/swagger.json"} + (app) :body + (->> (m/decode m/instance "application/json")) + :paths path :get))) + produces (comp set :produces spec) + consumes (comp set :consumes spec)] + + (testing "with defaults" + (let [path "/defaults"] + (is (= #{"application/json" + "application/transit+msgpack" + "application/transit+json" + "application/edn"} + (produces path) + (consumes path))))) + + (testing "with explicit muuntaja defaults" + (let [path "/explicit-defaults"] + (is (= #{"application/json" + "application/transit+msgpack" + "application/transit+json" + "application/edn"} + (produces path) + (consumes path))))) + + (testing "without edn decode" + (let [path "/no-edn-decode"] + (is (= #{"application/json" + "application/transit+msgpack" + "application/transit+json" + "application/edn"} + (produces path))) + (is (= #{"application/json" + "application/transit+msgpack" + "application/transit+json"} + (consumes path))))) + + (testing "just edn" + (let [path "/just-edn"] + (is (= #{"application/edn"} + (produces path) + (consumes path))))))) diff --git a/test/cljc/reitit/swagger_test.clj b/test/cljc/reitit/swagger_test.clj index 7c563ec2..b29b9305 100644 --- a/test/cljc/reitit/swagger_test.clj +++ b/test/cljc/reitit/swagger_test.clj @@ -180,5 +180,5 @@ :handler (swagger/create-swagger-handler)}}]]))] (is (= ["/ping"] (spec-paths app "/swagger.json"))) (is (= #{::swagger/default} - (-> {:request-method :get :uri "/swagger.json"} - (app) :body :x-id))))) + (-> {:request-method :get :uri "/swagger.json"} + (app) :body :x-id)))))