ring 2022-03-23

is there some middleware to normalize query/body parameter names e.g. someParam -> some_param ?

which router are you using? If you're using reitit I have a solution for that

Compojure at the moment, but I’m actually thinking about switching to reitit so you’re solution is potentially useful πŸ™‚

This is what I do:

(def ^:private custom-serialization
  (m/create
   (-> m/default-options
       (assoc-in [:formats "application/json" :encoder-opts] {:encode-key-fn (comp csk/->snake_case name)}) ;; clojure -> json
       (assoc-in [:formats "application/json" :decoder-opts] {:decode-key-fn (comp keyword csk/->kebab-case)})))) ;; json -> clojure

Then in the configuration/data of reitit:

:muuntaja custom-serialization

works a treat

you can swap out the csk to whatever you need

I also prefer reitit, but your example does not use anything reitit-specific, right? Muuntaja is a standalone library that you can also use as a ring middleware. https://github.com/metosin/muuntaja/blob/master/doc/With-Ring.md

Very true πŸ™‚

But, if he's switching maybe to reitit, at least he knows what I do πŸ™‚

πŸ˜† 1

We should probably also point @slack.jcpsantiago to https://github.com/clj-commons/camel-snake-kebab β€” the csk namespace in your example is just that πŸ™‚

oh yes, good shout. Tis a great library.

πŸ‘ 1

@ferdinand @dharrigan thank you both! yeah csk gets me super far already actually, but like I said I’ll move to reitit so @dharrigan’s solution is great

πŸ‘ 1

np, glad to help πŸ™‚