ring

Santiago 2022-03-23T17:45:23.190339Z

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

dharrigan 2022-03-23T17:56:14.237789Z

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

Santiago 2022-03-23T18:44:48.461609Z

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

dharrigan 2022-03-23T20:05:03.081009Z

This is what I do:

dharrigan 2022-03-23T20:06:14.497609Z

(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

dharrigan 2022-03-23T20:06:30.182879Z

Then in the configuration/data of reitit:

dharrigan 2022-03-23T20:06:35.468699Z

:muuntaja custom-serialization

dharrigan 2022-03-23T20:06:52.531969Z

works a treat

dharrigan 2022-03-23T20:06:59.970909Z

you can swap out the csk to whatever you need

Ferdinand Beyer 2022-03-23T20:12:19.310899Z

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

dharrigan 2022-03-23T20:12:57.698099Z

Very true πŸ™‚

dharrigan 2022-03-23T20:13:32.155839Z

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

πŸ˜† 1
Ferdinand Beyer 2022-03-23T20:14:56.993339Z

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 πŸ™‚

dharrigan 2022-03-23T20:15:10.480469Z

oh yes, good shout. Tis a great library.

πŸ‘ 1
Santiago 2022-03-23T20:51:30.037349Z

@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
dharrigan 2022-03-23T21:02:17.853279Z

np, glad to help πŸ™‚