feat: openapi3 multipart support for malli

This commit is contained in:
Joel Kaasinen 2023-03-15 15:48:53 +02:00
parent f322597c04
commit 1c65f533cf
2 changed files with 24 additions and 9 deletions

View file

@ -135,8 +135,8 @@
(defn -get-apidocs-openapi (defn -get-apidocs-openapi
[coercion {:keys [parameters responses content-types] :or {content-types ["application/json"]}} options] [coercion {:keys [parameters responses content-types] :or {content-types ["application/json"]}} options]
(let [{:keys [body request]} parameters (let [{:keys [body request multipart]} parameters
parameters (dissoc parameters :request :body) parameters (dissoc parameters :request :body :multipart)
->schema-object (fn [schema opts] ->schema-object (fn [schema opts]
(let [current-opts (merge options opts)] (let [current-opts (merge options opts)]
(json-schema/transform (coercion/-compile-model coercion schema current-opts) (json-schema/transform (coercion/-compile-model coercion schema current-opts)
@ -184,6 +184,14 @@
:content-type content-type})] :content-type content-type})]
[content-type {:schema schema}]))) [content-type {:schema schema}])))
(:content request)))}}) (:content request)))}})
(when multipart
{:requestBody
{:content
{"multipart/form-data"
{:schema
(->schema-object multipart {:in :requestBody
:type :schema
:content-type "multipart/form-data"})}}}})
(when responses (when responses
{:responses {:responses
(into {} (into {}

View file

@ -432,7 +432,11 @@
(deftest multipart-test (deftest multipart-test
(doseq [[coercion file-schema] (doseq [[coercion file-schema]
[#_[#'malli/coercion (fn [nom] [:map [nom :string]])] [[#'malli/coercion [:map {:json-schema {:type "string"
:format "binary"}}
[:filename :string]
[:content-type :string]
[:bytes :int]]]
#_[#'schema/coercion (fn [nom] {nom s/Str})] #_[#'schema/coercion (fn [nom] {nom s/Str})]
[#'spec/coercion reitit.http.interceptors.multipart/bytes-part]]] [#'spec/coercion reitit.http.interceptors.multipart/bytes-part]]]
(testing coercion (testing coercion
@ -453,13 +457,16 @@
app app
:body)] :body)]
(testing "multipart body" (testing "multipart body"
(is (= {:requestBody {:content {"multipart/form-data" {:schema {:type "object" (is (nil? (get-in spec [:paths "/upload" :post :parameters])))
:properties {"file" {:type "string" (is (= (merge {:type "object"
:properties {:file {:type "string"
:format "binary"}} :format "binary"}}
:required ["file"]}}}}} :required ["file"]}
(when-not (= #'spec/coercion coercion)
{:additionalProperties false}))
(-> spec (-> spec
(get-in [:paths "/upload" :post]) (get-in [:paths "/upload" :post :requestBody :content "multipart/form-data" :schema])
#_normalize)))) normalize))))
(testing "spec is valid" (testing "spec is valid"
(is (nil? (validate spec)))))))) (is (nil? (validate spec))))))))