metosin / compojure-api

Sweet web apis with Compojure & Swagger
http://metosin.github.io/compojure-api/doc/
Eclipse Public License 1.0
1.11k stars 149 forks source link

Calculate coercers/schemas outside contexts when not dependent on request #464

Open frenchy64 opened 4 months ago

frenchy64 commented 4 months ago

Coercers aren't really amenable to this at the moment, and use a memoized cache to get around this limitation (but given that equivalent Schema's are often not equal, might not be totally effective), but the idea is:

(context "/foo" req
  (GET "/" []
    :body [body :- MySchema]
    ...))
=>
(let [coercer (cache-coercion s/Int)]
  (context "/foo" req
    (GET "/" []
      :body [body :- coercer]
      ...))

Or perhaps an even more general optimization that would cache all sorts of things like merge-parameters and all the other args to routes/map->Route and the route itself, even under a dynamic context.

(let [rs (GET "/" []
             :body [body :- coercer]
             ...)]
  (context "/foo" req
    rs))