From 24c146a70294d811ebdde95c826ef28254c86df4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Heikki=20H=C3=A4m=C3=A4l=C3=A4inen?= Date: Wed, 21 Nov 2018 14:23:33 +0200 Subject: [PATCH] Add support for vector based specs in Swagger generation This allows data specs to be in vectors as well as maps. Before the change data spec [{:foo string?}] would throw exception when generating Swagger. --- .../reitit-spec/src/reitit/coercion/spec.cljc | 5 +++ test/cljc/reitit/swagger_test.clj | 43 +++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/modules/reitit-spec/src/reitit/coercion/spec.cljc b/modules/reitit-spec/src/reitit/coercion/spec.cljc index 9eff8bc6..44cf495a 100644 --- a/modules/reitit-spec/src/reitit/coercion/spec.cljc +++ b/modules/reitit-spec/src/reitit/coercion/spec.cljc @@ -53,6 +53,11 @@ (into-spec [this name] (dissoc (ds/spec (ensure-name name) this) :name)) + #?(:clj clojure.lang.PersistentVector + :cljs cljs.core.PersistentVector) + (into-spec [this name] + (dissoc (ds/spec (ensure-name name) this) :name)) + Maybe (into-spec [this name] (ds/spec (ensure-name name) this)) diff --git a/test/cljc/reitit/swagger_test.clj b/test/cljc/reitit/swagger_test.clj index b13f2571..4eefcb72 100644 --- a/test/cljc/reitit/swagger_test.clj +++ b/test/cljc/reitit/swagger_test.clj @@ -31,7 +31,17 @@ 500 {:description "fail"}} :handler (fn [{{{:keys [x y]} :query {:keys [z]} :path} :parameters}] - {:status 200, :body {:total (+ x y z)}})}}]] + {:status 200, :body {:total (+ x y z)}})} + :post {:summary "plus with body" + :parameters {:body [int?] + :path {:z int?}} + :swagger {:responses {400 {:schema {:type "string"} + :description "kosh"}}} + :responses {200 {:body {:total int?}} + 500 {:description "fail"}} + :handler (fn [{{{:keys [z]} :path + xs :body} :parameters}] + {:status 200, :body {:total (+ (reduce + xs) z)}})}}]] ["/schema" {:coercion schema/coercion} ["/plus/*z" @@ -58,7 +68,12 @@ (app {:request-method :get :uri "/api/spec/plus/3" - :query-params {:x "2", :y "1"}})))) + :query-params {:x "2", :y "1"}}))) + (is (= {:body {:total 7}, :status 200} + (app + {:request-method :post + :uri "/api/spec/plus/3" + :body-params [1 3]})))) (testing "schema" (is (= {:body {:total 6}, :status 200} (app @@ -126,7 +141,29 @@ 400 {:schema {:type "string"} :description "kosh"} 500 {:description "fail"}} - :summary "plus"}}}}] + :summary "plus"} + :post {:parameters [{:in "body", + :name "", + :description "", + :required true, + :schema {:type "array", + :items {:type "integer", + :format "int64"}}} + {:in "path" + :name "z" + :description "" + :type "integer" + :required true + :format "int64"}] + :responses {200 {:description "" + :schema {:properties {"total" {:format "int64" + :type "integer"}} + :required ["total"] + :type "object"}} + 400 {:schema {:type "string"} + :description "kosh"} + 500 {:description "fail"}} + :summary "plus with body"}}}}] (is (= expected spec)) (testing "ring-async swagger-spec"