Skip to content

Commit 4d0c8eb

Browse files
committed
Single map in endpoint denotes a response
1 parent ede30f1 commit 4d0c8eb

6 files changed

Lines changed: 43 additions & 8 deletions

File tree

src/compojure/api/api.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
" (:doc (meta #'compojure.api.middleware/api-middleware)))}
3838
api
3939
[& body]
40-
(let [[options handlers] (common/extract-parameters body)
40+
(let [[options handlers] (common/extract-parameters body false)
4141
options (rsc/deep-merge api-defaults options)
4242
handler (apply core/routes handlers)
4343
routes (routes/get-routes handler (:api options))

src/compojure/api/common.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
3. else => `{}`
1313
1414
Returns a tuple with parameters and body without the parameters"
15-
[c]
15+
[c expect-body]
1616
(cond
17-
(plain-map? (first c))
17+
(and (plain-map? (first c)) (or (not expect-body) (seq (rest c))))
1818
[(first c) (seq (rest c))]
1919

2020
(keyword? (first c))

src/compojure/api/meta.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@
318318
(seq responses) (assoc :responses (apply merge responses))))
319319

320320
(defn restructure [method [path arg & args] {:keys [context?]}]
321-
(let [[options body] (extract-parameters args)
321+
(let [[options body] (extract-parameters args true)
322322
[path-string lets arg-with-request arg] (destructure-compojure-api-request path arg)
323323

324324
{:keys [lets

src/compojure/api/swagger.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@
7676
(let [[path body] (if (string? (first body))
7777
[(first body) (rest body)]
7878
["/swagger.json" body])
79-
[extra-info] (extract-parameters body)]
8079
(GET path request
80+
[extra-info] (common/extract-parameters body false)]
8181
:no-doc true
8282
:name ::swagger
8383
(let [runtime-info (rsm/get-swagger-data request)

test/compojure/api/common_test.clj

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
(ns compojure.api.common-test
2-
(:require [compojure.api.common :refer :all]
2+
(:require [compojure.api.common :as common]
33
[midje.sweet :refer :all]))
44

55
(fact "group-with"
6-
(group-with pos? [1 -10 2 -4 -1 999]) => [[1 2 999] [-10 -4 -1]]
7-
(group-with pos? [1 2 999]) => [[1 2 999] nil])
6+
(common/group-with pos? [1 -10 2 -4 -1 999]) => [[1 2 999] [-10 -4 -1]]
7+
(common/group-with pos? [1 2 999]) => [[1 2 999] nil])
8+
9+
(fact "extract-parameters"
10+
11+
(facts "expect body"
12+
(common/extract-parameters [] true) => [{} nil]
13+
(common/extract-parameters [{:a 1}] true) => [{} [{:a 1}]]
14+
(common/extract-parameters [:a 1] true) => [{:a 1} nil]
15+
(common/extract-parameters [{:a 1} {:b 2}] true) => [{:a 1} [{:b 2}]]
16+
(common/extract-parameters [:a 1 {:b 2}] true) => [{:a 1} [{:b 2}]])
17+
18+
(facts "don't expect body"
19+
(common/extract-parameters [] false) => [{} nil]
20+
(common/extract-parameters [{:a 1}] false) => [{:a 1} nil]
21+
(common/extract-parameters [:a 1] false) => [{:a 1} nil]
22+
(common/extract-parameters [{:a 1} {:b 2}] false) => [{:a 1} [{:b 2}]]
23+
(common/extract-parameters [:a 1 {:b 2}] false) => [{:a 1} [{:b 2}]]))

test/compojure/api/integration_test.clj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@
7979
;; Facts
8080
;;
8181

82+
(facts "core routes"
83+
84+
(fact "keyword options"
85+
(let [route (GET "/ping" []
86+
:return String
87+
(ok "kikka"))]
88+
(route {:request-method :get :uri "/ping"}) => (contains {:body "kikka"})))
89+
90+
(fact "map options"
91+
(let [route (GET "/ping" []
92+
{:return String}
93+
(ok "kikka"))]
94+
(route {:request-method :get :uri "/ping"}) => (contains {:body "kikka"})))
95+
96+
(fact "map return"
97+
(let [route (GET "/ping" []
98+
{:body "kikka"})]
99+
(route {:request-method :get :uri "/ping"}) => (contains {:body "kikka"}))))
100+
82101
(facts "middleware ordering"
83102
(let [app (api
84103
(middleware [middleware* [middleware* 2]]

0 commit comments

Comments
 (0)