This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-12-05
Channels
- # adventofcode (111)
- # announcements (20)
- # babashka (19)
- # beginners (47)
- # calva (7)
- # clojure (56)
- # clojure-dev (27)
- # clojurescript (2)
- # events (1)
- # holy-lambda (1)
- # juxt (2)
- # meander (18)
- # minecraft (4)
- # missionary (107)
- # nextjournal (21)
- # off-topic (30)
- # reagent (7)
- # reitit (19)
- # releases (1)
- # tools-build (8)
- # tools-deps (7)
- # vim (22)
- # xtdb (4)
Hey,
Is it possible to decode/transform a UUID passed as a string into a java.util.UUID
as part of the request coercion?
The request is defined as:
[:map
[:request_id :uuid]
; other fields
]
Clients send the request_id
as a string. but I’d to work with a java.util.UUID
in the handlers after setting the coerce-request-middleware
https://github.com/metosin/reitit/blob/master/modules/reitit-malli/src/reitit/coercion/malli.cljc#L109 Override the default transformer to string instead of json transformer for content type
Do I need to specify all the inner keys? (`:body`, :string
and :response
)
Are there any side-effects to using the string decoder instead of the json decoder?
Am I doing it right? The request_id
is still a string in the :body-params
..
(reitit-coercion-malli/create
{:error-keys #{:humanized}
:compile malli-util/open-schema
:validate true
:enabled true
:encode-error (fn [validation-body] {:errors (:humanized validation-body)})
:default-values false
:strip-extra-keys true
:transformers {:body {:default reitit-coercion-malli/string-transformer-provider
:formats {"application/json" reitit-coercion-malli/string-transformer-provider}}
:string {:default reitit-coercion-malli/string-transformer-provider}}})
Uhhh, yes, string deocder includes transformations that don't make sense with JSON. AND using string decoder here doesn't change anything, UUID transformer is part of json-decoders (and string-decoders includes all json-decoders + others).
@U02AH3D0HEV :body-params
will never contain coerced values, those are always the raw params, coerced values are under req :parameters :body
@U061V0GG2, the :parameters
field doesn’t exist in the req
, the only relevant fields are :path-params
, :headers
and :body-params
Then you are missing the coercion middlewares
Ah, that’s my bad, I was in the wrong branch 😐
The :parameters
exist an the request_id
, once defined as a :uuid
gets coerced into a java.util.UUID
I didn’t know about the :parameters
field, though.
Can you point me to the relevant docs?
https://cljdoc.org/d/metosin/reitit/0.5.15/doc/ring/pluggable-coercion#defining-parameters-and-responses > Handlers can access the coerced parameters via the :parameters key in the request.
The main Coercion page doesn't mention this, because this is Ring specific detail
Thanks! 🙏
What do I need to change to add a custom transformer?
(A custom decoder with an :enter
function)
Is it the :transformers :body formats
?
You probably don't need customer tranformers.
You can add custom decode fns to ~any schema: [:uuid {:decode/string (fn [s] (UUID. (str "foo" s)))}]
(or :decode/json
if this decoding is required for values from JSON).
Thanks again!