From f87cd2f09f6a8a76540c726ad1c8f85d69a1217a Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sat, 17 Mar 2018 11:06:53 +0200 Subject: [PATCH] Support both Schema & Spec swagger --- .../src/reitit/coercion/schema.cljc | 24 +++++++-- .../reitit-swagger/src/reitit/swagger.cljc | 54 +++++++++++++------ project.clj | 4 +- 3 files changed, 61 insertions(+), 21 deletions(-) diff --git a/modules/reitit-schema/src/reitit/coercion/schema.cljc b/modules/reitit-schema/src/reitit/coercion/schema.cljc index 741f08a7..6843721a 100644 --- a/modules/reitit-schema/src/reitit/coercion/schema.cljc +++ b/modules/reitit-schema/src/reitit/coercion/schema.cljc @@ -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] diff --git a/modules/reitit-swagger/src/reitit/swagger.cljc b/modules/reitit-swagger/src/reitit/swagger.cljc index 5e875620..9883d8a8 100644 --- a/modules/reitit-swagger/src/reitit/swagger.cljc +++ b/modules/reitit-swagger/src/reitit/swagger.cljc @@ -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} diff --git a/project.clj b/project.clj index a0503927..9c57bacc 100644 --- a/project.clj +++ b/project.clj @@ -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"]