Skip to content

Commit d5d46d5

Browse files
authored
Merge pull request #715 from filipesilva/coerce-response-int
fix: throw if response status is not int
2 parents 30fd739 + f0fc440 commit d5d46d5

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

modules/reitit-core/src/reitit/coercion.cljc

+4-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@
183183

184184
(defn response-coercers [coercion responses opts]
185185
(some->> (for [[status model] responses]
186-
[status (response-coercer coercion model opts)])
186+
(do
187+
(when-not (int? status)
188+
(throw (ex-info "Response status must be int" {:status status})))
189+
[status (response-coercer coercion model opts)]))
187190
(filter second) (seq) (into {})))
188191

189192
(defn -compile-parameters [data coercion]

test/clj/reitit/ring/middleware/exception_test.clj

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
[reitit.ring.coercion]
99
[reitit.ring.middleware.exception :as exception]
1010
[ring.util.http-response :as http-response])
11-
(:import (java.sql SQLException SQLWarning)))
11+
(:import (clojure.lang ExceptionInfo)
12+
(java.sql SQLException SQLWarning)))
1213

1314
(derive ::kikka ::kukka)
1415

@@ -190,3 +191,15 @@
190191
(is (contains? problems ::s/spec))
191192
(is (contains? problems ::s/value))
192193
(is (contains? problems ::s/problems))))))))
194+
195+
(deftest response-keys-test
196+
(is (thrown-with-msg?
197+
ExceptionInfo
198+
#"Response status must be int"
199+
(ring/ring-handler
200+
(ring/router
201+
[["/coercion"
202+
{:middleware [reitit.ring.coercion/coerce-response-middleware]
203+
:coercion reitit.coercion.spec/coercion
204+
:responses {:200 {:body {:total pos-int?}}}
205+
:handler identity}]])))))

test/cljc/reitit/ring_spec_test.cljc

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
(ring/router
126126
["/api"
127127
["/plus/:e"
128-
{:get {:responses {"200" {}}
128+
{:get {:responses {200 {:description 1}}
129129
:handler identity}}]]
130130
{:data {:middleware [rrc/coerce-exceptions-middleware
131131
rrc/coerce-request-middleware

0 commit comments

Comments
 (0)