mirror of
https://github.com/metosin/reitit.git
synced 2025-12-19 09:21:10 +00:00
:responses have :body, not :schema
This commit is contained in:
parent
509697dc55
commit
32fb3ca294
11 changed files with 27 additions and 27 deletions
|
|
@ -74,7 +74,7 @@ A Ring routing app with input & output coercion using [data-specs](https://githu
|
||||||
(ring/router
|
(ring/router
|
||||||
["/api"
|
["/api"
|
||||||
["/math" {:get {:parameters {:query {:x int?, :y int?}}
|
["/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}]
|
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||||
{:status 200
|
{:status 200
|
||||||
:body {:total (+ x y)}})}}]]
|
: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}
|
:parameters {:query {:x s/Int}
|
||||||
:body {:y s/Int}
|
:body {:y s/Int}
|
||||||
:path {:z s/Int}}
|
:path {:z s/Int}}
|
||||||
:responses {200 {:schema {:total PositiveInt}}}
|
:responses {200 {:body {:total PositiveInt}}}
|
||||||
:handler (fn [{:keys [parameters]}]
|
:handler (fn [{:keys [parameters]}]
|
||||||
(let [total (+ (-> parameters :query :x)
|
(let [total (+ (-> parameters :query :x)
|
||||||
(-> parameters :body :y)
|
(-> parameters :body :y)
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
(def routes
|
(def routes
|
||||||
["/dspec" {:coercion reitit.coercion.spec/coercion}
|
["/dspec" {:coercion reitit.coercion.spec/coercion}
|
||||||
["/plus" {:name ::plus
|
["/plus" {:name ::plus
|
||||||
:responses {200 {:schema {:total int?}}}
|
:responses {200 {:body {:total int?}}}
|
||||||
:get {:summary "plus with query-params"
|
:get {:summary "plus with query-params"
|
||||||
:parameters {:query {:x int?, :y int?}}
|
:parameters {:query {:x int?, :y int?}}
|
||||||
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
(def routes
|
(def routes
|
||||||
["/schema" {:coercion reitit.coercion.schema/coercion}
|
["/schema" {:coercion reitit.coercion.schema/coercion}
|
||||||
["/plus" {:name ::plus
|
["/plus" {:name ::plus
|
||||||
:responses {200 {:schema {:total s/Int}}}
|
:responses {200 {:body {:total s/Int}}}
|
||||||
:get {:summary "plus with query-params"
|
:get {:summary "plus with query-params"
|
||||||
:parameters {:query {:x s/Int, :y s/Int}}
|
:parameters {:query {:x s/Int, :y s/Int}}
|
||||||
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
(def routes
|
(def routes
|
||||||
["/spec" {:coercion reitit.coercion.spec/coercion}
|
["/spec" {:coercion reitit.coercion.spec/coercion}
|
||||||
["/plus" {:name ::plus
|
["/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"
|
:get {:summary "plus with query-params"
|
||||||
:parameters {:query (s/keys :req-un [::x ::y])}
|
:parameters {:query (s/keys :req-un [::x ::y])}
|
||||||
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||||
|
|
|
||||||
|
|
@ -89,10 +89,10 @@
|
||||||
(request-coercion-failed! result coercion value in request)
|
(request-coercion-failed! result coercion value in request)
|
||||||
result))))))
|
result))))))
|
||||||
|
|
||||||
(defn response-coercer [coercion model {:keys [extract-response-format]
|
(defn response-coercer [coercion body {:keys [extract-response-format]
|
||||||
:or {extract-response-format (constantly nil)}}]
|
:or {extract-response-format (constantly nil)}}]
|
||||||
(if coercion
|
(if coercion
|
||||||
(let [coercer (-response-coercer coercion model)]
|
(let [coercer (-response-coercer coercion body)]
|
||||||
(fn [request response]
|
(fn [request response]
|
||||||
(let [format (extract-response-format request response)
|
(let [format (extract-response-format request response)
|
||||||
value (:body response)
|
value (:body response)
|
||||||
|
|
@ -126,8 +126,8 @@
|
||||||
(into {})))
|
(into {})))
|
||||||
|
|
||||||
(defn response-coercers [coercion responses opts]
|
(defn response-coercers [coercion responses opts]
|
||||||
(->> (for [[status {:keys [schema]}] responses :when schema]
|
(->> (for [[status {:keys [body]}] responses :when body]
|
||||||
[status (response-coercer coercion schema opts)])
|
[status (response-coercer coercion body opts)])
|
||||||
(into {})))
|
(into {})))
|
||||||
|
|
||||||
(defn- coercers-not-compiled! [match]
|
(defn- coercers-not-compiled! [match]
|
||||||
|
|
|
||||||
|
|
@ -92,10 +92,10 @@
|
||||||
|
|
||||||
(s/def :reitit.core.coercion/status
|
(s/def :reitit.core.coercion/status
|
||||||
(s/or :number number? :default #{:default}))
|
(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/description string?)
|
||||||
(s/def :reitit.core.coercion/response
|
(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]))
|
:reitit.core.coercion/description]))
|
||||||
(s/def :reitit.core.coercion/responses
|
(s/def :reitit.core.coercion/responses
|
||||||
(s/map-of :reitit.core.coercion/status :reitit.core.coercion/response))
|
(s/map-of :reitit.core.coercion/status :reitit.core.coercion/response))
|
||||||
|
|
|
||||||
|
|
@ -94,7 +94,7 @@
|
||||||
(suite (str (if coercion (protocol/get-name coercion))))
|
(suite (str (if coercion (protocol/get-name coercion))))
|
||||||
(let [routes ["/api"
|
(let [routes ["/api"
|
||||||
["/ping" {:parameters {:body {:x int?, :y int?}}
|
["/ping" {:parameters {:body {:x int?, :y int?}}
|
||||||
:responses {200 {:schema {:total pos-int?}}}
|
:responses {200 {:body {:total pos-int?}}}
|
||||||
:get {:handler (fn [request]
|
:get {:handler (fn [request]
|
||||||
(let [{:keys [x y]} (-> request :parameters :body)]
|
(let [{:keys [x y]} (-> request :parameters :body)]
|
||||||
{:status 200
|
{:status 200
|
||||||
|
|
@ -152,7 +152,7 @@
|
||||||
(ring/router
|
(ring/router
|
||||||
["/api"
|
["/api"
|
||||||
["/ping" {:parameters {:body {:x int?, :y int?}}
|
["/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}]
|
:get {:handler (fn [{{{:keys [x y]} :body} :parameters}]
|
||||||
{:status 200
|
{:status 200
|
||||||
:body {:total (+ x y)}})}}]]
|
:body {:total (+ x y)}})}}]]
|
||||||
|
|
@ -199,7 +199,7 @@
|
||||||
(let [m (m/create (jsonista-format/with-json-format m/default-options))
|
(let [m (m/create (jsonista-format/with-json-format m/default-options))
|
||||||
app (ring/ring-handler
|
app (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
["/plus" {:post {:responses {200 {:schema {:result Long}}}
|
["/plus" {:post {:responses {200 {:body {:result Long}}}
|
||||||
:parameters {:body {:x Long, :y Long}}
|
:parameters {:body {:x Long, :y Long}}
|
||||||
:handler (fn [request]
|
:handler (fn [request]
|
||||||
(let [body (-> request :parameters :body)]
|
(let [body (-> request :parameters :body)]
|
||||||
|
|
@ -224,7 +224,7 @@
|
||||||
(title "schema")
|
(title "schema")
|
||||||
(let [app (ring/ring-handler
|
(let [app (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
["/plus" {:post {:responses {200 {:schema {:result Long}}}
|
["/plus" {:post {:responses {200 {:body {:result Long}}}
|
||||||
:parameters {:body {:x Long, :y Long}}
|
:parameters {:body {:x Long, :y Long}}
|
||||||
:handler (fn [request]
|
:handler (fn [request]
|
||||||
(let [body (-> request :parameters :body)]
|
(let [body (-> request :parameters :body)]
|
||||||
|
|
@ -248,7 +248,7 @@
|
||||||
(title "data-spec")
|
(title "data-spec")
|
||||||
(let [app (ring/ring-handler
|
(let [app (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
["/plus" {:post {:responses {200 {:schema {:result int?}}}
|
["/plus" {:post {:responses {200 {:body {:result int?}}}
|
||||||
:parameters {:body {:x int?, :y int?}}
|
:parameters {:body {:x int?, :y int?}}
|
||||||
:handler (fn [request]
|
:handler (fn [request]
|
||||||
(let [body (-> request :parameters :body)]
|
(let [body (-> request :parameters :body)]
|
||||||
|
|
@ -277,7 +277,7 @@
|
||||||
(title "spec")
|
(title "spec")
|
||||||
(let [app (ring/ring-handler
|
(let [app (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
["/plus" {:post {:responses {200 {:schema ::response}}
|
["/plus" {:post {:responses {200 {:body ::response}}
|
||||||
:parameters {:body ::request}
|
:parameters {:body ::request}
|
||||||
:handler (fn [request]
|
:handler (fn [request]
|
||||||
(let [body (-> request :parameters :body)]
|
(let [body (-> request :parameters :body)]
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@
|
||||||
:form {:c int?}
|
:form {:c int?}
|
||||||
:header {:d int?}
|
:header {:d int?}
|
||||||
:path {:e int?}}
|
:path {:e int?}}
|
||||||
:responses {200 {:schema {:total pos-int?}}}
|
:responses {200 {:body {:total pos-int?}}}
|
||||||
:handler handler}}]]
|
:handler handler}}]]
|
||||||
{:data {:middleware middleware
|
{:data {:middleware middleware
|
||||||
:coercion spec/coercion}})))]
|
:coercion spec/coercion}})))]
|
||||||
|
|
@ -102,7 +102,7 @@
|
||||||
:form {:c s/Int}
|
:form {:c s/Int}
|
||||||
:header {:d s/Int}
|
:header {:d s/Int}
|
||||||
:path {:e 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}}]]
|
:handler handler}}]]
|
||||||
{:data {:middleware middleware
|
{:data {:middleware middleware
|
||||||
:coercion schema/coercion}})))]
|
:coercion schema/coercion}})))]
|
||||||
|
|
@ -139,9 +139,9 @@
|
||||||
(app valid-request))))
|
(app valid-request))))
|
||||||
|
|
||||||
(testing "invalid request"
|
(testing "invalid request"
|
||||||
(let [{:keys [status body]} (app invalid-request)]
|
(let [{:keys [status]} (app invalid-request)]
|
||||||
(is (= 400 status))))
|
(is (= 400 status))))
|
||||||
|
|
||||||
(testing "invalid response"
|
(testing "invalid response"
|
||||||
(let [{:keys [status body]} (app invalid-request2)]
|
(let [{:keys [status]} (app invalid-request2)]
|
||||||
(is (= 500 status))))))))))
|
(is (= 500 status))))))))))
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@
|
||||||
:form {:c string?}
|
:form {:c string?}
|
||||||
:header {:d string?}
|
:header {:d string?}
|
||||||
:path {:e string?}}
|
:path {:e string?}}
|
||||||
:responses {200 {:schema {:total pos-int?}}}
|
:responses {200 {:body {:total pos-int?}}}
|
||||||
:handler identity}}]]
|
:handler identity}}]]
|
||||||
{:data {:middleware [rrc/coerce-exceptions-middleware
|
{:data {:middleware [rrc/coerce-exceptions-middleware
|
||||||
rrc/coerce-request-middleware
|
rrc/coerce-request-middleware
|
||||||
|
|
|
||||||
|
|
@ -116,15 +116,15 @@
|
||||||
|
|
||||||
(is (s/valid?
|
(is (s/valid?
|
||||||
::rs/responses
|
::rs/responses
|
||||||
{:responses {200 {:description "ok", :schema string?}
|
{:responses {200 {:description "ok", :body string?}
|
||||||
400 {:description "fail"}
|
400 {:description "fail"}
|
||||||
500 {:schema string?}
|
500 {:body string?}
|
||||||
:default {}}}))
|
:default {}}}))
|
||||||
|
|
||||||
(is (not (s/valid?
|
(is (not (s/valid?
|
||||||
::rs/responses
|
::rs/responses
|
||||||
{:responses {"200" {:description "ok", :schema string?}}})))
|
{:responses {"200" {:description "ok", :body string?}}})))
|
||||||
|
|
||||||
(is (not (s/valid?
|
(is (not (s/valid?
|
||||||
::rs/responses
|
::rs/responses
|
||||||
{:responses {200 {:description :ok, :schema string?}}}))))
|
{:responses {200 {:description :ok, :body string?}}}))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue