Skip to content

Commit fd9222d

Browse files
committed
Use new ring-swagger and test describing schemas
1 parent c2c97a8 commit fd9222d

File tree

7 files changed

+43
-42
lines changed

7 files changed

+43
-42
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Compojure-api
22

3-
[![Build Status](https://travis-ci.org/metosin/compojure-api.png?branch=0.7.0)](https://travis-ci.org/metosin/compojure-api)
3+
[![Build Status](https://travis-ci.org/metosin/compojure-api.png)](https://travis-ci.org/metosin/compojure-api)
44

55
Stuff on top of [Compojure](https://github.com/weavejester/compojure) for making sweet web apis.
66

@@ -286,7 +286,7 @@ you can also wrap models in containers (`vector` and `set`) and add extra metada
286286
```clojure
287287
(POST* "/echos" []
288288
:return [Thingie]
289-
:body [thingies #{Thingie} {:description "set on thingies"}]
289+
:body [thingies (describe #{Thingie} "set on thingies")]
290290
(ok thingies))
291291
```
292292

examples/src/examples/handler.clj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns examples.handler
22
(:require [compojure.api.sweet :refer :all]
33
[ring.util.http-response :refer :all]
4+
[ring.swagger.schema :refer [describe]]
45
[examples.dates :refer :all]
56
[examples.domain :refer :all]))
67

@@ -27,7 +28,8 @@
2728
:query [query QueryParams]
2829
(ok query))
2930
(GET* "/sum" []
30-
:query-params [x :- Long y :- Long]
31+
:query-params [x :- (describe Long "first param")
32+
y :- (describe Long "second param")]
3133
:summary "sums x & y query-parameters"
3234
(ok {:total (+ x y)}))
3335
(GET* "/times/:x/:y" []
@@ -58,7 +60,7 @@
5860
(ok (get-pizza id)))
5961
(POST* "/" []
6062
:return Pizza
61-
:body [pizza NewPizza {:description "new pizza"}]
63+
:body [pizza (describe NewPizza "new pizza")]
6264
:summary "Adds a pizza"
6365
:nickname "addPizza"
6466
(ok (add! pizza)))

examples/src/examples/thingie.clj

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns examples.thingie
22
(:require [ring.util.http-response :refer :all]
33
[compojure.api.sweet :refer :all]
4+
[ring.swagger.schema :refer [field describe]]
45
[schema.core :as s]))
56

67
;;
@@ -9,11 +10,13 @@
910

1011
(s/defschema Total {:total Long})
1112

12-
(s/defschema Thingie {:id Long
13-
:hot Boolean
13+
(s/defschema Thingie {:id (describe Long "A integer")
14+
:hot (describe Boolean "yes or no")
1415
:tag (s/enum :kikka :kukka)
15-
:chief [{:name String
16-
:type #{{:id String}}}]})
16+
:chief (describe
17+
[{:name String
18+
:type #{{:id String}}}]
19+
"An array")})
1720

1821
(s/defschema FlatThingie (dissoc Thingie :chief))
1922

@@ -38,13 +41,16 @@
3841

3942
(GET* "/plus" []
4043
:return Total
41-
:query-params [x :- Long {y :- Long 1}]
44+
;; You can add any keys to meta-data, but Swagger-ui might not show them
45+
:query-params [x :- (describe Long "description")
46+
{y :- Long 1}]
4247
:summary "x+y with query-parameters. y defaults to 1."
4348
(ok {:total (+ x y)}))
4449

4550
(POST* "/minus" []
4651
:return Total
47-
:body-params [x :- Long y :- Long]
52+
:body-params [x :- (describe Long "first param")
53+
y :- (describe Long "second param")]
4854
:summary "x-y with body-parameters."
4955
(ok {:total (- x y)}))
5056

@@ -63,15 +69,15 @@
6369
(swaggered "responses"
6470
:description "responses demo"
6571
(context "/responses" []
66-
(POST* "/number" []
67-
:return Total
68-
:query-params [x :- Long y :- Long]
69-
:responses {403 ^{:message "Underflow"} ErrorEnvelope}
70-
:summary "x-y with body-parameters."
71-
(let [total (- x y)]
72-
(if (>= total 0)
73-
(ok {:total (- x y)})
74-
(forbidden {:message "difference is negative"}))))))
72+
(POST* "/number" []
73+
:return Total
74+
:query-params [x :- Long y :- Long]
75+
:responses {403 ^{:message "Underflow"} ErrorEnvelope}
76+
:summary "x-y with body-parameters."
77+
(let [total (- x y)]
78+
(if (>= total 0)
79+
(ok {:total (- x y)})
80+
(forbidden {:message "difference is negative"}))))))
7581

7682
(swaggered "primitives"
7783
:description "returning primitive values"
@@ -91,7 +97,7 @@
9197

9298
(GET* "/hello" []
9399
:return String
94-
:query-params [name :- String]
100+
:query-params [name :- (describe String "foobar")]
95101
:notes "<h1>hello world.</h1>"
96102
:summary "echos a string from query-params"
97103
(ok (str "hello, " name)))))
@@ -108,7 +114,7 @@
108114

109115
(POST* "/body" []
110116
:return Thingie
111-
:body [thingie Thingie]
117+
:body [thingie (describe Thingie "The request body")]
112118
:summary "echoes a Thingie from json-body"
113119
(ok thingie))
114120

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
[compojure "1.1.8"]
1313
[prismatic/schema "0.2.4"]
1414
[metosin/ring-http-response "0.4.0"]
15-
[metosin/ring-swagger "0.10.6"]]
15+
[metosin/ring-swagger "0.11.0"]]
1616
:profiles {:thingie {:ring {:handler examples.thingie/app
1717
:reload-paths ["src" "examples/src"]}
1818
:source-paths ["examples/src"]

src/compojure/api/common.clj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,3 @@
6161
{}))
6262

6363
(def meta-container? #{#'meta-container})
64-
65-

src/compojure/api/meta.clj

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,29 +103,25 @@
103103
(update-in [:parameters :responseMessages] (comp distinct concat) messages)
104104
(update-in [:responses] merge responses))))
105105

106-
(defmethod restructure-param :body [_ [value model model-meta] acc]
106+
(defmethod restructure-param :body [_ [value model] acc]
107107
"reads body-params into a enchanced let. First parameter is the let symbol,
108-
second is the Model to coerced! against, third parameter is optional meta-
109-
data for the model. Examples:
110-
:body [user User]
111-
:body [user User {:key \"value\"}]"
108+
second is the Model to coerced! against.
109+
Examples:
110+
:body [user User]"
112111
(-> acc
113112
(update-in [:lets] into [value (src-coerce! model :body-params :json)])
114113
(update-in [:parameters :parameters] conj {:type :body
115-
:model model
116-
:meta model-meta})))
114+
:model model})))
117115

