mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +00:00
Tests for swagger
This commit is contained in:
parent
f87cd2f09f
commit
9a2a8a1f16
4 changed files with 164 additions and 105 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
129
test/cljc/reitit/swagger_test.clj
Normal file
129
test/cljc/reitit/swagger_test.clj
Normal file
|
|
@ -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)))))
|
||||
Loading…
Reference in a new issue