diff --git a/modules/reitit-openapi/src/reitit/openapi.clj b/modules/reitit-openapi/src/reitit/openapi.clj index 497b811e..aac112a2 100644 --- a/modules/reitit-openapi/src/reitit/openapi.clj +++ b/modules/reitit-openapi/src/reitit/openapi.clj @@ -125,23 +125,24 @@ (when request ;; request allow to different :requestBody per content-type {:requestBody - {:content (merge - (select-keys request [:description]) - (when-let [{:keys [schema] :as data} (coercion/get-default request)] - (into {} - (map (fn [content-type] - (let [schema (->schema-object schema {:in :requestBody - :type :schema - :content-type content-type})] - [content-type (->content data schema)]))) - request-content-types)) - (into {} - (map (fn [[content-type {:keys [schema] :as data}]] - (let [schema (->schema-object schema {:in :requestBody - :type :schema - :content-type content-type})] - [content-type (->content data schema)]))) - (dissoc (:content request) :default)))}}) + (merge + (select-keys request [:description]) + {:content (merge + (when-let [{:keys [schema] :as data} (coercion/get-default request)] + (into {} + (map (fn [content-type] + (let [schema (->schema-object schema {:in :requestBody + :type :schema + :content-type content-type})] + [content-type (->content data schema)]))) + request-content-types)) + (into {} + (map (fn [[content-type {:keys [schema] :as data}]] + (let [schema (->schema-object schema {:in :requestBody + :type :schema + :content-type content-type})] + [content-type (->content data schema)]))) + (dissoc (:content request) :default)))})}) (when multipart {:requestBody {:content diff --git a/test/cljc/reitit/openapi_test.clj b/test/cljc/reitit/openapi_test.clj index 8d77dd4b..b8812921 100644 --- a/test/cljc/reitit/openapi_test.clj +++ b/test/cljc/reitit/openapi_test.clj @@ -869,19 +869,20 @@ (ring/router [["/openapi.json" {:get {:no-doc true + :openapi {:info {:title "" :version "0.0.1"}} :handler (openapi/create-openapi-handler)}}] ["/malli" {:coercion malli/coercion} ["/plus" {:post {:summary "plus with body" :request {:description "body description" :content {"application/json" {:schema {:x int?, :y int?} - :examples {"1+1" {:x 1, :y 1} - "1+2" {:x 1, :y 2}} + :examples {"1+1" {:value {:x 1, :y 1}} + "1+2" {:value {:x 1, :y 2}}} :openapi {:example {:x 2, :y 2}}}}} :responses {200 {:description "success" :content {"application/json" {:schema {:total int?} - :examples {"2" {:total 2} - "3" {:total 3}} + :examples {"2" {:value {:total 2}} + "3" {:value {:total 3}}} :openapi {:example {:total 4}}}}}} :handler (fn [request] (let [{:keys [x y]} (-> request :parameters :body)] @@ -891,28 +892,28 @@ :data {:middleware [openapi/openapi-feature rrc/coerce-exceptions-middleware rrc/coerce-request-middleware - rrc/coerce-response-middleware]}}))] - (is (= {"/malli/plus" {:post {:requestBody {:content {:description "body description", - "application/json" {:schema {:type "object", + rrc/coerce-response-middleware]}})) + spec (:body (app {:request-method :get :uri "/openapi.json"}))] + (is (= {"/malli/plus" {:post {:requestBody {:description "body description", + :content {"application/json" {:schema {:type "object", :properties {:x {:type "integer"}, :y {:type "integer"}}, :required [:x :y], :additionalProperties false}, - :examples {"1+1" {:x 1, :y 1}, "1+2" {:x 1, :y 2}}, + :examples {"1+1" {:value {:x 1, :y 1}} + "1+2" {:value {:x 1, :y 2}}}, :example {:x 2, :y 2}}}}, :responses {200 {:description "success", :content {"application/json" {:schema {:type "object", :properties {:total {:type "integer"}}, :required [:total], :additionalProperties false}, - :examples {"2" {:total 2}, "3" {:total 3}}, + :examples {"2" {:value {:total 2}}, + "3" {:value {:total 3}}}, :example {:total 4}}}}}, :summary "plus with body"}}} - (-> {:request-method :get - :uri "/openapi.json"} - (app) - :body - :paths)))) + (:paths spec))) + (is (nil? (validate spec)))) (testing "ref schemas" (let [registry (merge (mc/base-schemas) (mc/type-schemas)