From 771128c28135a75c15a9602078ec5f9d475501f5 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Thu, 14 Jun 2018 13:40:32 +0300 Subject: [PATCH] Support all parameter types --- CHANGELOG.md | 17 +++++++++++ modules/reitit-core/src/reitit/coercion.cljc | 8 ++--- test/cljc/reitit/coercion_test.cljc | 31 +++++++++++++------- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86874b5e..7f223f96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,25 @@ ## `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. +```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) ### `reitit-core` diff --git a/modules/reitit-core/src/reitit/coercion.cljc b/modules/reitit-core/src/reitit/coercion.cljc index 8389c79e..4fdababa 100644 --- a/modules/reitit-core/src/reitit/coercion.cljc +++ b/modules/reitit-core/src/reitit/coercion.cljc @@ -34,7 +34,7 @@ (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) :body (->ParameterCoercion :body-params :body false false) :form (->ParameterCoercion :form-params :string true true) @@ -73,7 +73,7 @@ (defn request-coercer [coercion type model {:keys [extract-request-format] :or {extract-request-format extract-request-format-default}}] (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) model (if open? (-open-model coercion model) model) coercer (-request-coercer coercion style model)] @@ -155,5 +155,5 @@ coercers under `:result` (provided by [[compile-request-coercers]]. If coercion or parameters are not defined, return `nil`" [match] - (if-let [result (:result match)] - (coerce-request result {:path-params (:path-params match)}))) + (if-let [coercers (:result match)] + (coerce-request coercers match))) diff --git a/test/cljc/reitit/coercion_test.cljc b/test/cljc/reitit/coercion_test.cljc index ef067497..b286b286 100644 --- a/test/cljc/reitit/coercion_test.cljc +++ b/test/cljc/reitit/coercion_test.cljc @@ -1,23 +1,26 @@ (ns reitit.coercion-test (:require [clojure.test :refer [deftest testing is]] [schema.core :as s] + [spec-tools.data-spec :as ds] [reitit.core :as r] [reitit.coercion :as coercion] - [reitit.coercion.spec :as spec] - [reitit.coercion.schema :as schema]) + [reitit.coercion.spec] + [reitit.coercion.schema]) #?(:clj (:import (clojure.lang ExceptionInfo)))) -(deftest spec-coercion-test +(deftest coercion-test (let [r (r/router - [["/schema" {:coercion schema/coercion} + [["/schema" {:coercion reitit.coercion.schema/coercion} ["/:number/:keyword" {:name ::user :parameters {:path {:number s/Int - :keyword s/Keyword}}}]] - ["/spec" {:coercion spec/coercion} + :keyword s/Keyword} + :query (s/maybe {:int s/Int})}}]] + ["/spec" {:coercion reitit.coercion.spec/coercion} ["/:number/:keyword" {:name ::user :parameters {:path {:number int? - :keyword keyword?}}}]] + :keyword keyword?} + :query (ds/maybe {:int int?})}}]] ["/none" ["/:number/:keyword" {:name ::user :parameters {:path {:number int? @@ -27,8 +30,11 @@ (testing "schema-coercion" (testing "succeeds" (let [m (r/match-by-path r "/schema/1/abba")] - (is (= {:path {:keyword :abba, :number 1}} - (coercion/coerce! m))))) + (is (= {:path {:keyword :abba, :number 1}, :query nil} + (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" (let [m (r/match-by-path r "/schema/kikka/abba")] (is (thrown? ExceptionInfo (coercion/coerce! m)))))) @@ -36,8 +42,11 @@ (testing "spec-coercion" (testing "succeeds" (let [m (r/match-by-path r "/spec/1/abba")] - (is (= {:path {:keyword :abba, :number 1}} - (coercion/coerce! m))))) + (is (= {:path {:keyword :abba, :number 1}, :query nil} + (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" (let [m (r/match-by-path r "/spec/kikka/abba")] (is (thrown? ExceptionInfo (coercion/coerce! m))))))