Skip to content

Commit bc1cab5

Browse files
committed
path-for doesn't lose the trailing path part. fixes #150
1 parent 68cf40c commit bc1cab5

File tree

3 files changed

+38
-25
lines changed

3 files changed

+38
-25
lines changed

src/compojure/api/routes.clj

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,28 @@
33
[clojure.string :as string]
44
[cheshire.core :as json]
55
[ring.swagger.swagger2 :as rss]
6-
[compojure.api.middleware :as mw]))
6+
[compojure.api.middleware :as mw]
7+
[clojure.string :as str]))
8+
9+
(defn- un-quote [s]
10+
(str/replace s #"^\"(.+(?=\"$))\"$" "$1"))
711

812
(defn ->path [s params]
9-
(->> s
10-
(re-seq #"(.*?):(.[^:|(/]*)([/]?)")
11-
(map (comp vec rest))
12-
(map #(update-in % [1] keyword))
13-
flatten
14-
(map (fn [token]
15-
(if (keyword? token)
16-
(string/replace
17-
(json/generate-string
18-
(or (token params)
19-
(throw
20-
(IllegalArgumentException.
21-
(str "Missing path-parameter "
22-
token " for path " s)))))
23-
#"^\"(.+(?=\"$))\"$"
24-
"$1")
25-
token)))
26-
(apply str)))
13+
(-> s
14+
(str/replace #":([^/]+)" " :$1 ")
15+
(str/split #" ")
16+
(->> (map
17+
(fn [[head :as token]]
18+
(if (= head \:)
19+
(let [key (keyword (subs token 1))
20+
value (key params)]
21+
(if value
22+
(un-quote (json/generate-string value))
23+
(throw
24+
(IllegalArgumentException.
25+
(str "Missing path-parameter " key " for path " s)))))
26+
token)))
27+
(apply str))))
2728

2829
(defn- duplicates [seq]
2930
(for [[id freq] (frequencies seq)

test/compojure/api/integration_test.clj

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,13 +806,13 @@
806806
(let [app (api (swagger-docs {:basePath "/serve/from/here"}))]
807807
(fact "override it"
808808
(let [[status spec] (get* app "/swagger.json")]
809-
status => 200
809+
status => 200
810810
(:basePath spec) => "/serve/from/here")))
811811

812812
(let [app (api (swagger-docs {:basePath "/"}))]
813813
(fact "can set it to the default"
814814
(let [[status spec] (get* app "/swagger.json")]
815-
status => 200
815+
status => 200
816816
(:basePath spec) => "/"))))
817817

818818
(fact "multiple different models with same name"
@@ -914,6 +914,18 @@
914914
body => {:country "FI"
915915
:zip 33200}))))
916916

917+
(fact "https://github.com/metosin/compojure-api/issues/150"
918+
(let [app (api
919+
(GET* "/companies/:company-id/refresh" []
920+
:path-params [company-id :- s/Int]
921+
:name :refresh-company
922+
:return String
923+
(ok (path-for :refresh-company {:company-id company-id}))))]
924+
(fact "path-for resolution"
925+
(let [[status body] (get* app "/companies/4/refresh")]
926+
status => 200
927+
body => "/companies/4/refresh"))))
928+
917929
(fact "multiple routes with same name fail at compile-time"
918930
(let [app' `(api
919931
(GET* "/api/pong" []

test/compojure/api/routes_test.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
=> (throws com.fasterxml.jackson.core.JsonGenerationException))
1414

1515
(fact "happy path"
16-
(->path "/a/:b/:c/d/:e" {:b (org.joda.time.LocalDate/parse "2015-05-22")
17-
:c 12345
18-
:e :kikka})
19-
=> "/a/2015-05-22/12345/d/kikka"))
16+
(->path "/a/:b/:c/d/:e/f" {:b (org.joda.time.LocalDate/parse "2015-05-22")
17+
:c 12345
18+
:e :kikka})
19+
=> "/a/2015-05-22/12345/d/kikka/f"))

0 commit comments

Comments
 (0)