Skip to content

Commit e5b5a49

Browse files
committed
Swagger via swagger-routes & api-options
swagger-ui and swagger-docs are still in sweet in this commit.
1 parent 4d0c8eb commit e5b5a49

4 files changed

Lines changed: 79 additions & 56 deletions

File tree

src/compojure/api/api.clj

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
(ns compojure.api.api
2-
(:require [compojure.api.core :as core]
2+
(:require [compojure.api.core :as c]
33
[compojure.api.swagger :as swagger]
44
[compojure.api.middleware :as middleware]
55
[compojure.api.routes :as routes]
66
[compojure.api.common :as common]
77
[ring.swagger.common :as rsc]
8-
[compojure.api.meta :as meta]))
8+
[compojure.api.meta :as meta]
9+
[ring.swagger.middleware :as rsm]))
910

1011
(def api-defaults
1112
(merge
1213
middleware/api-middleware-defaults
13-
{:api {:invalid-routes-fn routes/log-invalid-child-routes}}))
14+
{:api {:invalid-routes-fn routes/log-invalid-child-routes}
15+
:swagger nil}))
1416

1517
(defn
1618
^{:doc (str
@@ -31,6 +33,8 @@
3133
invalid routes (not satisfying compojure.api.route.Routing)
3234
setting value to nil ignores invalid routes completely.
3335
defaults to `compojure.api.routes/log-invalid-child-routes`
36+
- **:swagger** Options to configure the Swagger-routes. Defaults to nil.
37+
See `compojure.api.swagger/swagger-routes` for details.
3438
3539
### api-middleware options
3640
@@ -39,15 +43,17 @@
3943
[& body]
4044
(let [[options handlers] (common/extract-parameters body false)
4145
options (rsc/deep-merge api-defaults options)
42-
handler (apply core/routes handlers)
46+
handler (apply c/routes (concat handlers [(swagger/swagger-routes (:swagger options))]))
4347
routes (routes/get-routes handler (:api options))
4448
paths (-> routes routes/ring-swagger-paths swagger/transform-operations)
4549
lookup (routes/route-lookup-table routes)
50+
swagger-data (get-in options [:swagger :data])
4651
api-handler (-> handler
47-
(middleware/api-middleware (dissoc options :api))
52+
(cond-> swagger-data (rsm/wrap-swagger-data swagger-data))
53+
(middleware/api-middleware (dissoc options :api :swagger))
4854
(middleware/wrap-options {:paths paths
49-
:coercer (meta/memoized-coercer)
50-
:lookup lookup}))]
55+
:coercer (meta/memoized-coercer)
56+
:lookup lookup}))]
5157
(routes/create nil nil {} [handler] api-handler)))
5258

5359
(defmacro

src/compojure/api/swagger.clj

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(ns compojure.api.swagger
2-
(:require [compojure.api.common :refer :all]
3-
[compojure.api.core :refer [GET undocumented]]
4-
[compojure.api.common :refer [extract-parameters]]
2+
(:require [compojure.api.core :as c]
3+
[compojure.api.common :as common]
54
[compojure.api.middleware :as mw]
65
[ring.util.http-response :refer [ok]]
76
[ring.swagger.common :as rsc]
@@ -13,10 +12,6 @@
1312
[compojure.api.routes :as routes]
1413
[cheshire.core :as cheshire]))
1514

16-
;;
17-
;; generate schema names
18-
;;
19-
2015
#_(defn ensure-parameter-schema-names [endpoint]
2116
(if (get-in endpoint [:parameters :body])
2217
(update-in endpoint [:parameters :body] #(swagger/with-named-sub-schemas % "Body"))
@@ -33,51 +28,34 @@
3328
responses))))
3429
endpoint))
3530

36-
;;
37-
;; routes
38-
;;
31+
(defn- base-path [request]
32+
(let [context (swagger/context request)]
33+
(if (= "" context) "/" context)))
34+
35+
(defn- swagger-spec-path
36+
[app]
37+
(some-> app
38+
routes/get-routes
39+
routes/route-lookup-table
40+
::swagger
41+
keys
42+
first))
3943

4044
(defn transform-operations [swagger]
4145
(->> swagger
4246
(swagger2/transform-operations routes/non-nil-routes)
4347
(swagger2/transform-operations routes/strip-no-doc-endpoints)))
4448

