openapi content tests

This commit is contained in:
Tommi Reiman 2023-08-18 17:17:56 +03:00
parent d8e9819e0a
commit 226ca889b6
4 changed files with 65 additions and 37 deletions

View file

@ -195,23 +195,25 @@
{:responses {:responses
(into {} (into {}
(map (fn [[status {:keys [content], :as response}]] (map (fn [[status {:keys [content], :as response}]]
(let [content (merge (let [default (coercion/get-default-schema response)
(when-let [default (coercion/get-default-schema response)] content (-> (merge
(into {} (when default
(map (fn [content-type] (into {}
(let [schema (->schema-object default {:in :responses (map (fn [content-type]
:type :schema (let [schema (->schema-object default {:in :responses
:content-type content-type})] :type :schema
[content-type {:schema schema}]))) :content-type content-type})]
content-types)) [content-type {:schema schema}])))
(when content content-types))
(into {} (when content
(map (fn [[content-type {:keys [schema]}]] (into {}
(let [schema (->schema-object schema {:in :responses (map (fn [[content-type {:keys [schema]}]]
:type :schema (let [schema (->schema-object schema {:in :responses
:content-type content-type})] :type :schema
[content-type {:schema schema}]))) :content-type content-type})]
content)))] [content-type {:schema schema}])))
content)))
(dissoc :default))]
[status (merge (select-keys response [:description]) [status (merge (select-keys response [:description])
(when content (when content
{:content content}))])) {:content content}))]))

View file

@ -89,12 +89,13 @@
(select-keys response [:description]) (select-keys response [:description])
(when (or content default) (when (or content default)
(openapi/openapi-spec (openapi/openapi-spec
{::openapi/content (merge {::openapi/content (-> (merge
(when default (when default
(zipmap content-types (repeat default))) (zipmap content-types (repeat default)))
(->> (for [[content-type {:keys [schema]}] content] (->> (for [[content-type {:keys [schema]}] content]
[content-type schema]) [content-type schema])
(into {})))})))]))})) (into {})))
(dissoc :default))})))]))}))
(throw (throw
(ex-info (ex-info

View file

@ -128,17 +128,19 @@
(into (into
(empty responses) (empty responses)
(for [[k {:keys [content] :as response}] responses (for [[k {:keys [content] :as response}] responses
:let [default (coercion/get-default-schema response)]] :let [default (coercion/get-default-schema response)
content-types (remove #{:default} content-types)]]
[k (merge [k (merge
(select-keys response [:description]) (select-keys response [:description])
(when (or content default) (when (or content default)
(openapi/openapi-spec (openapi/openapi-spec
{::openapi/content (merge {::openapi/content (-> (merge
(when default (when default
(zipmap content-types (repeat default))) (zipmap content-types (repeat default)))
(->> (for [[content-type {:keys [schema]}] content] (->> (for [[content-type {:keys [schema]}] content]
[content-type schema]) [content-type schema])
(into {})))})))]))})) (into {})))
(dissoc :default))})))]))}))
(throw (throw
(ex-info (ex-info
(str "Can't produce Spec apidocs for " specification) (str "Can't produce Spec apidocs for " specification)

View file

@ -65,7 +65,10 @@
:description "kosh"}}} :description "kosh"}}}
:responses {200 {:description "success" :responses {200 {:description "success"
:body {:total int?}} :body {:total int?}}
500 {:description "fail"}} 500 {:description "fail"}
504 {:description "default"
:content {:default {:schema {:error string?}}}
:body {:masked string?}}}
:handler (fn [{{{:keys [z]} :path :handler (fn [{{{:keys [z]} :path
xs :body} :parameters}] xs :body} :parameters}]
{:status 200, :body {:total (+ (reduce + xs) z)}})}}]] {:status 200, :body {:total (+ (reduce + xs) z)}})}}]]
@ -91,7 +94,10 @@
:content {"application/json" {:schema {:type "string"}}}}}} :content {"application/json" {:schema {:type "string"}}}}}}
:responses {200 {:description "success" :responses {200 {:description "success"
:body [:map [:total int?]]} :body [:map [:total int?]]}
500 {:description "fail"}} 500 {:description "fail"}
504 {:description "default"
:content {:default {:schema {:error string?}}}
:body {:masked string?}}}
:handler (fn [{{{:keys [z]} :path :handler (fn [{{{:keys [z]} :path
xs :body} :parameters}] xs :body} :parameters}]
{:status 200, :body {:total (+ (reduce + xs) z)}})}}]] {:status 200, :body {:total (+ (reduce + xs) z)}})}}]]
@ -117,7 +123,10 @@
:description "kosh"}}} :description "kosh"}}}
:responses {200 {:description "success" :responses {200 {:description "success"
:body {:total s/Int}} :body {:total s/Int}}
500 {:description "fail"}} 500 {:description "fail"}
504 {:description "default"
:content {:default {:schema {:error s/Str}}}
:body {:masked s/Str}}}
:handler (fn [{{{:keys [z]} :path :handler (fn [{{{:keys [z]} :path
xs :body} :parameters}] xs :body} :parameters}]
{:status 200, :body {:total (+ (reduce + xs) z)}})}}]]] {:status 200, :body {:total (+ (reduce + xs) z)}})}}]]]
@ -193,7 +202,11 @@
:type "object"}}}} :type "object"}}}}
400 {:content {"application/json" {:schema {:type "string"}}} 400 {:content {"application/json" {:schema {:type "string"}}}
:description "kosh"} :description "kosh"}
500 {:description "fail"}} 500 {:description "fail"}
504 {:description "default"
:content {"application/json" {:schema {:properties {"error" {:type "string"}}
:required ["error"]
:type "object"}}}}}
:summary "plus with body"}} :summary "plus with body"}}
"/api/malli/plus/{z}" {:get {:parameters [{:in "query" "/api/malli/plus/{z}" {:get {:parameters [{:in "query"
:name :x :name :x
@ -231,7 +244,12 @@
:type "object"}}}} :type "object"}}}}
400 {:description "kosh" 400 {:description "kosh"
:content {"application/json" {:schema {:type "string"}}}} :content {"application/json" {:schema {:type "string"}}}}
500 {:description "fail"}} 500 {:description "fail"}
504 {:description "default"
:content {"application/json" {:schema {:additionalProperties false
:properties {:error {:type "string"}}
:required [:error]
:type "object"}}}}}
:summary "plus with body"}} :summary "plus with body"}}
"/api/schema/plus/{z}" {:get {:parameters [{:description "" "/api/schema/plus/{z}" {:get {:parameters [{:description ""
:in "query" :in "query"
@ -280,10 +298,15 @@
:type "object"}}}} :type "object"}}}}
400 {:description "kosh" 400 {:description "kosh"
:content {"application/json" {:schema {:type "string"}}}} :content {"application/json" {:schema {:type "string"}}}}
500 {:description "fail"}} 500 {:description "fail"}
504 {:description "default"
:content {"application/json" {:schema {:additionalProperties false
:properties {"error" {:type "string"}}
:required ["error"]
:type "object"}}}}}
:summary "plus with body"}}}}] :summary "plus with body"}}}}]
(is (= expected spec)) (is (= expected spec))
(is (nil? (validate spec)))))) (is (= nil (validate spec))))))
(defn spec-paths [app uri] (defn spec-paths [app uri]
(-> {:request-method :get, :uri uri} app :body :paths keys)) (-> {:request-method :get, :uri uri} app :body :paths keys))