From f0fc4404251b5331b108a88215bd8ae2137056af Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Sat, 18 Jan 2025 15:24:47 +0000 Subject: [PATCH] fix: throw if response status is not int Fix #667 --- modules/reitit-core/src/reitit/coercion.cljc | 5 ++++- .../clj/reitit/ring/middleware/exception_test.clj | 15 ++++++++++++++- test/cljc/reitit/ring_spec_test.cljc | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/modules/reitit-core/src/reitit/coercion.cljc b/modules/reitit-core/src/reitit/coercion.cljc index bc3dc6ca..377258d5 100644 --- a/modules/reitit-core/src/reitit/coercion.cljc +++ b/modules/reitit-core/src/reitit/coercion.cljc @@ -181,7 +181,10 @@ (defn response-coercers [coercion responses opts] (some->> (for [[status model] responses] - [status (response-coercer coercion model opts)]) + (do + (when-not (int? status) + (throw (ex-info "Response status must be int" {:status status}))) + [status (response-coercer coercion model opts)])) (filter second) (seq) (into {}))) (defn -compile-parameters [data coercion] diff --git a/test/clj/reitit/ring/middleware/exception_test.clj b/test/clj/reitit/ring/middleware/exception_test.clj index 71b11b58..fc4947e0 100644 --- a/test/clj/reitit/ring/middleware/exception_test.clj +++ b/test/clj/reitit/ring/middleware/exception_test.clj @@ -8,7 +8,8 @@ [reitit.ring.coercion] [reitit.ring.middleware.exception :as exception] [ring.util.http-response :as http-response]) - (:import (java.sql SQLException SQLWarning))) + (:import (clojure.lang ExceptionInfo) + (java.sql SQLException SQLWarning))) (derive ::kikka ::kukka) @@ -190,3 +191,15 @@ (is (contains? problems ::s/spec)) (is (contains? problems ::s/value)) (is (contains? problems ::s/problems)))))))) + +(deftest response-keys-test + (is (thrown-with-msg? + ExceptionInfo + #"Response status must be int" + (ring/ring-handler + (ring/router + [["/coercion" + {:middleware [reitit.ring.coercion/coerce-response-middleware] + :coercion reitit.coercion.spec/coercion + :responses {:200 {:body {:total pos-int?}}} + :handler identity}]]))))) diff --git a/test/cljc/reitit/ring_spec_test.cljc b/test/cljc/reitit/ring_spec_test.cljc index 5d4e4ee7..2e1b1648 100644 --- a/test/cljc/reitit/ring_spec_test.cljc +++ b/test/cljc/reitit/ring_spec_test.cljc @@ -125,7 +125,7 @@ (ring/router ["/api" ["/plus/:e" - {:get {:responses {"200" {}} + {:get {:responses {200 {:description 1}} :handler identity}}]] {:data {:middleware [rrc/coerce-exceptions-middleware rrc/coerce-request-middleware