metosin/reitit

Single item lists in query params with spec coercion

Open

#298 创建于 2019年6月26日

在 GitHub 查看
 (8 评论) (10 反应) (0 负责人)Clojure (1,569 star) (261 fork)batch import
enhancementhelp wanted

描述

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?)?

贡献者指南