Skip to content

Commit 2ac78b6

Browse files
committed
routes are handled as nested vectors for AOT, passed as c-api options instead of swagger-data, read from options and transformed into a ordered map for swagger => keep routes in order & works with AOT. fixes #138
1 parent 6d83d4a commit 2ac78b6

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

src/compojure/api/core.clj

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
[compojure.api.routes :as routes]
66
[compojure.core :refer :all]
77
[potemkin :refer [import-vars]]
8-
[ring.swagger.middleware :as rsm]
98
[ring.swagger.common :refer [extract-parameters]]
109
[clojure.walk :as walk]
1110
backtick))
@@ -16,13 +15,11 @@
1615
lookup table via wrap-options. Returned handler retains the original
1716
meta-data."
1817
[handler options]
19-
(let [{:keys [routes lookup] :as meta}
20-
(-> handler meta (update-in [:routes] routes/route-vector-to-route-map))]
18+
(let [meta (-> handler meta (assoc :options options))]
2119
(-> handler
22-
(rsm/wrap-swagger-data routes)
2320
(mw/api-middleware options)
24-
(mw/wrap-options {:lookup lookup})
25-
(with-meta (assoc meta :options options)))))
21+
(mw/wrap-options (select-keys meta [:routes :lookup]))
22+
(with-meta meta))))
2623

2724
(defmacro api
2825
"Returns a ring handler wrapped in compojure.api.middleware/api-middlware.

src/compojure/api/swagger.clj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,12 @@
305305
:name ::swagger
306306
(let [runtime-info# (rsm/get-swagger-data request#)
307307
base-path# {:basePath (base-path request#)}
308-
options# (:ring-swagger (mw/get-options request#))]
308+
options# (:ring-swagger (mw/get-options request#))
309+
routes# (:routes (mw/get-options request#))
310+
paths# (routes/route-vector-to-route-map routes#)]
309311
(ok
310312
(let [swagger# (merge runtime-info#
313+
paths#
311314
base-path#
312315
~extra-info)
313316
result# (swagger2/swagger-json swagger# options#)]
@@ -340,7 +343,8 @@
340343
endpoint is requested. Returns either the (valid) api or throws an
341344
exception."
342345
[api]
343-
(let [{:keys [routes options]} (meta api)]
346+
(let [{:keys [routes options]} (meta api)
347+
routes (routes/route-vector-to-route-map routes)]
344348
(assert (not (nil? routes)) "Api did not contain route definitions.")
345349
(when (swagger-api? api)
346350

test/compojure/api/swagger_ordering_test.clj

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@
2626
(->> app
2727
meta
2828
:routes
29-
:paths
30-
keys) => ["/a/1"
31-
"/a/2"
32-
"/a/3"
33-
"/a/b/4"
34-
"/a/b/5"
35-
"/a/c/6"
36-
"/a/c/7"
37-
"/a/c/8"
38-
"/a/c/9"
39-
"/a/c/10"])))
29+
(map first)) => ["/a/1"
30+
"/a/2"
31+
"/a/3"
32+
"/a/b/4"
33+
"/a/b/5"
34+
"/a/c/6"
35+
"/a/c/7"
36+
"/a/c/8"
37+
"/a/c/9"
38+
"/a/c/10"])))

test/compojure/api/sweet_test.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
[ring.mock.request :refer :all]
66
[schema.core :as s]
77
[clojure.java.io :as io]
8-
[scjsv.core :as scjsv]))
8+
[scjsv.core :as scjsv]
9+
[compojure.api.routes :as routes]))
910

1011
(def validate
1112
(scjsv/validator (slurp (io/resource "ring/swagger/v2.0_schema.json"))))
@@ -71,7 +72,7 @@
7172
(facts "api documentation"
7273
(fact "details are generated"
7374

74-
(-> app meta :routes)
75+
(-> app meta :routes routes/route-vector-to-route-map)
7576

7677
=> {:paths {"/ping" {:get {}}
7778
"/api/ping" {:get {}}

0 commit comments

Comments
 (0)