Fork me on GitHub
#reitit
<
2024-06-05
>
nonrecursive14:06:22

hey y'all, is there an example out there of using reitit.ring.middleware.muuntaja and configuring it to encode joda time objects? here's how the router is getting constructed:

(defn router
  [routes]
  (reitit-ring/router
   routes
   {:data {:coercion   reitit.coercion.malli/coercion
           :muuntaja   muuntaja/instance
           :middleware [;; swagger & openapi
                        swagger/swagger-feature
                        openapi/openapi-feature
                        ;; query-params & form-params
                        reitit-parameters/parameters-middleware
                        ;; content-negotiation
                        reitit-muuntaja/format-negotiate-middleware
                        ;; encoding response body
                        reitit-muuntaja/format-response-middleware
                        ;; decoding request body
                        reitit-muuntaja/format-request-middleware
                        ;; coercing response bodys
                        reitit-ring-coercion/coerce-response-middleware
                        ;; coercing request parameters
                        reitit-ring-coercion/coerce-request-middleware
                        ;; multipart
                        multipart/multipart-middleware]}}))

wevrem15:06:32

I’m doing something sort of along those lines. I have my own namespace where I create a muuntaja instance that incorporates custom read-/write-handlers, not for joda time objects but for tick instant’s. (And I can’t remember for sure but there might be joda time objects underneath there somewhere).

nonrecursive15:06:23

thank you! I think I figured it out. Here's what I have:

;; in :require
(:import
   (com.fasterxml.jackson.datatype.joda JodaModule)
   (org.joda.time LocalDate))

(def jsonista-mapper
  "Handle joda->json encoding"
  (jsonista/object-mapper
   {:modules [(JodaModule.)]}))

(def muuntaja-instance
  (muuntaja/create
   (assoc-in muuntaja/default-options
             [:formats "application/json" :encoder-opts :modules]
             [(JodaModule.)])))

(defn router
  [routes]
  (reitit-ring/router
   routes
   {:data {:coercion   reitit.coercion.malli/coercion
           :muuntaja   muuntaja-instance
           :middleware [ ;; swagger & openapi
                        swagger/swagger-feature
                        openapi/openapi-feature
                        ;; query-params & form-params
                        reitit-parameters/parameters-middleware
                        ;; content-negotiation
                        reitit-muuntaja/format-negotiate-middleware
                        ;; encoding response body
                        reitit-muuntaja/format-response-middleware
                        ;; decoding request body
                        reitit-muuntaja/format-request-middleware
                        ;; coercing response bodys
                        reitit-ring-coercion/coerce-response-middleware
                        ;; coercing request parameters
                        reitit-ring-coercion/coerce-request-middleware
                        ;; multipart
                        multipart/multipart-middleware]}}))

wevrem15:06:40

That looks good. I think there is also a single format-middleware that bundles together all three.