mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +00:00
Use defined :string :default transformer for query-string-coercer
This commit is contained in:
parent
4eb29d3ed9
commit
5ca22193d0
2 changed files with 35 additions and 12 deletions
|
|
@ -9,7 +9,8 @@
|
||||||
[malli.swagger :as swagger]
|
[malli.swagger :as swagger]
|
||||||
[malli.transform :as mt]
|
[malli.transform :as mt]
|
||||||
[malli.util :as mu]
|
[malli.util :as mu]
|
||||||
[reitit.coercion :as coercion]))
|
[reitit.coercion :as coercion]
|
||||||
|
[clojure.string :as string]))
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; coercion
|
;; coercion
|
||||||
|
|
@ -79,11 +80,13 @@
|
||||||
(defn- -query-string-coercer
|
(defn- -query-string-coercer
|
||||||
"Create coercer for query-parameters, always allows extra params and does
|
"Create coercer for query-parameters, always allows extra params and does
|
||||||
encoding using string-transformer."
|
encoding using string-transformer."
|
||||||
[schema transfomers options]
|
[schema string-transformer-provider options]
|
||||||
(let [;; Always allow extra paramaters on query-parameters encoding
|
(let [;; Always allow extra paramaters on query-parameters encoding
|
||||||
open-schema (mu/open-schema schema)
|
open-schema (mu/open-schema schema)
|
||||||
;; Do not remove extra keys
|
;; Do not remove extra keys
|
||||||
string-transformer (-transformer string-transformer-provider (assoc options :strip-extra-keys false))
|
string-transformer (if (satisfies? TransformationProvider string-transformer-provider)
|
||||||
|
(-transformer string-transformer-provider (assoc options :strip-extra-keys false))
|
||||||
|
string-transformer-provider)
|
||||||
encoder (m/encoder open-schema options string-transformer)]
|
encoder (m/encoder open-schema options string-transformer)]
|
||||||
(fn [value format]
|
(fn [value format]
|
||||||
(if encoder
|
(if encoder
|
||||||
|
|
@ -126,6 +129,9 @@
|
||||||
([opts]
|
([opts]
|
||||||
(let [{:keys [transformers lite compile options error-keys encode-error] :as opts} (merge default-options opts)
|
(let [{:keys [transformers lite compile options error-keys encode-error] :as opts} (merge default-options opts)
|
||||||
show? (fn [key] (contains? error-keys key))
|
show? (fn [key] (contains? error-keys key))
|
||||||
|
;; Query-string-coercer needs to construct transfomer without strip-extra-keys so it will
|
||||||
|
;; use the transformer-provider directly.
|
||||||
|
string-transformer-provider (:default (:string transformers))
|
||||||
transformers (walk/prewalk #(if (satisfies? TransformationProvider %) (-transformer % opts) %) transformers)
|
transformers (walk/prewalk #(if (satisfies? TransformationProvider %) (-transformer % opts) %) transformers)
|
||||||
compile (if lite (fn [schema options]
|
compile (if lite (fn [schema options]
|
||||||
(compile (binding [l/*options* options] (l/schema schema)) options))
|
(compile (binding [l/*options* options] (l/schema schema)) options))
|
||||||
|
|
@ -192,6 +198,6 @@
|
||||||
(-response-coercer [_ schema]
|
(-response-coercer [_ schema]
|
||||||
(-coercer schema :response transformers :encode opts))
|
(-coercer schema :response transformers :encode opts))
|
||||||
(-query-string-coercer [_ schema]
|
(-query-string-coercer [_ schema]
|
||||||
(-query-string-coercer schema transformers opts))))))
|
(-query-string-coercer schema string-transformer-provider opts))))))
|
||||||
|
|
||||||
(def coercion (create default-options))
|
(def coercion (create default-options))
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
(ns reitit.frontend.core-test
|
(ns reitit.frontend.core-test
|
||||||
(:require [clojure.string :as str]
|
(:require [clojure.string :as str]
|
||||||
[clojure.test :refer [are deftest is testing]]
|
[clojure.test :refer [are deftest is testing]]
|
||||||
|
[malli.core :as m]
|
||||||
|
[malli.transform :as mt]
|
||||||
[reitit.coercion :as rc]
|
[reitit.coercion :as rc]
|
||||||
[reitit.coercion.malli :as rcm]
|
[reitit.coercion.malli :as rcm]
|
||||||
[reitit.coercion.schema :as rcs]
|
[reitit.coercion.schema :as rcs]
|
||||||
|
|
@ -305,11 +307,26 @@
|
||||||
(rf/match->path {:path "foo"} {:q :x})
|
(rf/match->path {:path "foo"} {:q :x})
|
||||||
"foo?q=x")))
|
"foo?q=x")))
|
||||||
|
|
||||||
(is (= "foo?q=__x"
|
(testing "default string transformer"
|
||||||
(rf/match->path {:data {:coercion rcm/coercion
|
(is (= "foo?q=__x"
|
||||||
:parameters {:query [[:map
|
(rf/match->path {:data {:coercion rcm/coercion
|
||||||
[:q {:decode/string (fn [s] (keyword (subs s 2)))
|
:parameters {:query [[:map
|
||||||
:encode/string (fn [k] (str "__" (name k)))}
|
[:q {:decode/string (fn [s] (keyword (subs s 2)))
|
||||||
:keyword]]]}}
|
:encode/string (fn [k] (str "__" (name k)))}
|
||||||
:path "foo"}
|
:keyword]]]}}
|
||||||
{:q "x"}))))
|
:path "foo"}
|
||||||
|
{:q "x"}))))
|
||||||
|
|
||||||
|
(testing "custom string transformer"
|
||||||
|
(is (= "foo?q=--x"
|
||||||
|
(rf/match->path {:data {:coercion (rcm/create (assoc-in rcm/default-options
|
||||||
|
[:transformers :string :default]
|
||||||
|
(mt/transformer
|
||||||
|
{:name :foo-string
|
||||||
|
:encoders {:foo/type {:leave (fn [x] (str "--" x))}}})))
|
||||||
|
:parameters {:query [[:map
|
||||||
|
[:q (m/-simple-schema
|
||||||
|
{:type :foo/type
|
||||||
|
:pred string?})]]]}}
|
||||||
|
:path "foo"}
|
||||||
|
{:q "x"})))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue