Fork me on GitHub
#reitit
<
2019-11-25
>
conan17:11:35

Can anybody help we with a problem getting spec coercion working with ring? I've missed something. Following https://github.com/metosin/reitit/blob/master/doc/ring/coercion.md, • I've added a :middleware key to the options map passed to reitit.ring/ring-handler with the coercion middleware from reitit.ring.coercion • I've added a spec for the path param in my route (`::invitation-id` is a spec for a uuid, I got this from https://github.com/metosin/reitit/blob/master/doc/coercion/clojure_spec_coercion.md) • I've also specified a :coercion key at the top of my routes.

(s/def ::invitation-id ::invite/id)

(defn app
  []
  (rr/ring-handler
    (rr/router
      ["" {:coercion rspec/coercion}
       ["/invitations"
        ["/:invitation-id" {:parameters {:path (s/keys :req-un [::invitation-id])}
                            :delete delete-invite}]]])
    (rr/create-default-handler)
    {:middleware [rrc/coerce-exceptions-middleware
                  rrc/coerce-request-middleware
                  rrc/coerce-response-middleware]}))
The :parameters map in my request remains empty. I've seen examples where the router has an options map with a :data key that contains the coercion type and middleware (https://github.com/metosin/reitit/blob/master/examples/ring-spec-swagger/src/example/server.clj). I think I must have put something in the wrong place?

conan17:11:15

maybe this middleware should be passed to the router options instead of the ring-handler options? the ring-handler function is destructuring a key called :middleware from its options though

conan17:11:22

Reitit 0.3.10

ikitommi18:11:38

@conan you should put the coercion mw inside the router. Either under [:data :middleware] on router opts (gets merged to all routes) or to route data.

ikitommi18:11:41

might be good idea to document this..

conan18:11:50

Ah ok thanks. Out of interest what middleware goes on the ring-handler?

ikitommi18:11:37

Something that should effect the default handler too. They are applied before any routing, always

ikitommi18:11:58

request logging for example, if route misses should be logged too