fix: per-content-type openapi w/ spec

This commit is contained in:
Joel Kaasinen 2023-03-03 11:47:01 +02:00
parent b149c8c5af
commit 2d60702769
2 changed files with 45 additions and 26 deletions

View file

@ -118,7 +118,9 @@
{::openapi/content {"application/json" (coercion/-compile-model this (:body parameters) nil)}})})
(when (:request parameters)
{:requestBody (openapi/openapi-spec
{::openapi/content (coercion/-compile-model this (:content (:request parameters)) nil)})})
{::openapi/content (into {}
(for [[format model] (:content (:request parameters))]
[format (coercion/-compile-model this model nil)]))})})
(when responses
{:responses
(into
@ -131,7 +133,9 @@
{::openapi/content {"application/json" (coercion/-compile-model this (:body response) nil)}}))
(when (:content response)
(openapi/openapi-spec
{::openapi/content (coercion/-compile-model this (:content response) nil)})))]))})))
{::openapi/content (into {}
(for [[format model] (:content response)]
[format (coercion/-compile-model this model nil)]))})))]))})))
(throw
(ex-info
(str "Can't produce Spec apidocs for " specification)

View file

@ -385,15 +385,16 @@
(doseq [[coercion ->schema]
[[#'malli/coercion (fn [nom] [:map [nom :string]])]
[#'schema/coercion (fn [nom] {nom s/Str})]
#_ ;; Doesn't work yet
[#'spec/coercion (fn [nom] {nom string?})]]]
(testing coercion
(let [app (ring/ring-handler
(ring/router
[["/parameters"
{:post {:coercion @coercion
:parameters {:request {:content {"application/json" (->schema :b)}}}
:responses {200 {:content {"application/json" (->schema :ok)}}}
:parameters {:request {:content {"application/json" (->schema :b)
"application/edn" (->schema :c)}}}
:responses {200 {:content {"application/json" (->schema :ok)
"application/edn" (->schema :edn)}}}
:handler (fn [req]
{:status 200
:body (-> req :parameters :request)})}}]
@ -407,38 +408,52 @@
app
:body)]
(testing "body parameter"
(is (= {:schema {:type "object"
:properties {:b {:type "string"}}
:additionalProperties false
:required ["b"]}}
(-> spec
(get-in [:paths "/parameters" :post :requestBody :content "application/json"])
normalize))))
(is (match? {:schema {:type "object"
:properties {:b {:type "string"}}
#_#_:additionalProperties false ;; not present for spec
:required ["b"]}}
(-> spec
(get-in [:paths "/parameters" :post :requestBody :content "application/json"])
normalize)))
(is (match? {:schema {:type "object"
:properties {:c {:type "string"}}
#_#_:additionalProperties false ;; not present for spec
:required ["c"]}}
(-> spec
(get-in [:paths "/parameters" :post :requestBody :content "application/edn"])
normalize))))
(testing "body response"
(is (= {:schema {:type "object"
:properties {:ok {:type "string"}}
:additionalProperties false
:required ["ok"]}}
(-> spec
(get-in [:paths "/parameters" :post :responses 200 :content "application/json"])
normalize))))
(is (match? {:schema {:type "object"
:properties {:ok {:type "string"}}
#_#_:additionalProperties false ;; not present for spec
:required ["ok"]}}
(-> spec
(get-in [:paths "/parameters" :post :responses 200 :content "application/json"])
normalize)))
(is (match? {:schema {:type "object"
:properties {:edn {:type "string"}}
#_#_:additionalProperties false ;; not present for spec
:required ["edn"]}}
(-> spec
(get-in [:paths "/parameters" :post :responses 200 :content "application/edn"])
normalize))))
(testing "validation"
(let [valid-query {:request-method :post
:uri "/parameters"
:muuntaja/request {:format "application/json"}
:muuntaja/response {:format "application/json"}
:body-params {:b "x"}}]
(let [query {:request-method :post
:uri "/parameters"
:muuntaja/request {:format "application/json"}
:muuntaja/response {:format "application/json"}
:body-params {:b "x"}}]
(testing "of output"
(is (= {:type :reitit.coercion/response-coercion
:in [:response :body]}
(try
(app (assoc valid-query :body-params {:b "x"}))
(app query)
(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
(app (assoc valid-query :body-params {:z 1}))
(app (assoc query :body-params {:z 1}))
(catch clojure.lang.ExceptionInfo e
(select-keys (ex-data e) [:type :in]))))))))))))