Fork me on GitHub
#reitit
<
2022-07-25
>
annarcana20:07:44

I am failing to get actual malli coercion behavior and I'm rather at a loss as to why. I'm following the examples almost line for line and yet the compiled coercer doesn't showup in the :result key as shown in the docs. I just get nil, and the result is no coercion happens (`coerce` just returns nil). Code in the replies.

ArtW20:07:44

That appears to be a private repo.

annarcana20:07:55

Oh dear, so it is, I thought we'd opened this already.

annarcana20:07:01

I'll summarize then. We generate a set of routes that looks like this:

(def expected-api
  [[(str "/test-model")
    {:coercion reitit.coercion.malli/coercion}
    ["/" {:get {:responses {200 [TestModel]}}
          :post {:parameters {:body TestModel}
                 :responses {200 TestModel}}}]
    [(str "/:id")
     {:get {:parameters {:path [:id :int]}
            :responses {200 TestModel}}
      :patch {:parameters {:path [:id :int]
                           :body TestModel}
              :responses {200 TestModel}}
      :delete {:parameters {:path [:id :int]}
               :responses {200 "OK"}}}]]])
Turn it into a router like so:
(def router (ring/router generated-api
                         {:validate rrs/validate
                          :compile coercion/compile-request-coercers}))
Now according to the docs, if I match on one of these paths I should get a coercer under the :result key in the match but ... I don't.
(r/match-by-path router "/test-model/4")
{:template "/test-model/:id",
 :data
 {:coercion #Coercion{:name :malli},
  :get
  {:parameters {:path [:id :int]},
   :responses {200 [:map [:id :int] [:name :string] [:cat? :boolean] [:datetime [:or :string :int]]]}},
  :patch
  {:parameters
   {:path [:id :int], :body [:map [:id :int] [:name :string] [:cat? :boolean] [:datetime [:or :string :int]]]},
   :responses {200 [:map [:id :int] [:name :string] [:cat? :boolean] [:datetime [:or :string :int]]]}},
  :delete {:parameters {:path [:id :int]}, :responses {200 "OK"}}},
 :result nil,
 :path-params {:id "4"},
 :path "/test-model/4"}
So calling coerce! just returns nil and I do not understand why.