If response body is not defined, response is passed as-is

This commit is contained in:
Tommi Reiman 2018-06-24 18:36:55 +03:00
parent 5bc7b09e19
commit f6c460d05c
3 changed files with 23 additions and 11 deletions

View file

@ -3,6 +3,7 @@
## `reitit-core`
* `reitit.coercion/coerce!` coerced all parameters found in match, e.g. injecting in `:query-parameters` into `Match` with coerce those too if `:query` coercion is defined.
* if response coercion is not defined for a response status, response is still returned
* `spec-tools.data-spec/maybe` can be used in spec-coercion.
```clj

View file

@ -116,7 +116,8 @@
(defn coerce-response [coercers request response]
(if response
(if-let [coercer (or (coercers (:status response)) (coercers :default))]
(impl/fast-assoc response :body (coercer request response)))))
(impl/fast-assoc response :body (coercer request response))
response)))
(defn request-coercers [coercion parameters opts]
(->> (for [[k v] parameters

View file

@ -5,9 +5,8 @@
[reitit.ring.coercion :as rrc]
[reitit.coercion.spec :as spec]
[reitit.coercion.schema :as schema]
#?@(:clj [
[muuntaja.middleware]
[jsonista.core :as j]]))
#?@(:clj [[muuntaja.middleware]
[jsonista.core :as j]]))
#?(:clj
(:import (clojure.lang ExceptionInfo)
(java.io ByteArrayInputStream))))
@ -17,8 +16,11 @@
{:keys [c]} :form
{:keys [d]} :header
{:keys [e]} :path} :parameters}]
{:status 200
:body {:total (+ a b c d e)}})
(if (= 666 a)
{:status 500
:body {:evil true}}
{:status 200
:body {:total (+ a b c d e)}}))
(def valid-request
{:uri "/api/plus/5"
@ -51,19 +53,23 @@
:form {:c int?}
:header {:d int?}
:path {:e int?}}
:responses {200 {:body {:total pos-int?}}}
:responses {200 {:body {:total pos-int?}}
500 {:description "fail"}}
:handler handler}}]]
{:data {:middleware middleware
:coercion spec/coercion}})))]
(testing "withut exception handling"
(testing "without exception handling"
(let [app (create [rrc/coerce-request-middleware
rrc/coerce-response-middleware])]
(testing "all good"
(is (= {:status 200
:body {:total 15}}
(app valid-request))))
(app valid-request)))
(is (= {:status 500
:body {:evil true}}
(app (assoc-in valid-request [:query-params "a"] "666")))))
(testing "invalid request"
(is (thrown-with-msg?
@ -106,7 +112,8 @@
:form {:c s/Int}
:header {:d s/Int}
:path {:e s/Int}}
:responses {200 {:body {:total (s/constrained s/Int pos? 'positive)}}}
:responses {200 {:body {:total (s/constrained s/Int pos? 'positive)}}
500 {:description "fail"}}
:handler handler}}]]
{:data {:middleware middleware
:coercion schema/coercion}})))]
@ -118,7 +125,10 @@
(testing "all good"
(is (= {:status 200
:body {:total 15}}
(app valid-request))))
(app valid-request)))
(is (= {:status 500
:body {:evil true}}
(app (assoc-in valid-request [:query-params "a"] "666")))))
(testing "invalid request"
(is (thrown-with-msg?