feature: fetch openapi content types from muuntaja

(level 1 integration in #636)
This commit is contained in:
Martín Varela 2024-02-09 10:26:57 +02:00
parent f1e6d37dcf
commit ed280f9a33
2 changed files with 121 additions and 45 deletions

View file

@ -3,6 +3,7 @@
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[clojure.string :as str] [clojure.string :as str]
[meta-merge.core :refer [meta-merge]] [meta-merge.core :refer [meta-merge]]
[muuntaja.core :as m]
[reitit.coercion :as coercion] [reitit.coercion :as coercion]
[reitit.core :as r] [reitit.core :as r]
[reitit.trie :as trie])) [reitit.trie :as trie]))
@ -76,9 +77,7 @@
(-> path (trie/normalize opts) (str/replace #"\{\*" "{"))) (-> path (trie/normalize opts) (str/replace #"\{\*" "{")))
(defn -get-apidocs-openapi (defn -get-apidocs-openapi
[coercion {:keys [request parameters responses openapi/request-content-types openapi/response-content-types] [coercion {:keys [request muuntaja parameters responses openapi/request-content-types openapi/response-content-types]}]
:or {request-content-types ["application/json"]
response-content-types ["application/json"]}}]
(let [{:keys [body multipart]} parameters (let [{:keys [body multipart]} parameters
parameters (dissoc parameters :request :body :multipart) parameters (dissoc parameters :request :body :multipart)
->content (fn [data schema] ->content (fn [data schema]
@ -86,7 +85,13 @@
{:schema schema} {:schema schema}
(select-keys data [:description :examples]) (select-keys data [:description :examples])
(:openapi data))) (:openapi data)))
->schema-object #(coercion/-get-model-apidocs coercion :openapi %1 %2)] ->schema-object #(coercion/-get-model-apidocs coercion :openapi %1 %2)
request-content-types (or request-content-types
(when muuntaja (m/decodes muuntaja))
["application/json"])
response-content-types (or response-content-types
(when muuntaja (m/encodes muuntaja))
["application/json"])]
(merge (merge
(when (seq parameters) (when (seq parameters)
{:parameters {:parameters

View file

@ -685,52 +685,123 @@
(testing "spec is valid" (testing "spec is valid"
(is (nil? (validate spec)))))))) (is (nil? (validate spec))))))))
(deftest default-content-type-test (deftest default-content-type-test
(doseq [[coercion ->schema] [[malli/coercion (fn [nom] [:map [nom :string]])] (doseq [[coercion ->schema] [[malli/coercion (fn [nom] [:map [nom :string]])]
[schema/coercion (fn [nom] {nom s/Str})] [schema/coercion (fn [nom] {nom s/Str})]
[spec/coercion (fn [nom] {nom string?})]]] [spec/coercion (fn [nom] {nom string?})]]]
(testing (str coercion) (testing (str coercion)
(doseq [content-type ["application/json" "application/edn"]] (let [app (ring/ring-handler
(testing (str "default content type " content-type) (ring/router
(let [app (ring/ring-handler [["/explicit-content-type"
(ring/router {:post {:description "parameters"
[["/parameters" :coercion coercion
{:post {:description "parameters" :request {:content {"application/json" {:schema (->schema :b)}
:coercion coercion "application/edn" {:schema (->schema :c)}}}
:openapi/request-content-types [content-type] :responses {200 {:description "success"
:openapi/response-content-types [content-type "application/response"] :content {"application/json" {:schema (->schema :ok)}
:request {:content {"application/transit" {:schema (->schema :transit)}} "application/edn" {:schema (->schema :edn)}}}}
:body (->schema :default)} :handler (fn [req]
:responses {200 {:description "success" {:status 200
:content {"application/transit" {:schema (->schema :transit)}} :body (-> req :parameters :request)})}}]
:body (->schema :default)}} ["/muuntaja"
:handler (fn [req] {:post {:description "default content types from muuntaja"
{:status 200 :coercion coercion
:body (-> req :parameters :request)})}}] ;;; TODO: test the :parameters syntax
["/openapi.json" :request {:content {:default {:schema (->schema :b)}
{:get {:handler (openapi/create-openapi-handler) "application/reitit-request" {:schema (->schema :ok)}}}
:openapi {:info {:title "" :version "0.0.1"}} :responses {200 {:description "success"
:no-doc true}}]] :content {:default {:schema (->schema :ok)}
{:validate reitit.ring.spec/validate "application/reitit-response" {:schema (->schema :ok)}}}}
:data {:middleware [openapi/openapi-feature :handler (fn [req]
rrc/coerce-request-middleware {:status 200
rrc/coerce-response-middleware]}})) :body (-> req :parameters :request)})}}]
spec (-> {:request-method :get ["/override-default-content-type"
:uri "/openapi.json"} {:post {:description "override default content types from muuntaja"
app :coercion coercion
:body)] :openapi/request-content-types ["application/request"]
(testing "body parameter" :openapi/response-content-types ["application/response"]
(is (match? (matchers/in-any-order [content-type "application/transit"]) ;;; TODO: test the :parameters syntax
(-> spec :request {:content {:default {:schema (->schema :b)}}}
(get-in [:paths "/parameters" :post :requestBody :content]) :responses {200 {:description "success"
keys)))) :content {:default {:schema (->schema :ok)}}}}
(testing "body response" :handler (fn [req]
(is (match? (matchers/in-any-order [content-type "application/transit" "application/response"]) {:status 200
(-> spec :body (-> req :parameters :request)})}}]
(get-in [:paths "/parameters" :post :responses 200 :content]) ["/legacy"
keys)))) {:post {:description "default content types from muuntaja, legacy syntax"
(testing "spec is valid" :coercion coercion
(is (nil? (validate spec)))))))))) ;;; TODO: test the :parameters syntax
:request {:body {:schema (->schema :b)}}
:responses {200 {:description "success"
:body {:schema (->schema :ok)}}}
:handler (fn [req]
{:status 200
:body (-> req :parameters :request)})}}]
["/openapi.json"
{:get {:handler (openapi/create-openapi-handler)
:openapi {:info {:title "" :version "0.0.1"}}
:no-doc true}}]]
{:validate reitit.ring.spec/validate
:data {:muuntaja (m/create (-> m/default-options
(update-in [:formats] select-keys ["application/transit+json"])
(assoc :default-format "application/transit+json")))
:middleware [openapi/openapi-feature
rrc/coerce-request-middleware
rrc/coerce-response-middleware]}}))
spec (-> {:request-method :get
:uri "/openapi.json"}
app
:body)
spec-coercion (= coercion spec/coercion)]
(testing "explicit content types"
(testing "body parameter"
(is (= ["application/edn" "application/json"]
(-> spec
(get-in [:paths "/explicit-content-type" :post :requestBody :content])
keys
sort))))
(testing "body response"
(is (= ["application/edn" "application/json"]
(-> spec
(get-in [:paths "/explicit-content-type" :post :responses 200 :content])
keys
sort)))))
(testing "muuntaja content types"
(testing "body parameter"
(is (= ["application/transit+json" "application/reitit-request"]
(-> spec
(get-in [:paths "/muuntaja" :post :requestBody :content])
keys))))
(testing "body response"
(is (= ["application/transit+json" "application/reitit-response"]
(-> spec
(get-in [:paths "/muuntaja" :post :responses 200 :content])
keys)))))
(testing "overridden muuntaja content types"
(testing "body parameter"
(is (= ["application/request"]
(-> spec
(get-in [:paths "/override-default-content-type" :post :requestBody :content])
keys))))
(testing "body response"
(is (= ["application/response"]
(-> spec
(get-in [:paths "/override-default-content-type" :post :responses 200 :content])
keys)))))
(testing "legacy syntax muuntaja content types"
(testing "body parameter"
(is (= ["application/transit+json"]
(-> spec
(get-in [:paths "/legacy" :post :requestBody :content])
keys))))
(testing "body response"
(is (= ["application/transit+json"]
(-> spec
(get-in [:paths "/legacy" :post :responses 200 :content])
keys)))))
(testing "spec is valid"
(is (nil? (validate spec))))))))
(deftest recursive-test (deftest recursive-test
;; Recursive schemas only properly supported for malli ;; Recursive schemas only properly supported for malli