From 6d5bf64833c28fdbdfb9ef94182433e829c526dc Mon Sep 17 00:00:00 2001 From: Pauli Jaakkola Date: Mon, 2 Dec 2019 15:33:55 +0200 Subject: [PATCH] Implement encoding of multi-valued query params. --- modules/reitit-core/src/reitit/impl.cljc | 13 +++++++++---- test/cljc/reitit/impl_test.cljc | 12 +++++------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/modules/reitit-core/src/reitit/impl.cljc b/modules/reitit-core/src/reitit/impl.cljc index 5f41802c..c99d593b 100644 --- a/modules/reitit-core/src/reitit/impl.cljc +++ b/modules/reitit-core/src/reitit/impl.cljc @@ -73,7 +73,7 @@ (defn resolve-routes [raw-routes {:keys [coerce] :as opts}] (cond->> (->> (walk raw-routes opts) (map-data merge-data)) - coerce (into [] (keep #(coerce % opts))))) + coerce (into [] (keep #(coerce % opts))))) (defn path-conflicting-routes [routes opts] (-> (into {} @@ -239,14 +239,19 @@ [params] (maybe-map-values #(url-encode (into-string %)) params)) +(defn- query-parameter [k v] + (str (form-encode (into-string k)) + "=" + (form-encode (into-string v)))) + (defn query-string "shallow transform of query parameters into query string" [params] (->> params (map (fn [[k v]] - (str (form-encode (into-string k)) - "=" - (form-encode (into-string v))))) + (if (or (sequential? v) (set? v)) + (str/join "&" (map query-parameter (repeat k) v)) + (query-parameter k v)))) (str/join "&"))) (defmacro goog-extend [type base-type ctor & methods] diff --git a/test/cljc/reitit/impl_test.cljc b/test/cljc/reitit/impl_test.cljc index d8ff062f..1b2165c5 100644 --- a/test/cljc/reitit/impl_test.cljc +++ b/test/cljc/reitit/impl_test.cljc @@ -51,13 +51,11 @@ {:a 1} "a=1" {:a nil} "a=" {:a :b :c "d"} "a=b&c=d" - {:a "b c"} "a=b+c")) - -; TODO: support seq values? -;{:a ["b" "c"]} "a=b&a=c" -;{:a ["c" "b"]} "a=c&a=b" -;{:a (seq [1 2])} "a=1&a=2" -;{:a #{"c" "b"}} "a=b&a=c" + {:a "b c"} "a=b+c" + {:a ["b" "c"]} "a=b&a=c" + {:a ["c" "b"]} "a=c&a=b" + {:a (seq [1 2])} "a=1&a=2" + {:a #{"c" "b"}} "a=b&a=c")) ;; test from https://github.com/playframework/playframework -> UriEncodingSpec.scala