mirror of
https://github.com/metosin/reitit.git
synced 2025-12-26 11:38:26 +00:00
Merge pull request #67 from metosin/SchemaToBodyInResponseCoercion
Responses have :body, not :schema
This commit is contained in:
commit
cb5e4d1177
15 changed files with 44 additions and 42 deletions
|
|
@ -74,7 +74,7 @@ A Ring routing app with input & output coercion using [data-specs](https://githu
|
|||
(ring/router
|
||||
["/api"
|
||||
["/math" {:get {:parameters {:query {:x int?, :y int?}}
|
||||
:responses {200 {:schema {:total pos-int?}}}
|
||||
:responses {200 {:body {:total pos-int?}}}
|
||||
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||
{:status 200
|
||||
:body {:total (+ x y)}})}}]]
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ Handler can access the coerced parameters can be read under `:parameters` key in
|
|||
:parameters {:query {:x s/Int}
|
||||
:body {:y s/Int}
|
||||
:path {:z s/Int}}
|
||||
:responses {200 {:schema {:total PositiveInt}}}
|
||||
:responses {200 {:body {:total PositiveInt}}}
|
||||
:handler (fn [{:keys [parameters]}]
|
||||
(let [total (+ (-> parameters :query :x)
|
||||
(-> parameters :body :y)
|
||||
|
|
@ -81,7 +81,7 @@ Here's an full example for applying coercion with Reitit, Ring and Schema:
|
|||
:parameters {:query {:x s/Int}
|
||||
:body {:y s/Int}
|
||||
:path {:z s/Int}}
|
||||
:responses {200 {:schema {:total PositiveInt}}}
|
||||
:responses {200 {:body {:total PositiveInt}}}
|
||||
:handler (fn [{:keys [parameters]}]
|
||||
(let [total (+ (-> parameters :query :x)
|
||||
(-> parameters :body :y)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
(defproject just-coercion-with-ring "0.1.0-SNAPSHOT"
|
||||
:description "Reitit coercion with vanilla ring"
|
||||
:dependencies [[org.clojure/clojure "1.9.0-RC2"]
|
||||
:dependencies [[org.clojure/clojure "1.9.0"]
|
||||
[ring "1.6.3"]
|
||||
[metosin/muuntaja "0.4.1"]
|
||||
[metosin/reitit "0.1.0-SNAPSHOT"]])
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
(ns example.server
|
||||
(:require [ring.adapter.jetty :as jetty]
|
||||
[muuntaja.middleware]
|
||||
[ring.middleware.params]
|
||||
[reitit.middleware :as middleware]
|
||||
[reitit.ring.coercion :as rrc]))
|
||||
|
||||
|
|
@ -10,9 +12,9 @@
|
|||
;; to be set with :extract-request-format and extract-response-format
|
||||
(defn wrap-coercion [handler resource]
|
||||
(middleware/chain
|
||||
[rrc/coerce-request-middleware
|
||||
rrc/coerce-response-middleware
|
||||
rrc/coerce-exceptions-middleware]
|
||||
[rrc/coerce-exceptions-middleware
|
||||
rrc/coerce-request-middleware
|
||||
rrc/coerce-response-middleware]
|
||||
handler
|
||||
resource))
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
(defproject just-coercion-with-ring "0.1.0-SNAPSHOT"
|
||||
:description "Reitit coercion with vanilla ring"
|
||||
:dependencies [[org.clojure/clojure "1.9.0-RC2"]
|
||||
(defproject ring-example "0.1.0-SNAPSHOT"
|
||||
:description "Reitit Ring App"
|
||||
:dependencies [[org.clojure/clojure "1.9.0"]
|
||||
[ring "1.6.3"]
|
||||
[metosin/muuntaja "0.4.1"]
|
||||
[metosin/reitit "0.1.0-SNAPSHOT"]])
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
(ns example.dspec
|
||||
(:require [reitit.coercion.spec :as spec-coercion]))
|
||||
(:require [reitit.coercion.spec]))
|
||||
|
||||
(def routes
|
||||
["/dspec" {:coercion spec-coercion/coercion}
|
||||
["/dspec" {:coercion reitit.coercion.spec/coercion}
|
||||
["/plus" {:name ::plus
|
||||
:responses {200 {:schema {:total int?}}}
|
||||
:responses {200 {:body {:total int?}}}
|
||||
:get {:summary "plus with query-params"
|
||||
:parameters {:query {:x int?, :y int?}}
|
||||
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
(ns example.schema
|
||||
(:require [schema.core :as s]
|
||||
[reitit.coercion.schema :as schema-coercion]))
|
||||
[reitit.coercion.schema]))
|
||||
|
||||
(def routes
|
||||
["/schema" {:coercion schema-coercion/coercion}
|
||||
["/schema" {:coercion reitit.coercion.schema/coercion}
|
||||
["/plus" {:name ::plus
|
||||
:responses {200 {:schema {:total s/Int}}}
|
||||
:responses {200 {:body {:total s/Int}}}
|
||||
:get {:summary "plus with query-params"
|
||||
:parameters {:query {:x s/Int, :y s/Int}}
|
||||
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
(ns example.spec
|
||||
(:require [clojure.spec.alpha :as s]
|
||||
[spec-tools.spec :as spec]
|
||||
[reitit.coercion.spec :as spec-coercion]))
|
||||
[reitit.coercion.spec]))
|
||||
|
||||
;; wrap into Spec Records to enable runtime conforming
|
||||
(s/def ::x spec/int?)
|
||||
|
|
@ -9,9 +9,9 @@
|
|||
(s/def ::total spec/int?)
|
||||
|
||||
(def routes
|
||||
["/spec" {:coercion spec-coercion/coercion}
|
||||
["/spec" {:coercion reitit.coercion.spec/coercion}
|
||||
["/plus" {:name ::plus
|
||||
:responses {200 {:schema (s/keys :req-un [::total])}}
|
||||
:responses {200 {:body (s/keys :req-un [::total])}}
|
||||
:get {:summary "plus with query-params"
|
||||
:parameters {:query (s/keys :req-un [::x ::y])}
|
||||
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||
|
|
|
|||
|
|
@ -89,10 +89,10 @@
|
|||
(request-coercion-failed! result coercion value in request)
|
||||
result))))))
|
||||
|
||||
(defn response-coercer [coercion model {:keys [extract-response-format]
|
||||
:or {extract-response-format (constantly nil)}}]
|
||||
(defn response-coercer [coercion body {:keys [extract-response-format]
|
||||
:or {extract-response-format (constantly nil)}}]
|
||||
(if coercion
|
||||
(let [coercer (-response-coercer coercion model)]
|
||||
(let [coercer (-response-coercer coercion body)]
|
||||
(fn [request response]
|
||||
(let [format (extract-response-format request response)
|
||||
value (:body response)
|
||||
|
|
@ -126,8 +126,8 @@
|
|||
(into {})))
|
||||
|
||||
(defn response-coercers [coercion responses opts]
|
||||
(->> (for [[status {:keys [schema]}] responses :when schema]
|
||||
[status (response-coercer coercion schema opts)])
|
||||
(->> (for [[status {:keys [body]}] responses :when body]
|
||||
[status (response-coercer coercion body opts)])
|
||||
(into {})))
|
||||
|
||||
(defn- coercers-not-compiled! [match]
|
||||
|
|
|
|||
|
|
@ -92,10 +92,10 @@
|
|||
|
||||
(s/def :reitit.core.coercion/status
|
||||
(s/or :number number? :default #{:default}))
|
||||
(s/def :reitit.core.coercion/schema any?)
|
||||
(s/def :reitit.core.coercion/body any?)
|
||||
(s/def :reitit.core.coercion/description string?)
|
||||
(s/def :reitit.core.coercion/response
|
||||
(s/keys :opt-un [:reitit.core.coercion/schema
|
||||
(s/keys :opt-un [:reitit.core.coercion/body
|
||||
:reitit.core.coercion/description]))
|
||||
(s/def :reitit.core.coercion/responses
|
||||
(s/map-of :reitit.core.coercion/status :reitit.core.coercion/response))
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@
|
|||
(into
|
||||
(empty responses)
|
||||
(for [[k response] responses]
|
||||
[k (update response :schema #(coercion/-compile-model this % nil))])))))
|
||||
[k (update response :body #(coercion/-compile-model this % nil))])))))
|
||||
(-compile-model [_ model name]
|
||||
(into-spec model (or name (gensym "spec"))))
|
||||
(-open-model [_ spec] spec)
|
||||
|
|
|
|||
|
|
@ -94,7 +94,7 @@
|
|||
(suite (str (if coercion (protocol/get-name coercion))))
|
||||
(let [routes ["/api"
|
||||
["/ping" {:parameters {:body {:x int?, :y int?}}
|
||||
:responses {200 {:schema {:total pos-int?}}}
|
||||
:responses {200 {:body {:total pos-int?}}}
|
||||
:get {:handler (fn [request]
|
||||
(let [{:keys [x y]} (-> request :parameters :body)]
|
||||
{:status 200
|
||||
|
|
@ -152,7 +152,7 @@
|
|||
(ring/router
|
||||
["/api"
|
||||
["/ping" {:parameters {:body {:x int?, :y int?}}
|
||||
:responses {200 {:schema {:total pos-int?}}}
|
||||
:responses {200 {:body {:total pos-int?}}}
|
||||
:get {:handler (fn [{{{:keys [x y]} :body} :parameters}]
|
||||
{:status 200
|
||||
:body {:total (+ x y)}})}}]]
|
||||
|
|
@ -199,7 +199,7 @@
|
|||
(let [m (m/create (jsonista-format/with-json-format m/default-options))
|
||||
app (ring/ring-handler
|
||||
(ring/router
|
||||
["/plus" {:post {:responses {200 {:schema {:result Long}}}
|
||||
["/plus" {:post {:responses {200 {:body {:result Long}}}
|
||||
:parameters {:body {:x Long, :y Long}}
|
||||
:handler (fn [request]
|
||||
(let [body (-> request :parameters :body)]
|
||||
|
|
@ -224,7 +224,7 @@
|
|||
(title "schema")
|
||||
(let [app (ring/ring-handler
|
||||
(ring/router
|
||||
["/plus" {:post {:responses {200 {:schema {:result Long}}}
|
||||
["/plus" {:post {:responses {200 {:body {:result Long}}}
|
||||
:parameters {:body {:x Long, :y Long}}
|
||||
:handler (fn [request]
|
||||
(let [body (-> request :parameters :body)]
|
||||
|
|
@ -248,7 +248,7 @@
|
|||
(title "data-spec")
|
||||
(let [app (ring/ring-handler
|
||||
(ring/router
|
||||
["/plus" {:post {:responses {200 {:schema {:result int?}}}
|
||||
["/plus" {:post {:responses {200 {:body {:result int?}}}
|
||||
:parameters {:body {:x int?, :y int?}}
|
||||
:handler (fn [request]
|
||||
(let [body (-> request :parameters :body)]
|
||||
|
|
@ -277,7 +277,7 @@
|
|||
(title "spec")
|
||||
(let [app (ring/ring-handler
|
||||
(ring/router
|
||||
["/plus" {:post {:responses {200 {:schema ::response}}
|
||||
["/plus" {:post {:responses {200 {:body ::response}}
|
||||
:parameters {:body ::request}
|
||||
:handler (fn [request]
|
||||
(let [body (-> request :parameters :body)]
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@
|
|||
:form {:c int?}
|
||||
:header {:d int?}
|
||||
:path {:e int?}}
|
||||
:responses {200 {:schema {:total pos-int?}}}
|
||||
:responses {200 {:body {:total pos-int?}}}
|
||||
:handler handler}}]]
|
||||
{:data {:middleware middleware
|
||||
:coercion spec/coercion}})))]
|
||||
|
|
@ -102,7 +102,7 @@
|
|||
:form {:c s/Int}
|
||||
:header {:d s/Int}
|
||||
:path {:e s/Int}}
|
||||
:responses {200 {:schema {:total (s/constrained s/Int pos? 'positive)}}}
|
||||
:responses {200 {:body {:total (s/constrained s/Int pos? 'positive)}}}
|
||||
:handler handler}}]]
|
||||
{:data {:middleware middleware
|
||||
:coercion schema/coercion}})))]
|
||||
|
|
@ -139,9 +139,9 @@
|
|||
(app valid-request))))
|
||||
|
||||
(testing "invalid request"
|
||||
(let [{:keys [status body]} (app invalid-request)]
|
||||
(let [{:keys [status]} (app invalid-request)]
|
||||
(is (= 400 status))))
|
||||
|
||||
(testing "invalid response"
|
||||
(let [{:keys [status body]} (app invalid-request2)]
|
||||
(let [{:keys [status]} (app invalid-request2)]
|
||||
(is (= 500 status))))))))))
|
||||
|
|
|
|||
|
|
@ -89,7 +89,7 @@
|
|||
:form {:c string?}
|
||||
:header {:d string?}
|
||||
:path {:e string?}}
|
||||
:responses {200 {:schema {:total pos-int?}}}
|
||||
:responses {200 {:body {:total pos-int?}}}
|
||||
:handler identity}}]]
|
||||
{:data {:middleware [rrc/coerce-exceptions-middleware
|
||||
rrc/coerce-request-middleware
|
||||
|
|
|
|||
|
|
@ -116,15 +116,15 @@
|
|||
|
||||
(is (s/valid?
|
||||
::rs/responses
|
||||
{:responses {200 {:description "ok", :schema string?}
|
||||
{:responses {200 {:description "ok", :body string?}
|
||||
400 {:description "fail"}
|
||||
500 {:schema string?}
|
||||
500 {:body string?}
|
||||
:default {}}}))
|
||||
|
||||
(is (not (s/valid?
|
||||
::rs/responses
|
||||
{:responses {"200" {:description "ok", :schema string?}}})))
|
||||
{:responses {"200" {:description "ok", :body string?}}})))
|
||||
|
||||
(is (not (s/valid?
|
||||
::rs/responses
|
||||
{:responses {200 {:description :ok, :schema string?}}}))))
|
||||
{:responses {200 {:description :ok, :body string?}}}))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue