mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +00:00
If response body is not defined, response is passed as-is
This commit is contained in:
parent
5bc7b09e19
commit
f6c460d05c
3 changed files with 23 additions and 11 deletions
|
|
@ -3,6 +3,7 @@
|
||||||
## `reitit-core`
|
## `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.
|
* `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.
|
* `spec-tools.data-spec/maybe` can be used in spec-coercion.
|
||||||
|
|
||||||
```clj
|
```clj
|
||||||
|
|
|
||||||
|
|
@ -116,7 +116,8 @@
|
||||||
(defn coerce-response [coercers request response]
|
(defn coerce-response [coercers request response]
|
||||||
(if response
|
(if response
|
||||||
(if-let [coercer (or (coercers (:status response)) (coercers :default))]
|
(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]
|
(defn request-coercers [coercion parameters opts]
|
||||||
(->> (for [[k v] parameters
|
(->> (for [[k v] parameters
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,8 @@
|
||||||
[reitit.ring.coercion :as rrc]
|
[reitit.ring.coercion :as rrc]
|
||||||
[reitit.coercion.spec :as spec]
|
[reitit.coercion.spec :as spec]
|
||||||
[reitit.coercion.schema :as schema]
|
[reitit.coercion.schema :as schema]
|
||||||
#?@(:clj [
|
#?@(:clj [[muuntaja.middleware]
|
||||||
[muuntaja.middleware]
|
[jsonista.core :as j]]))
|
||||||
[jsonista.core :as j]]))
|
|
||||||
#?(:clj
|
#?(:clj
|
||||||
(:import (clojure.lang ExceptionInfo)
|
(:import (clojure.lang ExceptionInfo)
|
||||||
(java.io ByteArrayInputStream))))
|
(java.io ByteArrayInputStream))))
|
||||||
|
|
@ -17,8 +16,11 @@
|
||||||
{:keys [c]} :form
|
{:keys [c]} :form
|
||||||
{:keys [d]} :header
|
{:keys [d]} :header
|
||||||
{:keys [e]} :path} :parameters}]
|
{:keys [e]} :path} :parameters}]
|
||||||
{:status 200
|
(if (= 666 a)
|
||||||
:body {:total (+ a b c d e)}})
|
{:status 500
|
||||||
|
:body {:evil true}}
|
||||||
|
{:status 200
|
||||||
|
:body {:total (+ a b c d e)}}))
|
||||||
|
|
||||||
(def valid-request
|
(def valid-request
|
||||||
{:uri "/api/plus/5"
|
{:uri "/api/plus/5"
|
||||||
|
|
@ -51,19 +53,23 @@
|
||||||
:form {:c int?}
|
:form {:c int?}
|
||||||
:header {:d int?}
|
:header {:d int?}
|
||||||
:path {:e int?}}
|
:path {:e int?}}
|
||||||
:responses {200 {:body {:total pos-int?}}}
|
:responses {200 {:body {:total pos-int?}}
|
||||||
|
500 {:description "fail"}}
|
||||||
:handler handler}}]]
|
:handler handler}}]]
|
||||||
{:data {:middleware middleware
|
{:data {:middleware middleware
|
||||||
:coercion spec/coercion}})))]
|
:coercion spec/coercion}})))]
|
||||||
|
|
||||||
(testing "withut exception handling"
|
(testing "without exception handling"
|
||||||
(let [app (create [rrc/coerce-request-middleware
|
(let [app (create [rrc/coerce-request-middleware
|
||||||
rrc/coerce-response-middleware])]
|
rrc/coerce-response-middleware])]
|
||||||
|
|
||||||
(testing "all good"
|
(testing "all good"
|
||||||
(is (= {:status 200
|
(is (= {:status 200
|
||||||
:body {:total 15}}
|
: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"
|
(testing "invalid request"
|
||||||
(is (thrown-with-msg?
|
(is (thrown-with-msg?
|
||||||
|
|
@ -106,7 +112,8 @@
|
||||||
: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 {:body {:total (s/constrained s/Int pos? 'positive)}}}
|
:responses {200 {:body {:total (s/constrained s/Int pos? 'positive)}}
|
||||||
|
500 {:description "fail"}}
|
||||||
:handler handler}}]]
|
:handler handler}}]]
|
||||||
{:data {:middleware middleware
|
{:data {:middleware middleware
|
||||||
:coercion schema/coercion}})))]
|
:coercion schema/coercion}})))]
|
||||||
|
|
@ -118,7 +125,10 @@
|
||||||
(testing "all good"
|
(testing "all good"
|
||||||
(is (= {:status 200
|
(is (= {:status 200
|
||||||
:body {:total 15}}
|
: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"
|
(testing "invalid request"
|
||||||
(is (thrown-with-msg?
|
(is (thrown-with-msg?
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue