simpleui icon indicating copy to clipboard operation
simpleui copied to clipboard

Document that parameters need to be keywordized

Open Ramblurr opened this issue 2 years ago • 3 comments

I integrated ctmx into a template stack I use that runs pedestal+reitit. Out of the box any ctmx components relying on POST params were not working. The root issue was that the form parameters were not being convert to keywords. reitit.http.interceptors doesn't include an interceptor out of the box for this. reitit.http.interceptors.parameters only keywordizes query params.

The solution is to use the ring keyword-params middleware and wrap it in an interceptor:

(ns foo (:require
   [reitit.http.interceptors.parameters :as parameters]
  [ring.middleware.keyword-params :as keyword-params]))

(def keyword-params-interceptor
  {:name ::keyword-params
   :enter (fn [ctx]
            (let [request (:request ctx)]
              (assoc ctx :request
                     (keyword-params/keyword-params-request request))))})

(def default-interceptors [
                           ;; add other interceptors as you see fit
                           (parameters/parameters-interceptor)
                           keyword-params-interceptor])

; use default-interceptors in your routes

For ctmx it might be useful to mention in the docs/readme that the library expects all parameters to be keywordized.

Ramblurr avatar Sep 26 '22 08:09 Ramblurr

Thanks @Ramblurr ! I'll make sure to do that. Will you publish the template stack to e.g. clojars?

whamtet avatar Sep 26 '22 08:09 whamtet

I can't publish the template stack as is because it's internal (it has a lot of other non ctmx related stuff), but I can create a sample project showing pedestal + reitit.http.interceptors + ctmx!

Ramblurr avatar Oct 21 '22 06:10 Ramblurr

@Ramblurr please publish that and I will update my doco and make any other changes as necessary. Your help is much appreciated!

whamtet avatar Oct 21 '22 20:10 whamtet

Hi @Ramblurr , any luck reproducing the issue for me in a public repo? I would like to fix this issue but its good to confirm with a demo to nail down that we are talking about the same thing.

whamtet avatar Nov 06 '22 00:11 whamtet

Sorry for the delay!

Here is a sample repo: https://github.com/Ramblurr/ctmx-pedestal/blob/main/src/ctmx_pedestal/demo.clj

Just jump into the repl, load the demo ns, then run the start fn. You can see the commented bits about the keyword-params-interceptor that relate to this issue.

Ramblurr avatar Nov 07 '22 12:11 Ramblurr

@Ramblurr I've added a note to README.md in the introduction. Are there any other changes you'd like to see?

whamtet avatar Nov 07 '22 21:11 whamtet

Looks good to me! Maybe link to this issue for more context?

Ramblurr avatar Nov 08 '22 06:11 Ramblurr