118-
(defmethod restructure-param :query [_ [value model model-meta] acc]
116+
(defmethod restructure-param :query [_ [value model] acc]
119117
"reads query-params into a enchanced let. First parameter is the let symbol,
120-
second is the Model to coerced! against, third parameter is optional meta-
121-
data for the model. Examples:
122-
:query [user User]
123-
:query [user User {:key \"value\"}]"
118+
second is the Model to coerced! against.
119+
Examples:
120+
:query [user User]"
124121
(-> acc
125122
(update-in [:lets] into [value (src-coerce! model :query-params :query)])
126123
(update-in [:parameters :parameters] conj {:type :query
127-
:model model
128-
:meta model-meta})))
124+
:model model})))
129125

130126
(defmethod restructure-param :body-params [_ body-params acc]
131127
"restructures body-params with plumbing letk notation. Example:

test/compojure/api/sweet_test.clj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[schema.core :as s]
44
[compojure.api.routes :as routes]
55
[ring.mock.request :refer :all]
6+
[ring.swagger.schema :refer [describe]]
67
[cheshire.core :as cheshire]
78
[compojure.core :as compojure]
89
[compojure.api.sweet :refer :all]))
@@ -37,7 +38,7 @@
3738
identity)
3839
(POST* "/bands" []
3940
:return Band
40-
:body [band [NewBand] {:description "new Band"}]
41+
:body [band [NewBand]]
4142
:summary "Adds a Band"
4243
:nickname "addBand"
4344
identity)
@@ -55,7 +56,6 @@
5556
identity))))
5657

5758
(facts "swaggered"
58-
5959
(fact "details are generated"
6060
((routes/get-routes) app-name)
6161

@@ -80,8 +80,7 @@
8080
:uri "/api/bands"
8181
:metadata {:nickname "addBand"
8282
:parameters [{:type :body
83-
:model [NewBand]
84-
:meta {:description "new Band"}}]
83+
:model [NewBand]}]
8584
:return Band
8685
:summary "Adds a Band"}}
8786
{:method :get

0 commit comments

Comments
 (0)