Skip to content

Commit

Permalink
openapi content tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ikitommi committed Aug 18, 2023
1 parent d8e9819 commit 226ca88
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 37 deletions.
36 changes: 19 additions & 17 deletions modules/reitit-malli/src/reitit/coercion/malli.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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}))]))
Expand Down
13 changes: 7 additions & 6 deletions modules/reitit-schema/src/reitit/coercion/schema.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions modules/reitit-spec/src/reitit/coercion/spec.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 30 additions & 7 deletions test/cljc/reitit/openapi_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)}})}}]]
Expand All @@ -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)}})}}]]
Expand All @@ -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)}})}}]]]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 226ca88

Please sign in to comment.