mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 08:51:12 +00:00
Support both Schema & Spec swagger
This commit is contained in:
parent
b43c8cfed9
commit
f87cd2f09f
3 changed files with 61 additions and 21 deletions
|
|
@ -5,6 +5,7 @@
|
||||||
[schema.coerce :as sc]
|
[schema.coerce :as sc]
|
||||||
[schema.utils :as su]
|
[schema.utils :as su]
|
||||||
[schema-tools.coerce :as stc]
|
[schema-tools.coerce :as stc]
|
||||||
|
[schema-tools.swagger.core :as swagger]
|
||||||
[reitit.coercion :as coercion]))
|
[reitit.coercion :as coercion]))
|
||||||
|
|
||||||
(def string-coercion-matcher
|
(def string-coercion-matcher
|
||||||
|
|
@ -44,10 +45,25 @@
|
||||||
(reify coercion/Coercion
|
(reify coercion/Coercion
|
||||||
(-get-name [_] :schema)
|
(-get-name [_] :schema)
|
||||||
(-get-options [_] opts)
|
(-get-options [_] opts)
|
||||||
(-get-apidocs [_ _ {:keys [parameters responses] :as info}]
|
(-get-apidocs [this type {:keys [parameters responses]}]
|
||||||
(cond-> (dissoc info :parameters :responses)
|
(condp = type
|
||||||
parameters (assoc ::parameters parameters)
|
:swagger (merge
|
||||||
responses (assoc ::responses responses)))
|
(if parameters
|
||||||
|
{::swagger/parameters
|
||||||
|
(into
|
||||||
|
(empty parameters)
|
||||||
|
(for [[k v] parameters]
|
||||||
|
[k (coercion/-compile-model this v nil)]))})
|
||||||
|
(if responses
|
||||||
|
{::swagger/responses
|
||||||
|
(into
|
||||||
|
(empty responses)
|
||||||
|
(for [[k response] responses]
|
||||||
|
[k (update response :body #(coercion/-compile-model this % nil))]))}))
|
||||||
|
(throw
|
||||||
|
(ex-info
|
||||||
|
(str "Can't produce Schem apidocs for " type)
|
||||||
|
{:type type, :coercion :schema}))))
|
||||||
(-compile-model [_ model _] model)
|
(-compile-model [_ model _] model)
|
||||||
(-open-model [_ schema] (st/open-schema schema))
|
(-open-model [_ schema] (st/open-schema schema))
|
||||||
(-encode-error [_ error]
|
(-encode-error [_ error]
|
||||||
|
|
|
||||||
|
|
@ -104,6 +104,7 @@
|
||||||
(require '[reitit.swagger :as swagger])
|
(require '[reitit.swagger :as swagger])
|
||||||
(require '[reitit.ring.coercion :as rrc])
|
(require '[reitit.ring.coercion :as rrc])
|
||||||
(require '[reitit.coercion.spec :as spec])
|
(require '[reitit.coercion.spec :as spec])
|
||||||
|
(require '[reitit.coercion.schema :as schema])
|
||||||
|
|
||||||
(def app
|
(def app
|
||||||
(ring/ring-handler
|
(ring/ring-handler
|
||||||
|
|
@ -116,6 +117,8 @@
|
||||||
:swagger {:info {:title "my-api"}}
|
:swagger {:info {:title "my-api"}}
|
||||||
:handler swagger/swagger-spec-handler}}]
|
:handler swagger/swagger-spec-handler}}]
|
||||||
|
|
||||||
|
["/spec" {:coercion spec/coercion}
|
||||||
|
|
||||||
["/minus"
|
["/minus"
|
||||||
{:get {:summary "minus"
|
{:get {:summary "minus"
|
||||||
:parameters {:query {:x int?, :y int?}}
|
:parameters {:query {:x int?, :y int?}}
|
||||||
|
|
@ -130,15 +133,36 @@
|
||||||
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||||
{:status 200, :body {:total (+ x y)}})}}]]
|
{:status 200, :body {:total (+ x y)}})}}]]
|
||||||
|
|
||||||
|
["/schema" {:coercion schema/coercion}
|
||||||
|
|
||||||
|
["/minus"
|
||||||
|
{:get {:summary "minus"
|
||||||
|
:parameters {:query {:x Long, :y Long}}
|
||||||
|
:responses {200 {:body {:total Long}}}
|
||||||
|
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||||
|
{:status 200, :body {:total (- x y)}})}}]
|
||||||
|
|
||||||
|
["/plus"
|
||||||
|
{:get {:summary "plus"
|
||||||
|
:parameters {:query {:x Long, :y Long}}
|
||||||
|
:responses {200 {:body {:total Long}}}
|
||||||
|
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||||
|
{:status 200, :body {:total (+ x y)}})}}]]]
|
||||||
|
|
||||||
{:data {:middleware [swagger/swagger-feature
|
{:data {:middleware [swagger/swagger-feature
|
||||||
rrc/coerce-exceptions-middleware
|
rrc/coerce-exceptions-middleware
|
||||||
rrc/coerce-request-middleware
|
rrc/coerce-request-middleware
|
||||||
rrc/coerce-response-middleware]
|
rrc/coerce-response-middleware]}})))
|
||||||
:coercion spec/coercion}})))
|
|
||||||
|
|
||||||
(app
|
(app
|
||||||
{:request-method :get
|
{:request-method :get
|
||||||
:uri "/api/plus"
|
:uri "/api/spec/plus"
|
||||||
|
:query-params {:x "1", :y "2"}})
|
||||||
|
; {:body {:total 3}, :status 200}
|
||||||
|
|
||||||
|
(app
|
||||||
|
{:request-method :get
|
||||||
|
:uri "/api/schema/plus"
|
||||||
:query-params {:x "1", :y "2"}})
|
:query-params {:x "1", :y "2"}})
|
||||||
; {:body {:total 3}, :status 200}
|
; {:body {:total 3}, :status 200}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@
|
||||||
|
|
||||||
[meta-merge "1.0.0"]
|
[meta-merge "1.0.0"]
|
||||||
[metosin/spec-tools "0.6.1"]
|
[metosin/spec-tools "0.6.1"]
|
||||||
[metosin/schema-tools "0.10.0"]]
|
[metosin/schema-tools "0.10.1-SNAPSHOT"]]
|
||||||
|
|
||||||
:plugins [[jonase/eastwood "0.2.5"]
|
:plugins [[jonase/eastwood "0.2.5"]
|
||||||
[lein-doo "0.1.9"]
|
[lein-doo "0.1.9"]
|
||||||
|
|
@ -42,7 +42,7 @@
|
||||||
|
|
||||||
;; modules dependencies
|
;; modules dependencies
|
||||||
[metosin/reitit]
|
[metosin/reitit]
|
||||||
[metosin/schema-tools "0.10.0"]
|
[metosin/schema-tools]
|
||||||
|
|
||||||
[expound "0.5.0"]
|
[expound "0.5.0"]
|
||||||
[orchestra "2017.11.12-1"]
|
[orchestra "2017.11.12-1"]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue