This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-02-24
Channels
- # announcements (5)
- # aws (24)
- # babashka (41)
- # beginners (130)
- # bristol-clojurians (2)
- # calva (39)
- # chlorine-clover (64)
- # cider (30)
- # clojure (202)
- # clojure-belgium (1)
- # clojure-dev (99)
- # clojure-europe (5)
- # clojure-hungary (4)
- # clojure-italy (10)
- # clojure-losangeles (8)
- # clojure-nl (11)
- # clojure-norway (6)
- # clojure-spec (7)
- # clojure-uk (12)
- # clojurescript (52)
- # core-typed (26)
- # cursive (19)
- # data-science (19)
- # datomic (19)
- # duct (10)
- # emacs (17)
- # fulcro (22)
- # graalvm (11)
- # jobs (3)
- # kaocha (28)
- # leiningen (6)
- # lumo (2)
- # malli (10)
- # nrepl (2)
- # off-topic (23)
- # pathom (2)
- # pedestal (7)
- # re-frame (3)
- # reagent (30)
- # reitit (2)
- # remote-jobs (2)
- # shadow-cljs (77)
- # sql (10)
- # test-check (22)
- # tools-deps (37)
- # vscode (1)
- # yada (3)
I'm running into a very interesting coercion issue and I'm wondering if anyone here can help me see what I'm doing wrong or if there's a workaround. Here's my basic router:
(def handler
(ring/ring-handler
(ring/router
["/api"
{:coercion spec-coercion/coercion
:middleware [parameters/parameters-middleware ;; query-params & form-params
coercion/coerce-request-middleware ;; coercing request parameters
]}
["/foo"
{:post {:parameters {:consumes "application/x-www-form-urlencoded"
:form {:ids [int?]}}
:handler (fn [{{{:keys [ids]} :form} :parameters}]
(ok (str ids)))}}]
])))
There's one endpoint, "/api/foo" that expects a collection of ints. When I call it with various params I get the following results:
(comment
(require '[ring.mock.request :as mock])
;I expect this to fail
(handler (mock/request :post "/api/foo" {:ids 1}))
;Why does this fail?
(handler (mock/request :post "/api/foo" {:ids [1]}))
;These two pass
(handler (mock/request :post "/api/foo" {:ids [1 3]}))
(handler (mock/request :post "/api/foo" {:ids [1 2 3]}))
)
The odd case that I can't understand is the second one. When I call the endpoint with a sequence of one element something in the handler appears to be converting the [1] into a 1. Is this a known or expected behavior? Any workarounds? I've tried adding a spec like (s/or :int int? :ints (s/+ int?)) but that didn't work. If it would be helpful to list the requires vector I can provide that, too. Thanks!I manage to reproduce your error, it seems odd indeed. Which is interesting is that when you use swagger support, the way it handles x-www-form-urlencoded
input type is by separating each value in a newline, therefore I cannot be sure if I express in a swagger interface your second case correctly. I manage to workaround the problem with the following spec: (s/or :int int? :ints (s/coll-of int?))
but your first two cases will be handled equally.
👍 4