2022-08-30 16:24:21 +00:00
|
|
|
(ns reitit.openapi-test
|
2023-03-06 13:14:49 +00:00
|
|
|
(:require [clojure.java.shell :as shell]
|
|
|
|
|
[clojure.test :refer [deftest is testing]]
|
2023-02-16 08:26:54 +00:00
|
|
|
[jsonista.core :as j]
|
|
|
|
|
[matcher-combinators.test :refer [match?]]
|
2023-03-06 07:12:56 +00:00
|
|
|
[matcher-combinators.matchers :as matchers]
|
2022-08-30 16:24:21 +00:00
|
|
|
[muuntaja.core :as m]
|
|
|
|
|
[reitit.coercion.malli :as malli]
|
|
|
|
|
[reitit.coercion.schema :as schema]
|
|
|
|
|
[reitit.coercion.spec :as spec]
|
2023-03-15 08:22:26 +00:00
|
|
|
[reitit.http.interceptors.multipart]
|
2022-08-30 16:24:21 +00:00
|
|
|
[reitit.openapi :as openapi]
|
|
|
|
|
[reitit.ring :as ring]
|
2023-03-08 12:11:28 +00:00
|
|
|
[reitit.ring.spec]
|
2022-08-30 16:24:21 +00:00
|
|
|
[reitit.ring.coercion :as rrc]
|
|
|
|
|
[reitit.swagger-ui :as swagger-ui]
|
|
|
|
|
[schema.core :as s]
|
|
|
|
|
[spec-tools.data-spec :as ds]))
|
|
|
|
|
|
2023-03-06 13:14:49 +00:00
|
|
|
(defn validate
|
|
|
|
|
"Returns nil if data is a valid openapi spec, otherwise validation result"
|
|
|
|
|
[data]
|
|
|
|
|
(let [file (java.io.File/createTempFile "reitit-openapi" ".json")]
|
|
|
|
|
(.deleteOnExit file)
|
|
|
|
|
(spit file (j/write-value-as-string data))
|
|
|
|
|
(let [result (shell/sh "npx" "-p" "@seriousme/openapi-schema-validator" "validate-api" (.getPath file))]
|
|
|
|
|
(when-not (zero? (:exit result))
|
|
|
|
|
(j/read-value (:out result))))))
|
|
|
|
|
|
2022-08-30 16:24:21 +00:00
|
|
|
(def app
|
|
|
|
|
(ring/ring-handler
|
|
|
|
|
(ring/router
|
|
|
|
|
["/api"
|
|
|
|
|
{:openapi {:id ::math}}
|
|
|
|
|
|
|
|
|
|
["/openapi.json"
|
|
|
|
|
{:get {:no-doc true
|
2023-03-06 13:14:49 +00:00
|
|
|
:openapi {:info {:title "my-api"
|
|
|
|
|
:version "0.0.1"}}
|
2022-08-30 16:24:21 +00:00
|
|
|
:handler (openapi/create-openapi-handler)}}]
|
|
|
|
|
|
2023-02-15 14:23:59 +00:00
|
|
|
["/spec" {:coercion spec/coercion}
|
2022-08-30 16:24:21 +00:00
|
|
|
["/plus/:z"
|
2023-02-15 14:23:59 +00:00
|
|
|
{:get {:summary "plus"
|
2023-03-08 08:12:39 +00:00
|
|
|
:tags [:plus :spec]
|
2022-08-30 16:24:21 +00:00
|
|
|
:parameters {:query {:x int?, :y int?}
|
|
|
|
|
:path {:z int?}}
|
2023-03-08 08:12:39 +00:00
|
|
|
:openapi {:operationId "spec-plus"
|
|
|
|
|
:deprecated true
|
|
|
|
|
:responses {400 {:description "kosh"
|
2022-08-30 16:24:21 +00:00
|
|
|
:content {"application/json" {:schema {:type "string"}}}}}}
|
2023-03-06 13:14:49 +00:00
|
|
|
:responses {200 {:description "success"
|
|
|
|
|
:body {:total int?}}
|
2022-08-30 16:24:21 +00:00
|
|
|
500 {:description "fail"}}
|
|
|
|
|
:handler (fn [{{{:keys [x y]} :query
|
|
|
|
|
{:keys [z]} :path} :parameters}]
|
|
|
|
|
{:status 200, :body {:total (+ x y z)}})}
|
|
|
|
|
:post {:summary "plus with body"
|
|
|
|
|
:parameters {:body (ds/maybe [int?])
|
|
|
|
|
:path {:z int?}}
|
|
|
|
|
:openapi {:responses {400 {:content {"application/json" {:schema {:type "string"}}}
|
|
|
|
|
:description "kosh"}}}
|
2023-03-06 13:14:49 +00:00
|
|
|
:responses {200 {:description "success"
|
|
|
|
|
:body {:total int?}}
|
2022-08-30 16:24:21 +00:00
|
|
|
500 {:description "fail"}}
|
|
|
|
|
:handler (fn [{{{:keys [z]} :path
|
|
|
|
|
xs :body} :parameters}]
|
|
|
|
|
{:status 200, :body {:total (+ (reduce + xs) z)}})}}]]
|
|
|
|
|
|
|
|
|
|
["/malli" {:coercion malli/coercion}
|
|
|
|
|
["/plus/*z"
|
|
|
|
|
{:get {:summary "plus"
|
2023-03-08 08:12:39 +00:00
|
|
|
:tags [:plus :malli]
|
2022-08-30 16:24:21 +00:00
|
|
|
:parameters {:query [:map [:x int?] [:y int?]]
|
|
|
|
|
:path [:map [:z int?]]}
|
|
|
|
|
:openapi {:responses {400 {:description "kosh"
|
|
|
|
|
:content {"application/json" {:schema {:type "string"}}}}}}
|
2023-03-06 13:14:49 +00:00
|
|
|
:responses {200 {:description "success"
|
|
|
|
|
:body [:map [:total int?]]}
|
2022-08-30 16:24:21 +00:00
|
|
|
500 {:description "fail"}}
|
|
|
|
|
:handler (fn [{{{:keys [x y]} :query
|
|
|
|
|
{:keys [z]} :path} :parameters}]
|
|
|
|
|
{:status 200, :body {:total (+ x y z)}})}
|
|
|
|
|
:post {:summary "plus with body"
|
|
|
|
|
:parameters {:body [:maybe [:vector int?]]
|
|
|
|
|
:path [:map [:z int?]]}
|
|
|
|
|
:openapi {:responses {400 {:description "kosh"
|
|
|
|
|
:content {"application/json" {:schema {:type "string"}}}}}}
|
2023-03-06 13:14:49 +00:00
|
|
|
:responses {200 {:description "success"
|
|
|
|
|
:body [:map [:total int?]]}
|
2022-08-30 16:24:21 +00:00
|
|
|
500 {:description "fail"}}
|
|
|
|
|
:handler (fn [{{{:keys [z]} :path
|
|
|
|
|
xs :body} :parameters}]
|
|
|
|
|
{:status 200, :body {:total (+ (reduce + xs) z)}})}}]]
|
|
|
|
|
|
|
|
|
|
["/schema" {:coercion schema/coercion}
|
|
|
|
|
["/plus/*z"
|
|
|
|
|
{:get {:summary "plus"
|
2023-03-08 08:12:39 +00:00
|
|
|
:tags [:plus :schema]
|
2022-08-30 16:24:21 +00:00
|
|
|
:parameters {:query {:x s/Int, :y s/Int}
|
|
|
|
|
:path {:z s/Int}}
|
|
|
|
|
:openapi {:responses {400 {:content {"application/json" {:schema {:type "string"}}}
|
|
|
|
|
:description "kosh"}}}
|
2023-03-06 13:14:49 +00:00
|
|
|
:responses {200 {:description "success"
|
|
|
|
|
:body {:total s/Int}}
|
2022-08-30 16:24:21 +00:00
|
|
|
500 {:description "fail"}}
|
|
|
|
|
:handler (fn [{{{:keys [x y]} :query
|
|
|
|
|
{:keys [z]} :path} :parameters}]
|
|
|
|
|
{:status 200, :body {:total (+ x y z)}})}
|
|
|
|
|
:post {:summary "plus with body"
|
|
|
|
|
:parameters {:body (s/maybe [s/Int])
|
|
|
|
|
:path {:z s/Int}}
|
|
|
|
|
:openapi {:responses {400 {:content {"application/json" {:schema {:type "string"}}}
|
|
|
|
|
:description "kosh"}}}
|
2023-03-06 13:14:49 +00:00
|
|
|
:responses {200 {:description "success"
|
|
|
|
|
:body {:total s/Int}}
|
2022-08-30 16:24:21 +00:00
|
|
|
500 {:description "fail"}}
|
|
|
|
|
:handler (fn [{{{:keys [z]} :path
|
|
|
|
|
xs :body} :parameters}]
|
|
|
|
|
{:status 200, :body {:total (+ (reduce + xs) z)}})}}]]]
|
|
|
|
|
|
2023-03-08 12:11:28 +00:00
|
|
|
{:validate reitit.ring.spec/validate
|
|
|
|
|
:data {:middleware [openapi/openapi-feature
|
2022-08-30 16:24:21 +00:00
|
|
|
rrc/coerce-exceptions-middleware
|
|
|
|
|
rrc/coerce-request-middleware
|
|
|
|
|
rrc/coerce-response-middleware]}})))
|
|
|
|
|
|
|
|
|
|
(deftest openapi-test
|
|
|
|
|
(testing "endpoints work"
|
|
|
|
|
(testing "malli"
|
|
|
|
|
(is (= {:body {:total 6}, :status 200}
|
|
|
|
|
(app {:request-method :get
|
|
|
|
|
:uri "/api/malli/plus/3"
|
|
|
|
|
:query-params {:x "2", :y "1"}})))
|
|
|
|
|
(is (= {:body {:total 7}, :status 200}
|
|
|
|
|
(app {:request-method :post
|
|
|
|
|
:uri "/api/malli/plus/3"
|
|
|
|
|
:body-params [1 3]})))))
|
|
|
|
|
(testing "openapi-spec"
|
|
|
|
|
(let [spec (:body (app {:request-method :get
|
|
|
|
|
:uri "/api/openapi.json"}))
|
|
|
|
|
expected {:x-id #{::math}
|
|
|
|
|
:openapi "3.1.0"
|
2023-03-06 13:14:49 +00:00
|
|
|
:info {:title "my-api"
|
|
|
|
|
:version "0.0.1"}
|
2023-02-15 14:23:59 +00:00
|
|
|
:paths {"/api/spec/plus/{z}" {:get {:parameters [{:in "query"
|
|
|
|
|
:name "x"
|
|
|
|
|
:description ""
|
|
|
|
|
:required true
|
|
|
|
|
:schema {:type "integer"
|
|
|
|
|
:format "int64"}}
|
|
|
|
|
{:in "query"
|
|
|
|
|
:name "y"
|
|
|
|
|
:description ""
|
|
|
|
|
:required true
|
|
|
|
|
:schema {:type "integer"
|
|
|
|
|
:format "int64"}}
|
|
|
|
|
{:in "path"
|
|
|
|
|
:name "z"
|
|
|
|
|
:description ""
|
|
|
|
|
:required true
|
|
|
|
|
:schema {:type "integer"
|
|
|
|
|
:format "int64"}}]
|
2023-03-06 13:14:49 +00:00
|
|
|
:responses {200 {:description "success"
|
|
|
|
|
:content {"application/json" {:schema {:type "object"
|
2023-02-15 14:23:59 +00:00
|
|
|
:properties {"total" {:format "int64"
|
|
|
|
|
:type "integer"}}
|
|
|
|
|
:required ["total"]}}}}
|
|
|
|
|
400 {:description "kosh"
|
|
|
|
|
:content {"application/json" {:schema {:type "string"}}}}
|
|
|
|
|
500 {:description "fail"}}
|
2023-03-08 08:12:39 +00:00
|
|
|
:operationId "spec-plus"
|
|
|
|
|
:deprecated true
|
|
|
|
|
:tags [:plus :spec]
|
2023-02-15 14:23:59 +00:00
|
|
|
:summary "plus"}
|
|
|
|
|
:post {:parameters [{:in "path"
|
|
|
|
|
:name "z"
|
|
|
|
|
:required true
|
|
|
|
|
:description ""
|
|
|
|
|
:schema {:type "integer"
|
|
|
|
|
:format "int64"}}]
|
|
|
|
|
:requestBody {:content {"application/json" {:schema {:oneOf [{:items {:type "integer"
|
|
|
|
|
:format "int64"}
|
|
|
|
|
:type "array"}
|
|
|
|
|
{:type "null"}]}}}}
|
2023-03-06 13:14:49 +00:00
|
|
|
:responses {200 {:description "success"
|
|
|
|
|
:content {"application/json" {:schema {:properties {"total" {:format "int64"
|
2023-02-15 14:23:59 +00:00
|
|
|
:type "integer"}}
|
|
|
|
|
:required ["total"]
|
|
|
|
|
:type "object"}}}}
|
|
|
|
|
400 {:content {"application/json" {:schema {:type "string"}}}
|
|
|
|
|
:description "kosh"}
|
|
|
|
|
500 {:description "fail"}}
|
|
|
|
|
:summary "plus with body"}}
|
2022-08-30 16:24:21 +00:00
|
|
|
"/api/malli/plus/{z}" {:get {:parameters [{:in "query"
|
|
|
|
|
:name :x
|
|
|
|
|
:required true
|
|
|
|
|
:schema {:type "integer"}}
|
|
|
|
|
{:in "query"
|
|
|
|
|
:name :y
|
|
|
|
|
:required true
|
|
|
|
|
:schema {:type "integer"}}
|
|
|
|
|
{:in "path"
|
|
|
|
|
:name :z
|
|
|
|
|
:required true
|
|
|
|
|
:schema {:type "integer"}}]
|
2023-03-06 13:14:49 +00:00
|
|
|
:responses {200 {:description "success"
|
|
|
|
|
:content {"application/json" {:schema {:type "object"
|
2022-08-30 16:24:21 +00:00
|
|
|
:properties {:total {:type "integer"}}
|
2023-02-15 14:07:23 +00:00
|
|
|
:additionalProperties false
|
2022-08-30 16:24:21 +00:00
|
|
|
:required [:total]}}}}
|
|
|
|
|
400 {:description "kosh"
|
|
|
|
|
:content {"application/json" {:schema {:type "string"}}}}
|
|
|
|
|
500 {:description "fail"}}
|
2023-03-08 08:12:39 +00:00
|
|
|
:tags [:plus :malli]
|
2022-08-30 16:24:21 +00:00
|
|
|
:summary "plus"}
|
|
|
|
|
:post {:parameters [{:in "path"
|
|
|
|
|
:name :z
|
|
|
|
|
:schema {:type "integer"}
|
|
|
|
|
:required true}]
|
|
|
|
|
:requestBody {:content {"application/json" {:schema {:oneOf [{:items {:type "integer"}
|
|
|
|
|
:type "array"}
|
|
|
|
|
{:type "null"}]}}}}
|
2023-03-06 13:14:49 +00:00
|
|
|
:responses {200 {:description "success"
|
|
|
|
|
:content {"application/json" {:schema {:properties {:total {:type "integer"}}
|
2022-08-30 16:24:21 +00:00
|
|
|
:required [:total]
|
2023-02-15 14:07:23 +00:00
|
|
|
:additionalProperties false
|
2022-08-30 16:24:21 +00:00
|
|
|
:type "object"}}}}
|
|
|
|
|
400 {:description "kosh"
|
|
|
|
|
:content {"application/json" {:schema {:type "string"}}}}
|
|
|
|
|
500 {:description "fail"}}
|
|
|
|
|
:summary "plus with body"}}
|
|
|
|
|
"/api/schema/plus/{z}" {:get {:parameters [{:description ""
|
|
|
|
|
:in "query"
|
|
|
|
|
:name "x"
|
|
|
|
|
:required true
|
|
|
|
|
:schema {:format "int32"
|
|
|
|
|
:type "integer"}}
|
|
|
|
|
{:description ""
|
|
|
|
|
:in "query"
|
|
|
|
|
:name "y"
|
|
|
|
|
:required true
|
|
|
|
|
:schema {:type "integer"
|
|
|
|
|
:format "int32"}}
|
|
|
|
|
{:in "path"
|
|
|
|
|
:name "z"
|
|
|
|
|
:description ""
|
|
|
|
|
:required true
|
|
|
|
|
:schema {:type "integer"
|
|
|
|
|
:format "int32"}}]
|
2023-03-06 13:14:49 +00:00
|
|
|
:responses {200 {:description "success"
|
|
|
|
|
:content {"application/json" {:schema {:additionalProperties false
|
2022-08-30 16:24:21 +00:00
|
|
|
:properties {"total" {:format "int32"
|
|
|
|
|
:type "integer"}}
|
|
|
|
|
:required ["total"]
|
|
|
|
|
:type "object"}}}}
|
|
|
|
|
400 {:description "kosh"
|
|
|
|
|
:content {"application/json" {:schema {:type "string"}}}}
|
|
|
|
|
500 {:description "fail"}}
|
2023-03-08 08:12:39 +00:00
|
|
|
:tags [:plus :schema]
|
2022-08-30 16:24:21 +00:00
|
|
|
:summary "plus"}
|
|
|
|
|
:post {:parameters [{:in "path"
|
|
|
|
|
:name "z"
|
|
|
|
|
:description ""
|
|
|
|
|
:required true
|
|
|
|
|
:schema {:type "integer"
|
|
|
|
|
:format "int32"}}]
|
|
|
|
|
:requestBody {:content {"application/json" {:schema {:oneOf [{:type "array"
|
|
|
|
|
:items {:type "integer"
|
|
|
|
|
:format "int32"}}
|
|
|
|
|
{:type "null"}]}}}}
|
2023-03-06 13:14:49 +00:00
|
|
|
:responses {200 {:description "success"
|
|
|
|
|
:content {"application/json" {:schema {:properties {"total" {:format "int32"
|
2022-08-30 16:24:21 +00:00
|
|
|
:type "integer"}}
|
|
|
|
|
:additionalProperties false
|
|
|
|
|
:required ["total"]
|
|
|
|
|
:type "object"}}}}
|
|
|
|
|
400 {:description "kosh"
|
|
|
|
|
:content {"application/json" {:schema {:type "string"}}}}
|
|
|
|
|
500 {:description "fail"}}
|
|
|
|
|
:summary "plus with body"}}}}]
|
2023-03-06 13:14:49 +00:00
|
|
|
(is (= expected spec))
|
|
|
|
|
(is (nil? (validate spec))))))
|
2022-08-30 16:24:21 +00:00
|
|
|
|
|
|
|
|
(defn spec-paths [app uri]
|
|
|
|
|
(-> {:request-method :get, :uri uri} app :body :paths keys))
|
|
|
|
|
|
|
|
|
|
(deftest multiple-openapi-apis-test
|
|
|
|
|
(let [ping-route ["/ping" {:get (constantly "ping")}]
|
|
|
|
|
spec-route ["/openapi.json"
|
|
|
|
|
{:get {:no-doc true
|
|
|
|
|
:handler (openapi/create-openapi-handler)}}]
|
|
|
|
|
app (ring/ring-handler
|
|
|
|
|
(ring/router
|
|
|
|
|
[["/common" {:openapi {:id #{::one ::two}}}
|
|
|
|
|
ping-route]
|
|
|
|
|
|
|
|
|
|
["/one" {:openapi {:id ::one}}
|
|
|
|
|
ping-route
|
|
|
|
|
spec-route]
|
|
|
|
|
|
|
|
|
|
["/two" {:openapi {:id ::two}}
|
|
|
|
|
ping-route
|
|
|
|
|
spec-route
|
|
|
|
|
["/deep" {:openapi {:id ::one}}
|
|
|
|
|
ping-route]]
|
|
|
|
|
["/one-two" {:openapi {:id #{::one ::two}}}
|
|
|
|
|
spec-route]]))]
|
|
|
|
|
(is (= ["/common/ping" "/one/ping" "/two/deep/ping"]
|
|
|
|
|
(spec-paths app "/one/openapi.json")))
|
|
|
|
|
(is (= ["/common/ping" "/two/ping"]
|
|
|
|
|
(spec-paths app "/two/openapi.json")))
|
|
|
|
|
(is (= ["/common/ping" "/one/ping" "/two/ping" "/two/deep/ping"]
|
|
|
|
|
(spec-paths app "/one-two/openapi.json")))))
|
|
|
|
|
|
|
|
|
|
(deftest openapi-ui-config-test
|
|
|
|
|
(let [app (swagger-ui/create-swagger-ui-handler
|
|
|
|
|
{:path "/"
|
|
|
|
|
:url "/openapi.json"
|
|
|
|
|
:config {:jsonEditor true}})]
|
|
|
|
|
(is (= 302 (:status (app {:request-method :get, :uri "/"}))))
|
|
|
|
|
(is (= 200 (:status (app {:request-method :get, :uri "/index.html"}))))
|
|
|
|
|
(is (= {:jsonEditor true, :url "/openapi.json"}
|
|
|
|
|
(->> {:request-method :get, :uri "/config.json"}
|
|
|
|
|
(app) :body (m/decode m/instance "application/json"))))))
|
|
|
|
|
|
|
|
|
|
(deftest without-openapi-id-test
|
|
|
|
|
(let [app (ring/ring-handler
|
|
|
|
|
(ring/router
|
|
|
|
|
[["/ping"
|
|
|
|
|
{:get (constantly "ping")}]
|
|
|
|
|
["/openapi.json"
|
|
|
|
|
{:get {:no-doc true
|
|
|
|
|
:handler (openapi/create-openapi-handler)}}]]))]
|
|
|
|
|
(is (= ["/ping"] (spec-paths app "/openapi.json")))
|
|
|
|
|
(is (= #{::openapi/default}
|
|
|
|
|
(-> {:request-method :get :uri "/openapi.json"}
|
|
|
|
|
(app) :body :x-id)))))
|
|
|
|
|
|
|
|
|
|
(deftest with-options-endpoint-test
|
|
|
|
|
(let [app (ring/ring-handler
|
|
|
|
|
(ring/router
|
|
|
|
|
[["/ping"
|
|
|
|
|
{:options (constantly "options")}]
|
|
|
|
|
["/pong"
|
|
|
|
|
(constantly "options")]
|
|
|
|
|
["/openapi.json"
|
|
|
|
|
{:get {:no-doc true
|
|
|
|
|
:handler (openapi/create-openapi-handler)}}]]))]
|
|
|
|
|
(is (= ["/ping" "/pong"] (spec-paths app "/openapi.json")))
|
|
|
|
|
(is (= #{::openapi/default}
|
|
|
|
|
(-> {:request-method :get :uri "/openapi.json"}
|
|
|
|
|
(app) :body :x-id)))))
|
|
|
|
|
|
2023-02-16 08:26:54 +00:00
|
|
|
(defn- normalize
|
|
|
|
|
"Normalize format of openapi spec by converting it to json and back.
|
|
|
|
|
Handles differences like :q vs \"q\" in openapi generation."
|
|
|
|
|
[data]
|
|
|
|
|
(-> data
|
|
|
|
|
j/write-value-as-string
|
|
|
|
|
(j/read-value j/keyword-keys-object-mapper)))
|
2022-08-30 16:24:21 +00:00
|
|
|
|
|
|
|
|
(deftest all-parameter-types-test
|
2023-02-16 08:26:54 +00:00
|
|
|
(doseq [[coercion ->schema]
|
|
|
|
|
[[#'malli/coercion (fn [nom] [:map [nom :string]])]
|
|
|
|
|
[#'schema/coercion (fn [nom] {nom s/Str})]
|
|
|
|
|
[#'spec/coercion (fn [nom] {nom string?})]]]
|
|
|
|
|
(testing coercion
|
|
|
|
|
(let [app (ring/ring-handler
|
|
|
|
|
(ring/router
|
|
|
|
|
[["/parameters"
|
2023-03-07 07:10:37 +00:00
|
|
|
{:post {:decription "parameters"
|
|
|
|
|
:coercion @coercion
|
2023-02-16 08:26:54 +00:00
|
|
|
:parameters {:query (->schema :q)
|
|
|
|
|
:body (->schema :b)
|
|
|
|
|
:header (->schema :h)
|
|
|
|
|
:cookie (->schema :c)
|
|
|
|
|
:path (->schema :p)}
|
2023-03-07 07:10:37 +00:00
|
|
|
:responses {200 {:description "success"
|
|
|
|
|
:body (->schema :ok)}}
|
2023-02-16 08:26:54 +00:00
|
|
|
:handler identity}}]
|
|
|
|
|
["/openapi.json"
|
|
|
|
|
{:get {:handler (openapi/create-openapi-handler)
|
2023-03-07 07:10:37 +00:00
|
|
|
:openapi {:info {:title "" :version "0.0.1"}}
|
2023-03-08 12:11:28 +00:00
|
|
|
:no-doc true}}]]
|
|
|
|
|
{:data {:middleware [openapi/openapi-feature]}}))
|
2023-02-16 08:26:54 +00:00
|
|
|
spec (-> {:request-method :get
|
|
|
|
|
:uri "/openapi.json"}
|
|
|
|
|
app
|
|
|
|
|
:body)]
|
|
|
|
|
(testing "all non-body parameters"
|
|
|
|
|
(is (match? [{:in "query"
|
|
|
|
|
:name "q"
|
|
|
|
|
:required true
|
|
|
|
|
:schema {:type "string"}}
|
|
|
|
|
{:in "header"
|
|
|
|
|
:name "h"
|
|
|
|
|
:required true
|
|
|
|
|
:schema {:type "string"}}
|
|
|
|
|
{:in "cookie"
|
|
|
|
|
:name "c"
|
|
|
|
|
:required true
|
|
|
|
|
:schema {:type "string"}}
|
|
|
|
|
{:in "path"
|
|
|
|
|
:name "p"
|
|
|
|
|
:required true
|
|
|
|
|
:schema {:type "string"}}]
|
|
|
|
|
(-> spec
|
|
|
|
|
(get-in [:paths "/parameters" :post :parameters])
|
|
|
|
|
normalize))))
|
|
|
|
|
(testing "body parameter"
|
2023-03-15 14:01:57 +00:00
|
|
|
(is (match? (merge {:type "object"
|
|
|
|
|
:properties {:b {:type "string"}}
|
|
|
|
|
:required ["b"]}
|
|
|
|
|
;; spec outputs open schemas
|
|
|
|
|
(when-not (#{#'spec/coercion} coercion)
|
|
|
|
|
{:additionalProperties false}))
|
2023-02-16 08:26:54 +00:00
|
|
|
(-> spec
|
2023-03-15 14:01:57 +00:00
|
|
|
(get-in [:paths "/parameters" :post :requestBody :content "application/json" :schema])
|
2023-02-16 08:26:54 +00:00
|
|
|
normalize))))
|
|
|
|
|
(testing "body response"
|
2023-03-15 14:01:57 +00:00
|
|
|
(is (match? (merge {:type "object"
|
|
|
|
|
:properties {:ok {:type "string"}}
|
|
|
|
|
:required ["ok"]}
|
|
|
|
|
(when-not (#{#'spec/coercion} coercion)
|
|
|
|
|
{:additionalProperties false}))
|
2023-02-16 08:26:54 +00:00
|
|
|
(-> spec
|
2023-03-15 14:01:57 +00:00
|
|
|
(get-in [:paths "/parameters" :post :responses 200 :content "application/json" :schema])
|
2023-03-07 07:10:37 +00:00
|
|
|
normalize))))
|
|
|
|
|
(testing "spec is valid"
|
|
|
|
|
(is (nil? (validate spec))))))))
|
2023-02-16 08:26:54 +00:00
|
|
|
|
2023-03-15 08:22:26 +00:00
|
|
|
(deftest multipart-test
|
|
|
|
|
(doseq [[coercion file-schema]
|
|
|
|
|
[#_[#'malli/coercion (fn [nom] [:map [nom :string]])]
|
|
|
|
|
#_[#'schema/coercion (fn [nom] {nom s/Str})]
|
|
|
|
|
[#'spec/coercion reitit.http.interceptors.multipart/bytes-part]]]
|
|
|
|
|
(testing coercion
|
|
|
|
|
(let [app (ring/ring-handler
|
|
|
|
|
(ring/router
|
|
|
|
|
[["/upload"
|
|
|
|
|
{:post {:decription "upload"
|
|
|
|
|
:coercion @coercion
|
|
|
|
|
:parameters {:multipart {:file file-schema}}
|
|
|
|
|
:handler identity}}]
|
|
|
|
|
["/openapi.json"
|
|
|
|
|
{:get {:handler (openapi/create-openapi-handler)
|
|
|
|
|
:openapi {:info {:title "" :version "0.0.1"}}
|
|
|
|
|
:no-doc true}}]]
|
|
|
|
|
{:data {:middleware [openapi/openapi-feature]}}))
|
|
|
|
|
spec (-> {:request-method :get
|
|
|
|
|
:uri "/openapi.json"}
|
|
|
|
|
app
|
|
|
|
|
:body)]
|
|
|
|
|
(testing "multipart body"
|
|
|
|
|
(is (= {:requestBody {:content {"multipart/form-data" {:schema {:type "object"
|
|
|
|
|
:properties {"file" {:type "string"
|
|
|
|
|
:format "binary"}}
|
|
|
|
|
:required ["file"]}}}}}
|
|
|
|
|
(-> spec
|
|
|
|
|
(get-in [:paths "/upload" :post])
|
|
|
|
|
#_normalize))))
|
|
|
|
|
(testing "spec is valid"
|
|
|
|
|
(is (nil? (validate spec))))))))
|
|
|
|
|
|
2023-02-16 08:26:54 +00:00
|
|
|
(deftest per-content-type-test
|
|
|
|
|
(doseq [[coercion ->schema]
|
|
|
|
|
[[#'malli/coercion (fn [nom] [:map [nom :string]])]
|
|
|
|
|
[#'schema/coercion (fn [nom] {nom s/Str})]
|
|
|
|
|
[#'spec/coercion (fn [nom] {nom string?})]]]
|
|
|
|
|
(testing coercion
|
|
|
|
|
(let [app (ring/ring-handler
|
|
|
|
|
(ring/router
|
|
|
|
|
[["/parameters"
|
2023-03-07 07:10:37 +00:00
|
|
|
{:post {:description "parameters"
|
|
|
|
|
:coercion @coercion
|
2023-03-03 09:47:01 +00:00
|
|
|
:parameters {:request {:content {"application/json" (->schema :b)
|
|
|
|
|
"application/edn" (->schema :c)}}}
|
2023-03-07 07:10:37 +00:00
|
|
|
:responses {200 {:description "success"
|
|
|
|
|
:content {"application/json" (->schema :ok)
|
2023-03-03 09:47:01 +00:00
|
|
|
"application/edn" (->schema :edn)}}}
|
2023-02-16 08:26:54 +00:00
|
|
|
:handler (fn [req]
|
|
|
|
|
{:status 200
|
|
|
|
|
:body (-> req :parameters :request)})}}]
|
|
|
|
|
["/openapi.json"
|
|
|
|
|
{:get {:handler (openapi/create-openapi-handler)
|
2023-03-07 07:10:37 +00:00
|
|
|
:openapi {:info {:title "" :version "0.0.1"}}
|
2023-02-16 08:26:54 +00:00
|
|
|
:no-doc true}}]]
|
2023-03-08 12:11:28 +00:00
|
|
|
{:validate reitit.ring.spec/validate
|
|
|
|
|
:data {:middleware [openapi/openapi-feature
|
|
|
|
|
rrc/coerce-request-middleware
|
2023-02-16 08:26:54 +00:00
|
|
|
rrc/coerce-response-middleware]}}))
|
|
|
|
|
spec (-> {:request-method :get
|
|
|
|
|
:uri "/openapi.json"}
|
|
|
|
|
app
|
|
|
|
|
:body)]
|
|
|
|
|
(testing "body parameter"
|
2023-03-15 14:01:57 +00:00
|
|
|
(is (match? (merge {:type "object"
|
|
|
|
|
:properties {:b {:type "string"}}
|
|
|
|
|
:required ["b"]}
|
|
|
|
|
(when-not (#{#'spec/coercion} coercion)
|
|
|
|
|
{:additionalProperties false}))
|
2023-03-03 09:47:01 +00:00
|
|
|
(-> spec
|
2023-03-15 14:01:57 +00:00
|
|
|
(get-in [:paths "/parameters" :post :requestBody :content "application/json" :schema])
|
2023-03-03 09:47:01 +00:00
|
|
|
normalize)))
|
2023-03-15 14:01:57 +00:00
|
|
|
(is (match? (merge {:type "object"
|
|
|
|
|
:properties {:c {:type "string"}}
|
|
|
|
|
:required ["c"]}
|
|
|
|
|
(when-not (#{#'spec/coercion} coercion)
|
|
|
|
|
{:additionalProperties false}))
|
2023-03-03 09:47:01 +00:00
|
|
|
(-> spec
|
2023-03-15 14:01:57 +00:00
|
|
|
(get-in [:paths "/parameters" :post :requestBody :content "application/edn" :schema])
|
2023-03-03 09:47:01 +00:00
|
|
|
normalize))))
|
2023-02-16 08:26:54 +00:00
|
|
|
(testing "body response"
|
2023-03-15 14:01:57 +00:00
|
|
|
(is (match? (merge {:type "object"
|
|
|
|
|
:properties {:ok {:type "string"}}
|
|
|
|
|
:required ["ok"]}
|
|
|
|
|
(when-not (#{#'spec/coercion} coercion)
|
|
|
|
|
{:additionalProperties false}))
|
2023-03-03 09:47:01 +00:00
|
|
|
(-> spec
|
2023-03-15 14:01:57 +00:00
|
|
|
(get-in [:paths "/parameters" :post :responses 200 :content "application/json" :schema])
|
2023-03-03 09:47:01 +00:00
|
|
|
normalize)))
|
2023-03-15 14:01:57 +00:00
|
|
|
(is (match? (merge {:type "object"
|
|
|
|
|
:properties {:edn {:type "string"}}
|
|
|
|
|
:required ["edn"]}
|
|
|
|
|
(when-not (#{#'spec/coercion} coercion)
|
|
|
|
|
{:additionalProperties false}))
|
2023-03-03 09:47:01 +00:00
|
|
|
(-> spec
|
2023-03-15 14:01:57 +00:00
|
|
|
(get-in [:paths "/parameters" :post :responses 200 :content "application/edn" :schema])
|
2023-03-03 09:47:01 +00:00
|
|
|
normalize))))
|
2023-02-16 08:26:54 +00:00
|
|
|
(testing "validation"
|
2023-03-03 09:47:01 +00:00
|
|
|
(let [query {:request-method :post
|
|
|
|
|
:uri "/parameters"
|
|
|
|
|
:muuntaja/request {:format "application/json"}
|
|
|
|
|
:muuntaja/response {:format "application/json"}
|
|
|
|
|
:body-params {:b "x"}}]
|
2023-02-16 08:26:54 +00:00
|
|
|
(testing "of output"
|
|
|
|
|
(is (= {:type :reitit.coercion/response-coercion
|
|
|
|
|
:in [:response :body]}
|
|
|
|
|
(try
|
2023-03-03 09:47:01 +00:00
|
|
|
(app query)
|
2023-02-16 08:26:54 +00:00
|
|
|
(catch clojure.lang.ExceptionInfo e
|
|
|
|
|
(select-keys (ex-data e) [:type :in]))))))
|
|
|
|
|
(testing "of input"
|
|
|
|
|
(is (= {:type :reitit.coercion/request-coercion
|
|
|
|
|
:in [:request :body-params]}
|
|
|
|
|
(try
|
2023-03-03 09:47:01 +00:00
|
|
|
(app (assoc query :body-params {:z 1}))
|
2023-02-16 08:26:54 +00:00
|
|
|
(catch clojure.lang.ExceptionInfo e
|
2023-03-07 07:10:37 +00:00
|
|
|
(select-keys (ex-data e) [:type :in]))))))))
|
|
|
|
|
(testing "spec is valid"
|
|
|
|
|
(is (nil? (validate spec))))))))
|
2023-03-06 07:12:56 +00:00
|
|
|
|
|
|
|
|
(deftest default-content-type-test
|
|
|
|
|
(doseq [[coercion ->schema]
|
|
|
|
|
[[#'malli/coercion (fn [nom] [:map [nom :string]])]
|
|
|
|
|
[#'schema/coercion (fn [nom] {nom s/Str})]
|
|
|
|
|
[#'spec/coercion (fn [nom] {nom string?})]]]
|
|
|
|
|
(testing coercion
|
|
|
|
|
(doseq [content-type ["application/json" "application/edn"]]
|
|
|
|
|
(testing (str "default content type " content-type)
|
|
|
|
|
(let [app (ring/ring-handler
|
|
|
|
|
(ring/router
|
|
|
|
|
[["/parameters"
|
2023-03-07 07:10:37 +00:00
|
|
|
{:post {:description "parameters"
|
|
|
|
|
:coercion @coercion
|
2023-03-06 07:12:56 +00:00
|
|
|
:content-types [content-type] ;; TODO should this be under :openapi ?
|
|
|
|
|
:parameters {:request {:content {"application/transit" (->schema :transit)}
|
|
|
|
|
:body (->schema :default)}}
|
2023-03-07 07:10:37 +00:00
|
|
|
:responses {200 {:description "success"
|
|
|
|
|
:content {"application/transit" (->schema :transit)}
|
2023-03-06 07:12:56 +00:00
|
|
|
:body (->schema :default)}}
|
|
|
|
|
:handler (fn [req]
|
|
|
|
|
{:status 200
|
|
|
|
|
:body (-> req :parameters :request)})}}]
|
|
|
|
|
["/openapi.json"
|
|
|
|
|
{:get {:handler (openapi/create-openapi-handler)
|
2023-03-07 07:10:37 +00:00
|
|
|
:openapi {:info {:title "" :version "0.0.1"}}
|
2023-03-06 07:12:56 +00:00
|
|
|
:no-doc true}}]]
|
2023-03-08 12:11:28 +00:00
|
|
|
{:validate reitit.ring.spec/validate
|
|
|
|
|
:data {:middleware [openapi/openapi-feature
|
|
|
|
|
rrc/coerce-request-middleware
|
2023-03-06 07:12:56 +00:00
|
|
|
rrc/coerce-response-middleware]}}))
|
|
|
|
|
spec (-> {:request-method :get
|
|
|
|
|
:uri "/openapi.json"}
|
|
|
|
|
app
|
|
|
|
|
:body)]
|
|
|
|
|
(testing "body parameter"
|
|
|
|
|
(is (match? (matchers/in-any-order [content-type "application/transit"])
|
|
|
|
|
(-> spec
|
|
|
|
|
(get-in [:paths "/parameters" :post :requestBody :content])
|
|
|
|
|
keys))))
|
|
|
|
|
(testing "body response"
|
|
|
|
|
(is (match? (matchers/in-any-order [content-type "application/transit"])
|
|
|
|
|
(-> spec
|
|
|
|
|
(get-in [:paths "/parameters" :post :responses 200 :content])
|
2023-03-07 07:10:37 +00:00
|
|
|
keys))))
|
|
|
|
|
(testing "spec is valid"
|
|
|
|
|
(is (nil? (validate spec))))))))))
|