mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +00:00
openapi content tests
This commit is contained in:
parent
d8e9819e0a
commit
226ca889b6
4 changed files with 65 additions and 37 deletions
|
|
@ -195,23 +195,25 @@
|
|||
{:responses
|
||||
(into {}
|
||||
(map (fn [[status {:keys [content], :as response}]]
|
||||
(let [content (merge
|
||||
(when-let [default (coercion/get-default-schema response)]
|
||||
(into {}
|
||||
(map (fn [content-type]
|
||||
(let [schema (->schema-object default {:in :responses
|
||||
:type :schema
|
||||
:content-type content-type})]
|
||||
[content-type {:schema schema}])))
|
||||
content-types))
|
||||
(when content
|
||||
(into {}
|
||||
(map (fn [[content-type {:keys [schema]}]]
|
||||
(let [schema (->schema-object schema {:in :responses
|
||||
:type :schema
|
||||
:content-type content-type})]
|
||||
[content-type {:schema schema}])))
|
||||
content)))]
|
||||
(let [default (coercion/get-default-schema response)
|
||||
content (-> (merge
|
||||
(when default
|
||||
(into {}
|
||||
(map (fn [content-type]
|
||||
(let [schema (->schema-object default {:in :responses
|
||||
:type :schema
|
||||
:content-type content-type})]
|
||||
[content-type {:schema schema}])))
|
||||
content-types))
|
||||
(when content
|
||||
(into {}
|
||||
(map (fn [[content-type {:keys [schema]}]]
|
||||
(let [schema (->schema-object schema {:in :responses
|
||||
:type :schema
|
||||
:content-type content-type})]
|
||||
[content-type {:schema schema}])))
|
||||
content)))
|
||||
(dissoc :default))]
|
||||
[status (merge (select-keys response [:description])
|
||||
(when content
|
||||
{:content content}))]))
|
||||
|
|
|
|||
|
|
@ -89,12 +89,13 @@
|
|||
(select-keys response [:description])
|
||||
(when (or content default)
|
||||
(openapi/openapi-spec
|
||||
{::openapi/content (merge
|
||||
(when default
|
||||
(zipmap content-types (repeat default)))
|
||||
(->> (for [[content-type {:keys [schema]}] content]
|
||||
[content-type schema])
|
||||
(into {})))})))]))}))
|
||||
{::openapi/content (-> (merge
|
||||
(when default
|
||||
(zipmap content-types (repeat default)))
|
||||
(->> (for [[content-type {:keys [schema]}] content]
|
||||
[content-type schema])
|
||||
(into {})))
|
||||
(dissoc :default))})))]))}))
|
||||
|
||||
(throw
|
||||
(ex-info
|
||||
|
|
|
|||
|
|
@ -128,17 +128,19 @@
|
|||
(into
|
||||
(empty 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
|
||||
(select-keys response [:description])
|
||||
(when (or content default)
|
||||
(openapi/openapi-spec
|
||||
{::openapi/content (merge
|
||||
(when default
|
||||
(zipmap content-types (repeat default)))
|
||||
(->> (for [[content-type {:keys [schema]}] content]
|
||||
[content-type schema])
|
||||
(into {})))})))]))}))
|
||||
{::openapi/content (-> (merge
|
||||
(when default
|
||||
(zipmap content-types (repeat default)))
|
||||
(->> (for [[content-type {:keys [schema]}] content]
|
||||
[content-type schema])
|
||||
(into {})))
|
||||
(dissoc :default))})))]))}))
|
||||
(throw
|
||||
(ex-info
|
||||
(str "Can't produce Spec apidocs for " specification)
|
||||
|
|
|
|||
|
|
@ -65,7 +65,10 @@
|
|||
:description "kosh"}}}
|
||||
:responses {200 {:description "success"
|
||||
: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
|
||||
xs :body} :parameters}]
|
||||
{:status 200, :body {:total (+ (reduce + xs) z)}})}}]]
|
||||
|
|
@ -91,7 +94,10 @@
|
|||
:content {"application/json" {:schema {:type "string"}}}}}}
|
||||
:responses {200 {:description "success"
|
||||
: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
|
||||
xs :body} :parameters}]
|
||||
{:status 200, :body {:total (+ (reduce + xs) z)}})}}]]
|
||||
|
|
@ -117,7 +123,10 @@
|
|||
:description "kosh"}}}
|
||||
:responses {200 {:description "success"
|
||||
: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
|
||||
xs :body} :parameters}]
|
||||
{:status 200, :body {:total (+ (reduce + xs) z)}})}}]]]
|
||||
|
|
@ -193,7 +202,11 @@
|
|||
:type "object"}}}}
|
||||
400 {:content {"application/json" {:schema {:type "string"}}}
|
||||
: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"}}
|
||||
"/api/malli/plus/{z}" {:get {:parameters [{:in "query"
|
||||
:name :x
|
||||
|
|
@ -231,7 +244,12 @@
|
|||
:type "object"}}}}
|
||||
400 {:description "kosh"
|
||||
: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"}}
|
||||
"/api/schema/plus/{z}" {:get {:parameters [{:description ""
|
||||
:in "query"
|
||||
|
|
@ -280,10 +298,15 @@
|
|||
:type "object"}}}}
|
||||
400 {:description "kosh"
|
||||
: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"}}}}]
|
||||
(is (= expected spec))
|
||||
(is (nil? (validate spec))))))
|
||||
(is (= nil (validate spec))))))
|
||||
|
||||
(defn spec-paths [app uri]
|
||||
(-> {:request-method :get, :uri uri} app :body :paths keys))
|
||||
|
|
|
|||
Loading…
Reference in a new issue