mirror of
https://github.com/metosin/reitit.git
synced 2025-12-19 09:21:10 +00:00
read encodes and decodes from Muuntaja!
This commit is contained in:
parent
10ccbb72e3
commit
b54ee1a86b
4 changed files with 73 additions and 6 deletions
|
|
@ -12,6 +12,6 @@
|
||||||
(if options
|
(if options
|
||||||
(let [m (m/create options)]
|
(let [m (m/create options)]
|
||||||
{:data {:swagger {:produces (m/encodes m)
|
{:data {:swagger {:produces (m/encodes m)
|
||||||
:consumes (m/encodes m)}}
|
:consumes (m/decodes m)}}
|
||||||
:wrap (fn [handler]
|
:wrap (fn [handler]
|
||||||
(muuntaja.middleware/wrap-format handler m))}))))}))
|
(muuntaja.middleware/wrap-format handler m))}))))}))
|
||||||
|
|
|
||||||
|
|
@ -81,10 +81,11 @@
|
||||||
(merge {:swagger "2.0"
|
(merge {:swagger "2.0"
|
||||||
:x-id ids}))
|
:x-id ids}))
|
||||||
accept-route #(-> % second :swagger :id (or ::default) ->set (set/intersection ids) seq)
|
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))
|
(if (and data (not no-doc))
|
||||||
[method
|
[method
|
||||||
(meta-merge
|
(meta-merge
|
||||||
|
(apply meta-merge (keep (comp :swagger :data) middleware))
|
||||||
(if coercion
|
(if coercion
|
||||||
(coercion/-get-apidocs coercion :swagger data))
|
(coercion/-get-apidocs coercion :swagger data))
|
||||||
(select-keys data [:tags :summary :description])
|
(select-keys data [:tags :summary :description])
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,9 @@
|
||||||
(:require [clojure.test :refer [deftest testing is]]
|
(:require [clojure.test :refer [deftest testing is]]
|
||||||
[reitit.ring :as ring]
|
[reitit.ring :as ring]
|
||||||
[reitit.ring.middleware.alpha.muuntaja :as muuntaja]
|
[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
|
(deftest muuntaja-test
|
||||||
(let [data {:kikka "kukka"}
|
(let [data {:kikka "kukka"}
|
||||||
|
|
@ -15,4 +17,68 @@
|
||||||
:body
|
:body
|
||||||
(m/decode m/instance "application/json"))))))
|
(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)))))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue