:responses have :body, not :schema

This commit is contained in:
Tommi Reiman 2018-01-01 22:10:49 +02:00
parent 509697dc55
commit 32fb3ca294
11 changed files with 27 additions and 27 deletions

View file

@ -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)}})}}]]

View file

@ -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)

View file

@ -4,7 +4,7 @@
(def routes
["/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}]

View file

@ -5,7 +5,7 @@
(def routes
["/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}]

View file

@ -11,7 +11,7 @@
(def routes
["/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}]

View file

@ -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]

View file

@ -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))

View file

@ -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)]

View file

@ -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))))))))))

View file

@ -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

View file

@ -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?}}}))))