diff --git a/CHANGELOG.md b/CHANGELOG.md index 5aa591cd..0245ce3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/modules/reitit-core/src/reitit/coercion.cljc b/modules/reitit-core/src/reitit/coercion.cljc index 4fdababa..91a02231 100644 --- a/modules/reitit-core/src/reitit/coercion.cljc +++ b/modules/reitit-core/src/reitit/coercion.cljc @@ -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 diff --git a/test/cljc/reitit/ring_coercion_test.cljc b/test/cljc/reitit/ring_coercion_test.cljc index ea767457..d212a150 100644 --- a/test/cljc/reitit/ring_coercion_test.cljc +++ b/test/cljc/reitit/ring_coercion_test.cljc @@ -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?