Support all parameter types

This commit is contained in:
Tommi Reiman 2018-06-14 13:40:32 +03:00
parent 8f7bb94c94
commit 771128c281
3 changed files with 41 additions and 15 deletions

View file

@ -2,8 +2,25 @@
## `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.
* `spec-tools.data-spec/maybe` can be used in spec-coercion. * `spec-tools.data-spec/maybe` can be used in spec-coercion.
```clj
(def router
(reitit.core/router
["/spec" {:coercion reitit.coercion.spec/coercion}
["/:number/:keyword" {:parameters {:path {:number int?
:keyword keyword?}
:query (ds/maybe {:int int?})}}]]
{:compile reitit.coercion/compile-request-coercers}))
(-> (reitit.core/match-by-path router "/spec/10/kikka")
(assoc :query-params {:int "10"})
(reitit.coercion/coerce!))
; {:path {:number 10, :keyword :kikka}
; :query {:int 10}}
```
## 0.1.2 (2018-6-6) ## 0.1.2 (2018-6-6)
### `reitit-core` ### `reitit-core`

View file

@ -34,7 +34,7 @@
(defrecord ParameterCoercion [in style keywordize? open?]) (defrecord ParameterCoercion [in style keywordize? open?])
(def ^:no-doc ring-parameter-coercion (def ^:no-doc defaut-parameter-coercion
{:query (->ParameterCoercion :query-params :string true true) {:query (->ParameterCoercion :query-params :string true true)
:body (->ParameterCoercion :body-params :body false false) :body (->ParameterCoercion :body-params :body false false)
:form (->ParameterCoercion :form-params :string true true) :form (->ParameterCoercion :form-params :string true true)
@ -73,7 +73,7 @@
(defn request-coercer [coercion type model {:keys [extract-request-format] (defn request-coercer [coercion type model {:keys [extract-request-format]
:or {extract-request-format extract-request-format-default}}] :or {extract-request-format extract-request-format-default}}]
(if coercion (if coercion
(let [{:keys [keywordize? open? in style]} (ring-parameter-coercion type) (let [{:keys [keywordize? open? in style]} (defaut-parameter-coercion type)
transform (comp (if keywordize? walk/keywordize-keys identity) in) transform (comp (if keywordize? walk/keywordize-keys identity) in)
model (if open? (-open-model coercion model) model) model (if open? (-open-model coercion model) model)
coercer (-request-coercer coercion style model)] coercer (-request-coercer coercion style model)]
@ -155,5 +155,5 @@
coercers under `:result` (provided by [[compile-request-coercers]]. coercers under `:result` (provided by [[compile-request-coercers]].
If coercion or parameters are not defined, return `nil`" If coercion or parameters are not defined, return `nil`"
[match] [match]
(if-let [result (:result match)] (if-let [coercers (:result match)]
(coerce-request result {:path-params (:path-params match)}))) (coerce-request coercers match)))

View file

@ -1,23 +1,26 @@
(ns reitit.coercion-test (ns reitit.coercion-test
(:require [clojure.test :refer [deftest testing is]] (:require [clojure.test :refer [deftest testing is]]
[schema.core :as s] [schema.core :as s]
[spec-tools.data-spec :as ds]
[reitit.core :as r] [reitit.core :as r]
[reitit.coercion :as coercion] [reitit.coercion :as coercion]
[reitit.coercion.spec :as spec] [reitit.coercion.spec]
[reitit.coercion.schema :as schema]) [reitit.coercion.schema])
#?(:clj #?(:clj
(:import (clojure.lang ExceptionInfo)))) (:import (clojure.lang ExceptionInfo))))
(deftest spec-coercion-test (deftest coercion-test
(let [r (r/router (let [r (r/router
[["/schema" {:coercion schema/coercion} [["/schema" {:coercion reitit.coercion.schema/coercion}
["/:number/:keyword" {:name ::user ["/:number/:keyword" {:name ::user
:parameters {:path {:number s/Int :parameters {:path {:number s/Int
:keyword s/Keyword}}}]] :keyword s/Keyword}
["/spec" {:coercion spec/coercion} :query (s/maybe {:int s/Int})}}]]
["/spec" {:coercion reitit.coercion.spec/coercion}
["/:number/:keyword" {:name ::user ["/:number/:keyword" {:name ::user
:parameters {:path {:number int? :parameters {:path {:number int?
:keyword keyword?}}}]] :keyword keyword?}
:query (ds/maybe {:int int?})}}]]
["/none" ["/none"
["/:number/:keyword" {:name ::user ["/:number/:keyword" {:name ::user
:parameters {:path {:number int? :parameters {:path {:number int?
@ -27,8 +30,11 @@
(testing "schema-coercion" (testing "schema-coercion"
(testing "succeeds" (testing "succeeds"
(let [m (r/match-by-path r "/schema/1/abba")] (let [m (r/match-by-path r "/schema/1/abba")]
(is (= {:path {:keyword :abba, :number 1}} (is (= {:path {:keyword :abba, :number 1}, :query nil}
(coercion/coerce! m))))) (coercion/coerce! m))))
(let [m (r/match-by-path r "/schema/1/abba")]
(is (= {:path {:keyword :abba, :number 1}, :query {:int 10}}
(coercion/coerce! (assoc m :query-params {:int "10"}))))))
(testing "throws with invalid input" (testing "throws with invalid input"
(let [m (r/match-by-path r "/schema/kikka/abba")] (let [m (r/match-by-path r "/schema/kikka/abba")]
(is (thrown? ExceptionInfo (coercion/coerce! m)))))) (is (thrown? ExceptionInfo (coercion/coerce! m))))))
@ -36,8 +42,11 @@
(testing "spec-coercion" (testing "spec-coercion"
(testing "succeeds" (testing "succeeds"
(let [m (r/match-by-path r "/spec/1/abba")] (let [m (r/match-by-path r "/spec/1/abba")]
(is (= {:path {:keyword :abba, :number 1}} (is (= {:path {:keyword :abba, :number 1}, :query nil}
(coercion/coerce! m))))) (coercion/coerce! m))))
(let [m (r/match-by-path r "/schema/1/abba")]
(is (= {:path {:keyword :abba, :number 1}, :query {:int 10}}
(coercion/coerce! (assoc m :query-params {:int "10"}))))))
(testing "throws with invalid input" (testing "throws with invalid input"
(let [m (r/match-by-path r "/spec/kikka/abba")] (let [m (r/match-by-path r "/spec/kikka/abba")]
(is (thrown? ExceptionInfo (coercion/coerce! m)))))) (is (thrown? ExceptionInfo (coercion/coerce! m))))))