mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +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.utils :as su]
|
||||
[schema-tools.coerce :as stc]
|
||||
[schema-tools.swagger.core :as swagger]
|
||||
[reitit.coercion :as coercion]))
|
||||
|
||||
(def string-coercion-matcher
|
||||
|
|
@ -44,10 +45,25 @@
|
|||
(reify coercion/Coercion
|
||||
(-get-name [_] :schema)
|
||||
(-get-options [_] opts)
|
||||
(-get-apidocs [_ _ {:keys [parameters responses] :as info}]
|
||||
(cond-> (dissoc info :parameters :responses)
|
||||
parameters (assoc ::parameters parameters)
|
||||
responses (assoc ::responses responses)))
|
||||
(-get-apidocs [this type {:keys [parameters responses]}]
|
||||
(condp = type
|
||||
:swagger (merge
|
||||
(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)
|
||||
(-open-model [_ schema] (st/open-schema schema))
|
||||
(-encode-error [_ error]
|
||||
|
|
|
|||
|
|
@ -104,6 +104,7 @@
|
|||
(require '[reitit.swagger :as swagger])
|
||||
(require '[reitit.ring.coercion :as rrc])
|
||||
(require '[reitit.coercion.spec :as spec])
|
||||
(require '[reitit.coercion.schema :as schema])
|
||||
|
||||
(def app
|
||||
(ring/ring-handler
|
||||
|
|
@ -116,29 +117,52 @@
|
|||
:swagger {:info {:title "my-api"}}
|
||||
:handler swagger/swagger-spec-handler}}]
|
||||
|
||||
["/minus"
|
||||
{:get {:summary "minus"
|
||||
:parameters {:query {:x int?, :y int?}}
|
||||
:responses {200 {:body {:total int?}}}
|
||||
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||
{:status 200, :body {:total (- x y)}})}}]
|
||||
["/spec" {:coercion spec/coercion}
|
||||
|
||||
["/plus"
|
||||
{:get {:summary "plus"
|
||||
:parameters {:query {:x int?, :y int?}}
|
||||
:responses {200 {:body {:total int?}}}
|
||||
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||
{:status 200, :body {:total (+ x y)}})}}]]
|
||||
["/minus"
|
||||
{:get {:summary "minus"
|
||||
:parameters {:query {:x int?, :y int?}}
|
||||
:responses {200 {:body {:total int?}}}
|
||||
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||
{:status 200, :body {:total (- x y)}})}}]
|
||||
|
||||
["/plus"
|
||||
{:get {:summary "plus"
|
||||
:parameters {:query {:x int?, :y int?}}
|
||||
:responses {200 {:body {:total int?}}}
|
||||
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||
{: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
|
||||
rrc/coerce-exceptions-middleware
|
||||
rrc/coerce-request-middleware
|
||||
rrc/coerce-response-middleware]
|
||||
:coercion spec/coercion}})))
|
||||
rrc/coerce-response-middleware]}})))
|
||||
|
||||
(app
|
||||
{: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"}})
|
||||
; {:body {:total 3}, :status 200}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
[meta-merge "1.0.0"]
|
||||
[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"]
|
||||
[lein-doo "0.1.9"]
|
||||
|
|
@ -42,7 +42,7 @@
|
|||
|
||||
;; modules dependencies
|
||||
[metosin/reitit]
|
||||
[metosin/schema-tools "0.10.0"]
|
||||
[metosin/schema-tools]
|
||||
|
||||
[expound "0.5.0"]
|
||||
[orchestra "2017.11.12-1"]
|
||||
|
|
|
|||
Loading…
Reference in a new issue