metosin/reitit

Single item lists in query params with spec coercion

Open

#298 aperta il 26 giu 2019

Vedi su GitHub
 (8 commenti) (10 reazioni) (0 assegnatari)Clojure (261 fork)batch import
enhancementhelp wanted

Metriche repository

Star
 (1569 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

When defining a route with an array value in the query params like so:

{:get {:parameters {:query {:strings [string?]}}
           :handler (fn [{{:keys [query]} :parameters}]
                            {:status 200
                             :body query})}}

the query string is expected to look something like ?strings=hello&strings=world. However when only one is sent the query string will be ?strings=hello. Causing reitit to spit out

{
  "spec": "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:spec$367119/strings]), :type :map, :leaf? false})",
  "problems": [
    {
      "path": [
        "strings"
      ],
      "pred": "clojure.core/coll?",
      "val": "hello",
      "via": [
        "spec$367119/strings"
      ],
      "in": [
        "strings"
      ]
    }
  ],
  "type": "reitit.coercion/request-coercion",
  "coercion": "spec",
  "value": {
    "strings": "hello"
  },
  "in": [
    "request",
    "query-params"
  ]
}

Maybe reitit should be able to coerce ?strings=hello to ["hello"] if the spec is (coll-of string?)?

Guida contributor