Handle empty seq as empty string in query-string

example:

instead of

```clojure
(query-string {:nil nil
               :vec []
               :seq-empty '()})
;; => "nil=&&"
```

now

```clojure
(query-string {:nil nil
               :vec []
               :seq-empty '()})
;; => "nil=&vec=&seq-empty="
```
This commit is contained in:
Toni Väisänen 2022-10-12 13:19:10 +03:00
parent 3dff4c84aa
commit c69b4cde3a
2 changed files with 7 additions and 1 deletions

View file

@ -249,6 +249,10 @@
(->> params
(map (fn [[k v]]
(if (or (sequential? v) (set? v))
(str/join "&" (map query-parameter (repeat k) v))
(if (seq v)
(str/join "&" (map query-parameter (repeat k) v))
;; Empty seq results in single & character in the query string.
;; Handle as empty string to behave similarly as when the value is nil.
(query-parameter k ""))
(query-parameter k v))))
(str/join "&")))

View file

@ -50,6 +50,8 @@
{"a" "b"} "a=b"
{:a 1} "a=1"
{:a nil} "a="
{:a []} "a="
{:a '()} "a="
{:a :b :c "d"} "a=b&c=d"
{:a "b c"} "a=b+c"
{:a ["b" "c"]} "a=b&a=c"