diff --git a/modules/reitit-malli/src/reitit/coercion/malli.cljc b/modules/reitit-malli/src/reitit/coercion/malli.cljc index 91a155fa..4a4dc9ea 100644 --- a/modules/reitit-malli/src/reitit/coercion/malli.cljc +++ b/modules/reitit-malli/src/reitit/coercion/malli.cljc @@ -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}))])) diff --git a/modules/reitit-schema/src/reitit/coercion/schema.cljc b/modules/reitit-schema/src/reitit/coercion/schema.cljc index 0ffa4e2b..e0822ad9 100644 --- a/modules/reitit-schema/src/reitit/coercion/schema.cljc +++ b/modules/reitit-schema/src/reitit/coercion/schema.cljc @@ -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 diff --git a/modules/reitit-spec/src/reitit/coercion/spec.cljc b/modules/reitit-spec/src/reitit/coercion/spec.cljc index 8043a78a..b988d140 100644 --- a/modules/reitit-spec/src/reitit/coercion/spec.cljc +++ b/modules/reitit-spec/src/reitit/coercion/spec.cljc @@ -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) diff --git a/test/cljc/reitit/openapi_test.clj b/test/cljc/reitit/openapi_test.clj index 31af695a..564b1a6d 100644 --- a/test/cljc/reitit/openapi_test.clj +++ b/test/cljc/reitit/openapi_test.clj @@ -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))