Fork me on GitHub
#reitit
<
2023-06-01
>
Santiago06:06:33

I was surprised to find that coerced query-params use strings as keys instead of keywords like body-params . Is there configuration available to change that?

p-himik07:06:21

What about (-> req :parameters :query)?

Santiago08:06:16

:parameters is nil but :params is the same as :query-params (as expected)

p-himik08:06:51

Mm, then it's some middleware then that populates :parameters.

Santiago08:06:13

the luminous docs also references this, but makes no further comment about it

Santiago08:06:28

(I'm not using Luminous, simply found the reference while googling about this)

p-himik08:06:01

Ah, it's reitit.ring.coercion/coerce-request-middleware on my end.

Santiago08:06:28

;; Global route Configuration - coersion and middleware applied to all routes
(def router-configuration
  "Reitit configuration of coercion, data format transformation and middleware for all routing"
  {:data {:coercion   reitit.coercion.spec/coercion
          :muuntaja   muuntaja/instance
          :middleware [;; swagger feature for OpenAPI documentation
                       api-docs/swagger-feature
                       ;; query-params & form-params
                       parameters/parameters-middleware
                       ;; content-negotiation
                       middleware-muuntaja/format-middleware
                       ;; coercing response body
                       coercion/coerce-response-middleware
                       ;; coercing request parameters
                       coercion/coerce-request-middleware
                       ;; Pretty print exceptions
                       coercion/coerce-exceptions-middleware
                       ;; logging with mulog
                       [middleware-arqivist/wrap-trace-events :trace-events]]}
   ;; pretty-print reitit exceptions for human consumptions
   :exception pretty/exception})
this is my middleware stack (I have coerce-request-middleware in there)

p-himik08:06:47

My coercion is reitit.coercion.malli. But that shouldn't affect this particular thing, I think. The impl of coerce-request-middleware is straightforward, so perhaps you could debug it to see why you don't have :parameters in there. Just in case - middleware transform is great for stuff like this. Is there a chance that that particular resource where you checked that key overrides the coercion?

Santiago08:06:07

not that I can see

["/redirect"
    {:swagger {:externalDocs
               {:description "Slack OAuthV2 docs"
                :url ""}}
     :get
     {:summary "OAuth2 redirect target"
      :description "This endpoint receives the data about the workspace, after a user successfully added the app to their account."
      :parameters {:query-params ::specs/oauth-redirect}
      :responses {200 {:body string?}
                  500 {:body string?}}
      :handler (handlers/oauth-redirect system)}}]
route definition looks pretty standard to me, no special middleware applied to it

Santiago08:06:37

or did you mean something else by "particular resources overriding the coercion"?

p-himik08:06:55

Nah, that's what I meant. But also, I specify my parameters differently - I'd write that as :parameters {:query ...}. But given the code of the coercing middleware, it shouldn't affect anything.

Santiago08:06:46

interesting, I can't use :query there at all

Execution error (AssertionError) at spec-tools.core/create-spec (core.cljc:536).
Assert failed:  Unable to resolve spec: :jcpsantiago.arqivist.api.slack.specs/oauth-redirect
works fine with query-params though (except for the strings, but it looks to me as if it's happening by design)

Santiago08:06:34

☝️ I only changed :query-params to :query , nothing else. No changes to the spec namespace etc

Santiago08:06:43

in any case it's probably an unrelated matter 🙂

p-himik08:06:49

Really weird.

p-himik08:06:58

It actually might be related, if the :parameters key is there and has value nil. It would mean that the coercer just doesn't work - it returns nil. I don't know how the Spec coercer works so can't tell but maybe it looks for the :query key which doesn't work for you.

p-himik08:06:53

Oh, can you check whether that spec is getting any use at all? E.g. feed some wrong data to the endpoint and see if it reports it.

p-himik08:06:30

If you see that the spec ends up not being used, try resolving that keyword spec manually and passing the spec object itself.

Santiago08:06:45

got it! great debugging help @U2FRKM4TW! classic typo xD oath instead of oauth in the spec name! :melting_face: now i'm getting a :parameters key with the expected keywordized map. Thanks!

p-himik08:06:47

Great! HTH.