Implement encoding of multi-valued query params.

This commit is contained in:
Pauli Jaakkola 2019-12-02 15:33:55 +02:00
parent 0adb820bba
commit 6d5bf64833
2 changed files with 14 additions and 11 deletions

View file

@ -73,7 +73,7 @@
(defn resolve-routes [raw-routes {:keys [coerce] :as opts}] (defn resolve-routes [raw-routes {:keys [coerce] :as opts}]
(cond->> (->> (walk raw-routes opts) (map-data merge-data)) (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] (defn path-conflicting-routes [routes opts]
(-> (into {} (-> (into {}
@ -239,14 +239,19 @@
[params] [params]
(maybe-map-values #(url-encode (into-string %)) 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 (defn query-string
"shallow transform of query parameters into query string" "shallow transform of query parameters into query string"
[params] [params]
(->> params (->> params
(map (fn [[k v]] (map (fn [[k v]]
(str (form-encode (into-string k)) (if (or (sequential? v) (set? v))
"=" (str/join "&" (map query-parameter (repeat k) v))
(form-encode (into-string v))))) (query-parameter k v))))
(str/join "&"))) (str/join "&")))
(defmacro goog-extend [type base-type ctor & methods] (defmacro goog-extend [type base-type ctor & methods]

View file

@ -51,13 +51,11 @@
{:a 1} "a=1" {:a 1} "a=1"
{:a nil} "a=" {:a nil} "a="
{:a :b :c "d"} "a=b&c=d" {:a :b :c "d"} "a=b&c=d"
{:a "b c"} "a=b+c")) {:a "b c"} "a=b+c"
{:a ["b" "c"]} "a=b&a=c"
; TODO: support seq values? {:a ["c" "b"]} "a=c&a=b"
;{:a ["b" "c"]} "a=b&a=c" {:a (seq [1 2])} "a=1&a=2"
;{:a ["c" "b"]} "a=c&a=b" {:a #{"c" "b"}} "a=b&a=c"))
;{:a (seq [1 2])} "a=1&a=2"
;{:a #{"c" "b"}} "a=b&a=c"
;; test from https://github.com/playframework/playframework -> UriEncodingSpec.scala ;; test from https://github.com/playframework/playframework -> UriEncodingSpec.scala