This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-01-25
Channels
- # announcements (3)
- # asami (63)
- # babashka (5)
- # babashka-sci-dev (32)
- # beginners (56)
- # calva (2)
- # cider (28)
- # clj-commons (9)
- # clj-kondo (16)
- # cljdoc (41)
- # cljs-dev (19)
- # clojure (67)
- # clojure-europe (15)
- # clojure-nl (1)
- # clojure-poland (1)
- # clojure-uk (2)
- # clojurescript (27)
- # community-development (10)
- # data-science (2)
- # datascript (8)
- # datomic (21)
- # events (3)
- # fulcro (54)
- # graalvm (18)
- # introduce-yourself (2)
- # juxt (3)
- # lsp (6)
- # music (1)
- # nextjournal (8)
- # off-topic (44)
- # omni-trace (1)
- # reitit (13)
- # releases (3)
- # rewrite-clj (4)
- # shadow-cljs (10)
- # spacemacs (6)
- # sql (12)
- # tools-build (17)
- # tools-deps (3)
- # web-security (1)
I'm wondering, in compojure sweet
, the predessor to reitit
, it had this functionality of meta/restructure-param
which allowed one to register a multimethod value, that was handled by compojure sweet
(detailed here: https://github.com/metosin/compojure-api/wiki/Creating-your-own-metadata-handlers). What would be the approach if one was moving to reitit
from compojure sweet
and the legacy system had loads of these. What comes to mind is simply to add to the response (on the way out) a key/value pair and have some middleware look for that key/value pair and handle accordingly.
Compiled middleware can be used to attach middleware to specfici routes based on route-data: https://cljdoc.org/d/metosin/reitit/0.5.15/doc/ring/compiling-middleware
If I want to unit test my reitit
routes with malli
validation, how should the body
be submitted? Despite how I submit the body
contents I get coercion error. I've tried plain data as in nclj maps, json
and even InputStream
to no avail.. Feeling quite stupid at the moment 🙈
Error
ERROR in () (coercion.cljc:46)
Claim
expected: (= {:status 200, :body {}} (app (request "/api/claim" :put valid-json-claim)))
actual: clojure.lang.ExceptionInfo: Request coercion failed: #reitit.coercion.CoercionError (full schema here)
.
.
.
:errors (#Error{:path [], :in [], :schema [:map {:closed true} (schema continues...)
Route conf
["/claim"
{:put {:parameters {:body schema/claim}
:responses {200 {:body string?}}
:handler (fn [{:keys [body-params]}]
(log/info (str "Got body" (:body body-params)))
{:status 200
:body (:body body-params)})}}]
Reitit conf
(def app
(ring/ring-handler
(ring/router
(routes)
{:data {:muuntaja muuntaja/instance
:middleware [#_wrap-internal-error
swagger/swagger-feature
middleware.muuntaja/format-negotiate-middleware
middleware.muuntaja/format-response-middleware
middleware.muuntaja/format-request-middleware
wrap-dbg
#_coercion/coerce-exceptions-middleware
coercion/coerce-request-middleware
coercion/coerce-response-middleware]
:coercion (reitit.coercion.malli/create
{;; set of keys to include in error messages
:error-keys #{:value :humanized #_:type :coercion :in :schema :errors #_:transformed}
;; validate request & response
:validate true
;; top-level short-circuit to disable request & response coercion
:enabled true
;; strip-extra-keys (effects only predefined transformers)
:strip-extra-keys false
;; add/set default values
:default-values true
;; malli options
:options nil})}})
(ring/routes
(swagger-ui/create-swagger-ui-handler
{:path "/"
:config {:validatorUrl nil
:operationsSorter "alpha"}})
(ring/create-default-handler))))
Test, where valid-json-claim
is a map turned into json
with jsonista
(defn- request
([uri method]
(request uri method nil))
([uri method body]
{:uri uri
:request-method method
:body body}))
(deftest routes-test
(testing "Routes"
(testing "Claim"
(is (= {:status 200 :body {}}
(app (request "/api/claim" :put valid-json-claim)))))))
Hello! Can route params be optional? I want to route "/messages"
and "/messages/123"
to the same handler. Rails allows routes like "/messages(/:id)"
, is that possible?
There are catch-all-parameters indeed ➡️ https://github.com/metosin/reitit/blob/master/doc/basics/route_syntax.md Route with catch-all parameter:
["/public/*path"]
hi, could somebody help me understand what's wrong with my params declaration? I have a :get
, and I want a query param param
to be foo
, bar
, or nothing. My parameters map is {:query {:param (ds/maybe (s/spec #{"foo" "bar"}))}}
. My handler correctly rejects any non-empty param
value that's not "foo"
or "bar"
but incorrectly rejects a request that doesn't specify any value for param