Merge pull request #592 from metosin/openapi-fixes

misc. fixes for openapi3 support
This commit is contained in:
Joel Kaasinen 2023-03-15 17:47:44 +02:00 committed by GitHub
commit 8bf4b5c6a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 203 additions and 197 deletions

View file

@ -53,7 +53,7 @@
["/openapi.json" ["/openapi.json"
{:get {:no-doc true {:get {:no-doc true
:openapi {:info {:title "my-api" :openapi {:info {:title "my-api"
:description "openap-docs with reitit-http" :description "openapi3-docs with reitit-http"
:version "0.0.1"} :version "0.0.1"}
;; used in /secure APIs below ;; used in /secure APIs below
:components {:securitySchemes {"auth" {:type :apiKey :components {:securitySchemes {"auth" {:type :apiKey

View file

@ -107,13 +107,12 @@
(if (:schema $) (if (:schema $)
(update $ :schema #(coercion/-compile-model this % nil)) (update $ :schema #(coercion/-compile-model this % nil))
$))]))}))) $))]))})))
:openapi (openapi/openapi-spec :openapi (merge
(merge
(when (seq (dissoc parameters :body :request)) (when (seq (dissoc parameters :body :request))
{::openapi/parameters (openapi/openapi-spec {::openapi/parameters
(into (empty parameters) (into (empty parameters)
(for [[k v] (dissoc parameters :body :request)] (for [[k v] (dissoc parameters :body :request)]
[k (coercion/-compile-model this v nil)]))}) [k (coercion/-compile-model this v nil)]))}))
(when (:body parameters) (when (:body parameters)
{:requestBody (openapi/openapi-spec {:requestBody (openapi/openapi-spec
{::openapi/content (zipmap content-types (repeat (coercion/-compile-model this (:body parameters) nil)))})}) {::openapi/content (zipmap content-types (repeat (coercion/-compile-model this (:body parameters) nil)))})})
@ -140,7 +139,7 @@
(when response (when response
(into {} (into {}
(for [[format model] (:content response)] (for [[format model] (:content response)]
[format (coercion/-compile-model this model nil)]))))})))]))}))) [format (coercion/-compile-model this model nil)]))))})))]))}))
(throw (throw
(ex-info (ex-info
(str "Can't produce Spec apidocs for " specification) (str "Can't produce Spec apidocs for " specification)

View file

@ -408,20 +408,23 @@
(get-in [:paths "/parameters" :post :parameters]) (get-in [:paths "/parameters" :post :parameters])
normalize)))) normalize))))
(testing "body parameter" (testing "body parameter"
(is (match? {:schema {:type "object" (is (match? (merge {:type "object"
:properties {:b {:type "string"}} :properties {:b {:type "string"}}
#_#_:additionalProperties false ;; not present for spec :required ["b"]}
:required ["b"]}} ;; spec outputs open schemas
(when-not (#{#'spec/coercion} coercion)
{:additionalProperties false}))
(-> spec (-> spec
(get-in [:paths "/parameters" :post :requestBody :content "application/json"]) (get-in [:paths "/parameters" :post :requestBody :content "application/json" :schema])
normalize)))) normalize))))
(testing "body response" (testing "body response"
(is (match? {:schema {:type "object" (is (match? (merge {:type "object"
:properties {:ok {:type "string"}} :properties {:ok {:type "string"}}
#_#_:additionalProperties false ;; not present for spec :required ["ok"]}
:required ["ok"]}} (when-not (#{#'spec/coercion} coercion)
{:additionalProperties false}))
(-> spec (-> spec
(get-in [:paths "/parameters" :post :responses 200 :content "application/json"]) (get-in [:paths "/parameters" :post :responses 200 :content "application/json" :schema])
normalize)))) normalize))))
(testing "spec is valid" (testing "spec is valid"
(is (nil? (validate spec)))))))) (is (nil? (validate spec))))))))
@ -458,34 +461,38 @@
app app
:body)] :body)]
(testing "body parameter" (testing "body parameter"
(is (match? {:schema {:type "object" (is (match? (merge {:type "object"
:properties {:b {:type "string"}} :properties {:b {:type "string"}}
#_#_:additionalProperties false ;; not present for spec :required ["b"]}
:required ["b"]}} (when-not (#{#'spec/coercion} coercion)
{:additionalProperties false}))
(-> spec (-> spec
(get-in [:paths "/parameters" :post :requestBody :content "application/json"]) (get-in [:paths "/parameters" :post :requestBody :content "application/json" :schema])
normalize))) normalize)))
(is (match? {:schema {:type "object" (is (match? (merge {:type "object"
:properties {:c {:type "string"}} :properties {:c {:type "string"}}
#_#_:additionalProperties false ;; not present for spec :required ["c"]}
:required ["c"]}} (when-not (#{#'spec/coercion} coercion)
{:additionalProperties false}))
(-> spec (-> spec
(get-in [:paths "/parameters" :post :requestBody :content "application/edn"]) (get-in [:paths "/parameters" :post :requestBody :content "application/edn" :schema])
normalize)))) normalize))))
(testing "body response" (testing "body response"
(is (match? {:schema {:type "object" (is (match? (merge {:type "object"
:properties {:ok {:type "string"}} :properties {:ok {:type "string"}}
#_#_:additionalProperties false ;; not present for spec :required ["ok"]}
:required ["ok"]}} (when-not (#{#'spec/coercion} coercion)
{:additionalProperties false}))
(-> spec (-> spec
(get-in [:paths "/parameters" :post :responses 200 :content "application/json"]) (get-in [:paths "/parameters" :post :responses 200 :content "application/json" :schema])
normalize))) normalize)))
(is (match? {:schema {:type "object" (is (match? (merge {:type "object"
:properties {:edn {:type "string"}} :properties {:edn {:type "string"}}
#_#_:additionalProperties false ;; not present for spec :required ["edn"]}
:required ["edn"]}} (when-not (#{#'spec/coercion} coercion)
{:additionalProperties false}))
(-> spec (-> spec
(get-in [:paths "/parameters" :post :responses 200 :content "application/edn"]) (get-in [:paths "/parameters" :post :responses 200 :content "application/edn" :schema])
normalize)))) normalize))))
(testing "validation" (testing "validation"
(let [query {:request-method :post (let [query {:request-method :post