45-
(defn base-path [request]
46-
(let [context (swagger/context request)]
47-
(if (= "" context) "/" context)))
48-
49-
;;
50-
;; Public api
51-
;;
52-
5349
(defn swagger-ui [& params]
54-
(undocumented
50+
(c/undocumented
5551
(apply rsui/swagger-ui params)))
5652

57-
(defn swagger-docs
58-
"Route to serve the swagger api-docs. If the first
59-
parameter is a String, it is used as a url for the
60-
api-docs, otherwise \"/swagger.json\" will be used.
61-
Next Keyword value pairs OR a map for meta-data.
62-
Meta-data can be any valid swagger 2.0 data. Common
63-
case is to introduce API Info and Tags here:
64-
65-
{:info {:version \"1.0.0\"
66-
:title \"Sausages\"
67-
:description \"Sausage description\"
68-
:termsOfService \"http://helloreverb.com/terms/\"
69-
:contact {:name \"My API Team\"
70-
:email \"foo@example.com\"
71-
:url \"http://www.metosin.fi\"}
72-
:license {:name: \"Eclipse Public License\"
73-
:url: \"http://www.eclipse.org/legal/epl-v10.html\"}}
74-
:tags [{:name \"sausages\", :description \"Sausage api-set}]}"
75-
[& body]
53+
(defn swagger-docs [& body]
7654
(let [[path body] (if (string? (first body))
7755
[(first body) (rest body)]
7856
["/swagger.json" body])
79-
(GET path request
8057
[extra-info] (common/extract-parameters body false)]
58+
(c/GET path request
8159
:no-doc true
8260
:name ::swagger
8361
(let [runtime-info (rsm/get-swagger-data request)
@@ -88,13 +66,51 @@
8866
spec (swagger2/swagger-json swagger options)]
8967
(ok spec)))))
9068

91-
(defn swagger-spec-path [app]
92-
(some-> app
93-
routes/get-routes
94-
routes/route-lookup-table
95-
::swagger
96-
keys
97-
first))
69+
;;
70+
;; Public api
71+
;;
72+
73+
(def swagger-defaults {:ui "/", :spec "/swagger.json"})
74+
75+
(defn swagger-routes
76+
"Returns routes for swagger-articats (ui & spec). Accepts an options map, with the
77+
following options:
78+
79+
**:ui** Uri for the swagger-ui (defaults to \"/\").
80+
Setting the value to nil will cause the swagger-ui not to be mounted
81+
82+
**:spec** Uri for the swagger-spec (defaults to \"/swagger.json\")
83+
Setting the value to nil will cause the swagger-ui not to be mounted
84+
85+
**:data** Swagger data in the Ring-Swagger format.
86+
87+
**:options**
88+
**:ui** Options to configure the ui
89+
**:spec** Options to configure the spec. Nada at the moment.
90+
91+
Example options:
92+
93+
{:ui \"/api-docs\"
94+
:spec \"/swagger.json\"
95+
:options {:ui {:jsonEditor true}
96+
:spec {}}
97+
:data {:info {:version \"1.0.0\"
98+
:title \"Sausages\"
99+
:description \"Sausage description\"
100+
:termsOfService \"http://helloreverb.com/terms/\"
101+
:contact {:name \"My API Team\"
102+
:email \"foo@example.com\"
103+
:url \"http://www.metosin.fi\"}
104+
:license {:name: \"Eclipse Public License\"
105+
:url: \"http://www.eclipse.org/legal/epl-v10.html\"}}
106+
:tags [{:name \"sausages\", :description \"Sausage api-set\"}]}}"
107+
([] (swagger-routes {}))
108+
([options]
109+
(if options
110+
(let [{:keys [ui spec data] {ui-options :ui spec-options :spec} :options} (merge swagger-defaults options)]
111+
(c/routes
112+
(if ui (apply swagger-ui ui (mapcat identity (merge ui-options (if spec {:swagger-docs spec})))))
113+
(if spec (apply swagger-docs spec (mapcat identity data))))))))
98114

99115
(defn validate
100116
"Validates a api. If the api is Swagger-enabled, the swagger-spec

src/compojure/api/sweet.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
[compojure.api.swagger
4040

4141
swagger-ui
42-
swagger-docs]
42+
swagger-docs
43+
swagger-routes]
4344

4445
[ring.swagger.json-schema
4546

test/compojure/api/integration_test.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,10 +949,10 @@
949949
(fact "swagger-spec-path"
950950
(fact "defaults to /swagger.json"
951951
(let [app (api (swagger-docs))]
952-
(swagger/swagger-spec-path app) => "/swagger.json"))
952+
(#'swagger/swagger-spec-path app) => "/swagger.json"))
953953
(fact "follows defined path"
954954
(let [app (api (swagger-docs "/api/api-docs/swagger.json"))]
955-
(swagger/swagger-spec-path app) => "/api/api-docs/swagger.json")))
955+
(#'swagger/swagger-spec-path app) => "/api/api-docs/swagger.json")))
956956

957957
(defrecord NonSwaggerRecord [data])
958958

0 commit comments

Comments
 (0)