From 9a2a8a1f16e89fbda0ca76822176995ad07b71c3 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sat, 17 Mar 2018 14:18:55 +0200 Subject: [PATCH] Tests for swagger --- .../src/reitit/coercion/schema.cljc | 31 +++-- .../reitit-spec/src/reitit/coercion/spec.cljc | 31 +++-- .../reitit-swagger/src/reitit/swagger.cljc | 78 +---------- test/cljc/reitit/swagger_test.clj | 129 ++++++++++++++++++ 4 files changed, 164 insertions(+), 105 deletions(-) create mode 100644 test/cljc/reitit/swagger_test.clj diff --git a/modules/reitit-schema/src/reitit/coercion/schema.cljc b/modules/reitit-schema/src/reitit/coercion/schema.cljc index 6843721a..9d9b706a 100644 --- a/modules/reitit-schema/src/reitit/coercion/schema.cljc +++ b/modules/reitit-schema/src/reitit/coercion/schema.cljc @@ -6,7 +6,8 @@ [schema.utils :as su] [schema-tools.coerce :as stc] [schema-tools.swagger.core :as swagger] - [reitit.coercion :as coercion])) + [reitit.coercion :as coercion] + [clojure.set :as set])) (def string-coercion-matcher stc/string-coercion-matcher) @@ -47,19 +48,21 @@ (-get-options [_] opts) (-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))]))})) + :swagger (swagger/swagger-spec + (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 + :let [response (set/rename-keys response {:body :schema})]] + [k (update response :schema #(coercion/-compile-model this % nil))]))}))) (throw (ex-info (str "Can't produce Schem apidocs for " type) diff --git a/modules/reitit-spec/src/reitit/coercion/spec.cljc b/modules/reitit-spec/src/reitit/coercion/spec.cljc index 4ecad5e4..ce905451 100644 --- a/modules/reitit-spec/src/reitit/coercion/spec.cljc +++ b/modules/reitit-spec/src/reitit/coercion/spec.cljc @@ -4,7 +4,8 @@ [spec-tools.data-spec :as ds] [spec-tools.conform :as conform] [spec-tools.swagger.core :as swagger] - [reitit.coercion :as coercion]) + [reitit.coercion :as coercion] + [clojure.set :as set]) #?(:clj (:import (spec_tools.core Spec)))) @@ -69,19 +70,21 @@ (-get-options [_] opts) (-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))]))})) + :swagger (swagger/swagger-spec + (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 + :let [response (set/rename-keys response {:body :schema})]] + [k (update response :schema #(coercion/-compile-model this % nil))]))}))) (throw (ex-info (str "Can't produce Spec apidocs for " type) diff --git a/modules/reitit-swagger/src/reitit/swagger.cljc b/modules/reitit-swagger/src/reitit/swagger.cljc index 9883d8a8..1347a17e 100644 --- a/modules/reitit-swagger/src/reitit/swagger.cljc +++ b/modules/reitit-swagger/src/reitit/swagger.cljc @@ -84,7 +84,7 @@ (if coercion (coercion/-get-apidocs coercion :swagger (-> e :data))) (-> e :data (select-keys [:tags :summary :description])) - (-> e :data :swagger))])))) + (-> e :data :swagger (dissoc :id)))])))) (seq) (into {}))])) (filter second) @@ -94,79 +94,3 @@ :body (meta-merge swagger {:paths paths})})))) - -;; -;; spike -;; - -(ns reitit.swagger.spike) -(require '[reitit.ring :as ring]) -(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 - (ring/router - ["/api" - {:swagger {:id ::math}} - - ["/swagger.json" - {:get {:no-doc true - :swagger {:info {:title "my-api"}} - :handler swagger/swagger-spec-handler}}] - - ["/spec" {:coercion spec/coercion} - - ["/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]}}))) - -(app - {:request-method :get - :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} - -(app - {:request-method :get - :uri "/api/swagger.json"}) -; ... swagger-spec for "/api/minus" & "/api/plus" diff --git a/test/cljc/reitit/swagger_test.clj b/test/cljc/reitit/swagger_test.clj new file mode 100644 index 00000000..8edcd777 --- /dev/null +++ b/test/cljc/reitit/swagger_test.clj @@ -0,0 +1,129 @@ +(ns reitit.swagger-test + (:require [clojure.test :refer :all] + [reitit.ring :as ring] + [reitit.swagger :as swagger] + [reitit.ring.coercion :as rrc] + [reitit.coercion.spec :as spec] + [reitit.coercion.schema :as schema])) + +(def app + (ring/ring-handler + (ring/router + ["/api" + {:swagger {:id ::math}} + + ["/swagger.json" + {:get {:no-doc true + :swagger {:info {:title "my-api"}} + :handler swagger/swagger-spec-handler}}] + + ["/spec" {:coercion spec/coercion} + + ["/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]}}))) + +(deftest swagger-test + (testing "endpoints work" + (testing "spec" + (is (= {:body {:total 3}, :status 200} + (app + {:request-method :get + :uri "/api/spec/plus" + :query-params {:x "2", :y "1"}}))) + (is (= {:body {:total 1}, :status 200} + (app + {:request-method :get + :uri "/api/spec/minus" + :query-params {:x "2", :y "1"}})))) + (testing "schema" + (is (= {:body {:total 3}, :status 200} + (app + {:request-method :get + :uri "/api/schema/plus" + :query-params {:x "2", :y "1"}}))) + (is (= {:body {:total 1}, :status 200} + (app + {:request-method :get + :uri "/api/schema/minus" + :query-params {:x "2", :y "1"}}))))) + (testing "swagger-spec" + (let [spec (:body (app + {:request-method :get + :uri "/api/swagger.json"}))] + (is (= {:x-id ::math + :info {:title "my-api"} + :paths { + ;; schema doesn't yet generate parameter data + "/api/schema/minus" {:get {:summary "minus"}} + "/api/schema/plus" {:get {:summary "plus"}} + + ;; spec does! + "/api/spec/minus" {:get {:parameters [{:description "" + :format "int64" + :in "query" + :name "x" + :required true + :type "integer"} + {:description "" + :format "int64" + :in "query" + :name "y" + :required true + :type "integer"}] + :responses {200 {:description "" + :schema {:properties {"total" {:format "int64" + :type "integer"}} + :required ["total"] + :type "object"}}} + :summary "minus"}} + "/api/spec/plus" {:get {:parameters [{:description "" + :format "int64" + :in "query" + :name "x" + :required true + :type "integer"} + {:description "" + :format "int64" + :in "query" + :name "y" + :required true + :type "integer"}] + :responses {200 {:description "" + :schema {:properties {"total" {:format "int64" + :type "integer"}} + :required ["total"] + :type "object"}}} + :summary "plus"}}}} + spec)))))