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}]
(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]

View file

@ -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