Skip to content

Commit

Permalink
Use defined :string :default transformer for query-string-coercer
Browse files Browse the repository at this point in the history
  • Loading branch information
Deraen committed Jan 31, 2025
1 parent 4eb29d3 commit 5ca2219
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
14 changes: 10 additions & 4 deletions modules/reitit-malli/src/reitit/coercion/malli.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
[malli.swagger :as swagger]
[malli.transform :as mt]
[malli.util :as mu]
[reitit.coercion :as coercion]))
[reitit.coercion :as coercion]
[clojure.string :as string]))

;;
;; coercion
Expand Down Expand Up @@ -79,11 +80,13 @@
(defn- -query-string-coercer
"Create coercer for query-parameters, always allows extra params and does
encoding using string-transformer."
[schema transfomers options]
[schema string-transformer-provider options]
(let [;; Always allow extra paramaters on query-parameters encoding
open-schema (mu/open-schema schema)
;; 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)]
(fn [value format]
(if encoder
Expand Down Expand Up @@ -126,6 +129,9 @@
([opts]
(let [{:keys [transformers lite compile options error-keys encode-error] :as opts} (merge default-options opts)
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)
compile (if lite (fn [schema options]
(compile (binding [l/*options* options] (l/schema schema)) options))
Expand Down Expand Up @@ -192,6 +198,6 @@
(-response-coercer [_ schema]
(-coercer schema :response transformers :encode opts))
(-query-string-coercer [_ schema]
(-query-string-coercer schema transformers opts))))))
(-query-string-coercer schema string-transformer-provider opts))))))

(def coercion (create default-options))
33 changes: 25 additions & 8 deletions test/cljs/reitit/frontend/core_test.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
(ns reitit.frontend.core-test
(:require [clojure.string :as str]
[clojure.test :refer [are deftest is testing]]
[malli.core :as m]
[malli.transform :as mt]
[reitit.coercion :as rc]
[reitit.coercion.malli :as rcm]
[reitit.coercion.schema :as rcs]
Expand Down Expand Up @@ -305,11 +307,26 @@
(rf/match->path {:path "foo"} {:q :x})
"foo?q=x")))

(is (= "foo?q=__x"
(rf/match->path {:data {:coercion rcm/coercion
:parameters {:query [[:map
[:q {:decode/string (fn [s] (keyword (subs s 2)))
:encode/string (fn [k] (str "__" (name k)))}
:keyword]]]}}
:path "foo"}
{:q "x"}))))
(testing "default string transformer"
(is (= "foo?q=__x"
(rf/match->path {:data {:coercion rcm/coercion
:parameters {:query [[:map
[:q {:decode/string (fn [s] (keyword (subs s 2)))
:encode/string (fn [k] (str "__" (name k)))}
:keyword]]]}}
: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"})))))

0 comments on commit 5ca2219

Please sign in to comment.