Fork me on GitHub
#ring
<
2022-03-23
>
Santiago17:03:23

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

dharrigan17:03:14

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

Santiago18:03:48

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

dharrigan20:03:03

This is what I do:

dharrigan20:03:14

(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

dharrigan20:03:30

Then in the configuration/data of reitit:

dharrigan20:03:35

:muuntaja custom-serialization

dharrigan20:03:52

works a treat

dharrigan20:03:59

you can swap out the csk to whatever you need

Ferdinand Beyer20:03:19

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

dharrigan20:03:57

Very true 🙂

dharrigan20:03:32

But, if he's switching maybe to reitit, at least he knows what I do 🙂

😆 1
Ferdinand Beyer20:03:56

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 🙂

dharrigan20:03:10

oh yes, good shout. Tis a great library.

👍 1
Santiago20:03:30

@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
dharrigan21:03:17

np, glad to help 🙂