+12
-1
@@ -10,6 +10,17 @@
|
|||||||
[tbd.health.routes :as health]
|
[tbd.health.routes :as health]
|
||||||
[tbd.math.routes :as math]))
|
[tbd.math.routes :as math]))
|
||||||
|
|
||||||
|
(defn- swagger-handler
|
||||||
|
"reitit emits `\"definitions\": null` when no route declares a schema, which
|
||||||
|
violates the Swagger 2.0 schema (must be an object) and makes validators reject the spec."
|
||||||
|
[]
|
||||||
|
(let [handler (swagger/create-swagger-handler)]
|
||||||
|
(fn [req]
|
||||||
|
(let [resp (handler req)]
|
||||||
|
(cond-> resp
|
||||||
|
(and (map? (:body resp)) (nil? (:definitions (:body resp))))
|
||||||
|
(update :body dissoc :definitions))))))
|
||||||
|
|
||||||
;; Each logical group (health, users, orders, ...) owns a `tbd.<group>.routes`
|
;; Each logical group (health, users, orders, ...) owns a `tbd.<group>.routes`
|
||||||
;; namespace that returns its own route subtree; they're concatenated here.
|
;; namespace that returns its own route subtree; they're concatenated here.
|
||||||
(def router
|
(def router
|
||||||
@@ -17,7 +28,7 @@
|
|||||||
[["/swagger.json"
|
[["/swagger.json"
|
||||||
{:get {:no-doc true
|
{:get {:no-doc true
|
||||||
:swagger {:info {:title "tbd API" :version "1.0.0"}}
|
:swagger {:info {:title "tbd API" :version "1.0.0"}}
|
||||||
:handler (swagger/create-swagger-handler)}}]
|
:handler (swagger-handler)}}]
|
||||||
["/" {:get {:no-doc true
|
["/" {:get {:no-doc true
|
||||||
:handler (fn [_req]
|
:handler (fn [_req]
|
||||||
{:status 200
|
{:status 200
|
||||||
|
|||||||
@@ -22,7 +22,9 @@
|
|||||||
(deftest swagger-spec-test
|
(deftest swagger-spec-test
|
||||||
(let [resp (request :get "/swagger.json")]
|
(let [resp (request :get "/swagger.json")]
|
||||||
(is (= 200 (:status resp)))
|
(is (= 200 (:status resp)))
|
||||||
(is (re-find #"/api/ping" (slurp (:body resp))))))
|
(is (re-find #"/api/ping" (slurp (:body resp))))
|
||||||
|
(is (not (re-find #"\"definitions\":null" (slurp (:body (request :get "/swagger.json")))))
|
||||||
|
"a null `definitions` makes the spec invalid Swagger 2.0")))
|
||||||
|
|
||||||
(deftest unknown-route-test
|
(deftest unknown-route-test
|
||||||
(is (= 404 (:status (request :get "/nope")))))
|
(is (= 404 (:status (request :get "/nope")))))
|
||||||
|
|||||||
Reference in New Issue
Block a user