reitit

eval-on-point 2026-05-27T16:52:07.207819Z

Is it possible to modify route data in middleware records? [Some of the reitit middlewares](https://github.com/metosin/reitit/blob/5d5c965deb3c7c8d64b8dd636432eaa14f0324ec/modules/reitit-middleware/src/reitit/ring/middleware/muuntaja.clj#L95)seemseem to be able to do this it but I am having trouble reproducing it for arbitrary route data. Context: I have an authorization middleware record that adds support for an :authorize key in the route data. When that key is in the route data, I want the middleware to add :response {403...} to the route data for coercion and openapi features.

juhoteperi 2026-05-27T17:05:58.930349Z

Yes, compiled middleware is the feature those examples use: https://cljdoc.org/d/metosin/reitit/0.10.1/doc/ring/compiling-middleware

juhoteperi 2026-05-27T17:07:11.721139Z

The doc examples only return a middleware function from the compilation fn

juhoteperi 2026-05-27T17:08:39.393469Z

But the compile fn can return a map with {:wrap middleware-fn :data route-data-to-merge-into-the-route}

juhoteperi 2026-05-27T17:08:45.885029Z

like those muuntaja mw do

juhoteperi 2026-05-27T17:10:11.227409Z

The docs don't really describe this. The compiling middleware does mention that the fn should return IntoMiddleware: https://github.com/metosin/reitit/blob/5d5c965deb3c7c8d64b8dd636432eaa14f0324ec/modules/reitit-core/src/reitit/middleware.cljc#L15-L55

juhoteperi 2026-05-27T17:10:37.410769Z

and there the code shows that returning a Fn is just same as {:wrap mw-fn}

eval-on-point 2026-05-27T18:08:55.937429Z

Thanks for the thoughtful response. That makes a lot of sense. Then in my case, I think that there is some other issue. When I use match-by-path to inspect the route data, the :data key from the middleware does not appear to be merged in. Same with the muuntaja middlewares. However, my openapi document reflects the available muuntaja formats, but does not show my response schemas.

juhoteperi 2026-05-27T18:18:15.142199Z

I don't exactly remember how the data from compiled middleware is applied. For openapi docs, I know it works because the openapi.json handler inspects the route-tree & route-data on the request handling time, so it sees the route-data which has all the transformations (compile time mw changes) visible. Seems possible match-by-path works on the route tree where these aren't yet applied.

juhoteperi 2026-05-27T18:18:49.180679Z

Could be something on how it merges the route data. But I would think is uses the same meta-merge logic as elsewhere.

juhoteperi 2026-05-27T18:30:26.062879Z

The route data from middlewares is applied at the Router level: https://github.com/metosin/reitit/blob/master/modules/reitit-core/src/reitit/middleware.cljc#L101-L120 compile-result could be usable with match-by-path to debug what is happening

eval-on-point 2026-05-27T19:38:39.882959Z

I think that you are right about compile-result being important. The :data key of the created endpoints does not have the :data keys of the expanded and transformed middlewares merged into it. So, when the openapi doc is constructed here: https://github.com/metosin/reitit/blob/5d5c965deb3c7c8d64b8dd636432eaa14f0324ec/modules/reitit-openapi/src/reitit/openapi.clj#L215-L221, only the [:data :openapi] path defined by each middleware is represented in the openapi doc. So, it is impossible to extend the OpenApi doc via middleware. I believe a similar issue occurs for coercions but haven't tested it.