Fork me on GitHub
#reitit
<
2022-03-08
>
coby05:03:31

Hi all! I have some nested routes like this:

["/:lang"
      ["/" {... :name :home}]
      ["/*slugs" {... :name :page}]]
Where *slugs matches an arbitrarily nested route:
(reitit/match-by-path $router "/en/one/two")
;; {:template "/:lang/*slugs",
;;  :data {... :name :page},
;;  :result nil,
;;  :path-params {:slugs "one/two", :lang "en"},
;;  :path "/en/one/two"}
However, it URL-encodes the nested bit when I try to do reverse routing:
(reitit/match-by-name $router :page {:lang :en :slugs "one/two"})
;; {:template "/:lang/*slugs",
;;  :data
;;  {... :name :page},
;;  :result nil,
;;  :path-params {:lang "en", :slugs "one/two"},
;;  :path "/en/one%2Ftwo"}
Is there a way to disable/control the encoding?

coby05:03:15

or, alternatively, a better way to handle arbitrarily nested routes...?

wontheone105:03:20

https://github.com/metosin/testit#testit- Hello Good people of Metosin. I think testit had a nice idea. Can I ask why it became Deprecated status?

1
DenisMc17:03:52

Hi, quick question that I’d love some help with. Consider the following code:

(require '[muuntaja.core :as mu])
(require '(clojure.java [io :as io]))
(def new-muuntaja-instance
  (mu/create
    (-> mu/default-options
        (assoc-in
          [:formats "application/json" :decoder-opts]
          {:bigdecimals true}))))

(->> (io/input-stream (.getBytes "{\"value\":100}"))
     (mu/decode new-muuntaja-instance "application/json")
     )

(->> (io/input-stream (.getBytes "{\"value\":100.1}"))
     (mu/decode new-muuntaja-instance "application/json")
     )
I have two simple decode functions that use muuntaja to decode a numeric value. I have configured :bigdecimals to true - which decodes doubles as bigdecimals. All well and good, except I would like all values (including ints) to be decoded to bigdecimals for this particular field (as it is 100.1 is a double -> decoded as a bigdecimal, 100 is an int -> decoded as an int). So you can understand my motivation, I am using a malli schema to validate my json, and this is failing validation for amounts that happen to not have a decimal place in my json (because they are being encoded as ints and I am expecting all values to be bigdecimals). Any idea on how I would ensure consistent bigdecimal encoding for numeric fields using muuntaja?

dharrigan17:03:07

A possibility is to provider your own module

dharrigan17:03:49

muuntaja uses jsonista under the hood, and one of the things it allows you to do is to provide a :modules key. You can define your own jacksondatabind module that would give you the behavior you want.

dharrigan17:03:03

I think the approach would be to examine this:

dharrigan17:03:17

notice lines 140-156

dharrigan17:03:37

notice also how a custom jackson module is provided clojure-module.

dharrigan17:03:43

You could do something similar

dharrigan17:03:19

ALso, here is a so answer that may help too (which, although not related to clojure, would give you some insight on what you could try)

DenisMc17:03:03

Thanks for the tips David, I’ll take a look at creating my own module and see how I get on

dharrigan17:03:44

I hope it is useful 🙂

dharrigan17:03:48

Good luck! 🙂

dviramontes22:03:45

does anyone have any examples of applying a middleware (in my case format response as json) for a single route ?

dviramontes22:03:00

this is what i tried but i get an error

(ring/router [["/foobar"
{:get {:responses  {200 {:body map?}}
       :handler    (constantly (response/ok {:data 123}))}
 :middleware [muuntaja/format-negotiate-middleware]}]
,,,])
> HTTP ERROR 500 java.lang.IllegalArgumentException: No implementation of method: :write-body-to-stream of protocol: #'ring.core.protocols/StreamableResponseBody found for class: clojure.lang.PersistentArrayMap

wevrem00:03:28

This middleware does not format the response. It is only doing content negotiation. I think you need to include muuntaja/format-response-middleware. Or consider muuntaja/format-middleware which gets you negotiation, decoding, and encoding all bundled together. https://cljdoc.org/d/metosin/reitit-schema/0.5.15/doc/ring/content-negotiation#content-negotiation

1