Comments (11)
ugh. we'll check this out.
from compojure-api.
Ah yes. Middleware is executed before the handler checks if the route is correct.
- Compojure executes GET* "/first" handler, it returns nil
- Compojure executes GET* "/second" handler, it should return nil (but instead the middleware is executed first)
- Should executes GET "/third" handler...
I merged the tests and I'll have a fix shortly.
from compojure-api.
... And the fix is probably to remove :middlewares because it's impossible to implement this properly.
To apply middlewares after route is checked but before the handler is called would require changes in compojure.core generated code: https://github.com/weavejester/compojure/blob/1.1.8/src/compojure/core.clj#L99.
from compojure-api.
Couldn't middlewares be applied on the route body:
(GET* "/foo" []
:middlewares [mw]
(ok "foo"))
;;; =>
(GET "/foo" request
(mw request))
or do I understand something wrong? (I though that middlewares is desugared into something like that)
from compojure-api.
Middlewares don't take a request as param but a handler:
(defn mw [handler]
(fn [req]
(let [req (update-in req [:x] ...)
res (handler req)]
res)))
(GET* "/foo" req
:body-params [x :- String]
:middlewares [mw]
(ok x)
=>
(GET "/foo" req
;; mock-handler
((mw
(fn [_] (ok x)))
req)
And even while it's possible to create "a mock handler": (fn [_] ~@body)
the edits to request won't be seen by coercers etc.
But if we forget the middleware semantics and create e.g. "routewares" we could implement what you are suggesting:
;; Can't edit the req
;; can call the route body conditionally
(defn rw [req next]
(if (... req)
(ok {:value "bar"})
(next)))
(GET* "/foo" []
:routewares [rw]
(ok "foo"))
from compojure-api.
routewares are fine to me.
I found the bug while adding authentication middleware for just few selected routes (e.g. content modifying ones). Not sure what is the right way to do that kind of functionality.
EDIT though I want to do authentication check before parsing the request...
from compojure-api.
I think I found a fix :)
from compojure-api.
I tried with the latest version it doesn't seem to work.
(defroute* my-routes
(GET* "/route1" []
:middle wares [admin-only]
... )
(GET* "/route2" []
:middle wares [user-only]
... ))
route2
isn't accessible for users :-(
from compojure-api.
I haven't yet merged the fix to master branch.
from compojure-api.
Ah thanks! I'll try with the right version.
from compojure-api.
It works perfectly, thanks!
from compojure-api.
Related Issues (20)
- OpenAPI? HOT 5
- Add {:coercion :select-schema} option
- muuntaja encoder modifying swagger.json output HOT 1
- Vulnerability issue in dependent lib
- Missing security and deprecated meta
- Memory use rises with requests HOT 3
- Can't get Swagger UI to provide interface for arbitrary query params HOT 2
- fix broken url link in docs
- Clj-kondo linting issue HOT 2
- Consider bumping to Compojure 1.7.0 or Compojure 1.6.3
- Mixing compojure.api.sweet/defapi and compojure.core/defroutes
- How do I specify an optional field when accepting multipart-params for a request?
- Compojure 2.x should be backwards compatible with 1.x HOT 3
- Automatically push `context` parameters into endpoints when safe HOT 3
- Calculate coercers/schemas outside contexts when not dependent on request
- Pull out static parts of an otherwise-dynamic context
- Compile to non-linear routing strategies
- Immutable `restructure` interface
- Potential memory leak
- java.lang.AbstractMethodError and dependency mess HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from compojure-api.