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.
Yes, compiled middleware is the feature those examples use: https://cljdoc.org/d/metosin/reitit/0.10.1/doc/ring/compiling-middleware
The doc examples only return a middleware function from the compilation fn
But the compile fn can return a map with {:wrap middleware-fn :data route-data-to-merge-into-the-route}
like those muuntaja mw do
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
and there the code shows that returning a Fn is just same as {:wrap mw-fn}
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.
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.
Could be something on how it merges the route data. But I would think is uses the same meta-merge logic as elsewhere.
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
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.