Skip to content

Commit

Permalink
Add a full-route key to the request map
Browse files Browse the repository at this point in the history
This provides access to the full route that contains the common prefix.
The full-route info is added to the :compojure/full-route key in the
request map.

We use :compojure/route to get the route info and add it to the
attributes of our metrics and traces, but when compojure.core/context
is used, we are not able to get the parameters that are not
instantiated. This change adds :compojure/full-route key that serves
similarly as the existing :compojure/route does - the only difference
is that the new key has a common prefix.

Co-authored-by: Liam Chen <[email protected]>
Co-authored-by: Claire Alvis <[email protected]>
  • Loading branch information
liamchzh and calvis committed May 11, 2022
1 parent d8f39af commit 39c4a7d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
31 changes: 21 additions & 10 deletions src/compojure/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,20 @@
(handler request respond raise)))))

(defn- wrap-route-info [handler route-info]
(fn
([request]
(handler (assoc request :compojure/route route-info)))
([request respond raise]
(handler (assoc request :compojure/route route-info) respond raise))))
(let [[_ path] route-info]
(fn
([request]
(let [full-route (str (:compojure/route-context request) path)]
(handler (assoc request
:compojure/route route-info
:compojure/full-route full-route))))
([request respond raise]
(let [full-route (str (:compojure/route-context request) path)]
(handler (assoc request
:compojure/route route-info
:compojure/full-route full-route)
respond
raise))))))

(defn- wrap-route-matches [handler method path]
(fn
Expand Down Expand Up @@ -251,7 +260,7 @@
(defn- remove-suffix [path suffix]
(subs path 0 (- (count path) (count suffix))))

(defn- context-request [request route]
(defn- context-request [request route context-path]
(if-let [params (clout/route-matches route request)]
(let [uri (:uri request)
path (:path-info request uri)
Expand All @@ -261,7 +270,8 @@
(-> request
(assoc-route-params (decode-route-params params))
(assoc :path-info (if (= subpath "") "/" subpath)
:context (remove-suffix uri subpath))))))
:context (remove-suffix uri subpath))
(update :compojure/route-context str context-path)))))

(defn- context-route [route]
(let [re-context {:__path-info #"|/.*"}]
Expand All @@ -279,7 +289,7 @@
:else
`(clout/route-compile (str ~route ":__path-info") ~re-context))))

(defn ^:no-doc make-context [route make-handler]
(defn ^:no-doc make-context [route path make-handler]
(letfn [(handler
([request]
(when-let [context-handler (make-handler request)]
Expand All @@ -292,10 +302,10 @@
handler
(fn
([request]
(if-let [request (context-request request route)]
(if-let [request (context-request request route path)]
(handler request)))
([request respond raise]
(if-let [request (context-request request route)]
(if-let [request (context-request request route path)]
(handler request respond raise)
(respond nil)))))))

Expand All @@ -311,6 +321,7 @@
[path args & routes]
`(make-context
~(context-route path)
~path
(fn [request#]
(let-request [~args request#]
(routes ~@routes)))))
Expand Down
30 changes: 28 additions & 2 deletions test/compojure/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
(assoc :params {:y "bar"}))]
((GET "/:x" [x :as r]
(is (= x "foo"))
(is (= (dissoc r :params :route-params :compojure/route)
(is (= (dissoc r
:params
:route-params
:compojure/route
:compojure/full-route)
(dissoc req :params)))
nil)
req)))
Expand Down Expand Up @@ -323,7 +327,29 @@
request (route (mock/request :post "/foo/1" {}))]
(testing "ANY request has matched route information"
(is (= (request :compojure/route)
[:any "/foo/:id"])))))
[:any "/foo/:id"]))
(is (= (request :compojure/full-route)
"/foo/:id"))))

(let [route (context "/foo/:foo-id" [_] (GET "/bar/:bar-id" req req))
request (route (mock/request :get "/foo/1/bar/2"))]
(testing "request has matched route information with path prefix"
(is (= (request :compojure/route)
[:get "/bar/:bar-id"]))
(is (= (request :compojure/route-context)
"/foo/:foo-id"))
(is (= (request :compojure/full-route)
"/foo/:foo-id/bar/:bar-id"))))

(let [route (context "/foo/:foo-id" [_]
(context "/bar/:bar-id" [_]
(GET "/baz/:baz-id" req req)))
request (route (mock/request :get "/foo/1/bar/2/baz/3"))]
(testing "request has matched route information with multiple path prefix"
(is (= (request :compojure/route-context)
"/foo/:foo-id/bar/:bar-id"))
(is (= (request :compojure/full-route)
"/foo/:foo-id/bar/:bar-id/baz/:baz-id")))))

(deftest route-async-test
(testing "single route"
Expand Down

0 comments on commit 39c4a7d

Please sign in to comment.