diff --git a/doc/ring/coercion.md b/doc/ring/coercion.md index 342f1020..ca908bfc 100644 --- a/doc/ring/coercion.md +++ b/doc/ring/coercion.md @@ -37,7 +37,7 @@ Coercion can be attached to route data under `:coercion` key. There can be multi Parameters are defined in route data under `:parameters` key. It's value should be a map of parameter `:type` -> Coercion Schema. -Responses are defined in route data under `:responses` key. It's value should be a map of http status code to a map which can contain `:body` key with Coercion Schema as value. +Responses are defined in route data under `:responses` key. It's value should be a map of http status code to a map which can contain `:body` key with Coercion Schema as value. Additionally, the key `:default` specifies the coercion for other status codes. Below is an example with [Plumatic Schema](https://github.com/plumatic/schema). It defines schemas for `:query`, `:body` and `:path` parameters and for http 200 response `:body`. @@ -54,7 +54,8 @@ Handlers can access the coerced parameters via the `:parameters` key in the requ :parameters {:query {:x s/Int} :body {:y s/Int} :path {:z s/Int}} - :responses {200 {:body {:total PositiveInt}}} + :responses {200 {:body {:total PositiveInt}} + :default {:body {:error s/Str}}} :handler (fn [{:keys [parameters]}] (let [total (+ (-> parameters :query :x) (-> parameters :body :y) diff --git a/examples/openapi/src/example/server.clj b/examples/openapi/src/example/server.clj index 83e3a6b0..9dcb4c06 100644 --- a/examples/openapi/src/example/server.clj +++ b/examples/openapi/src/example/server.clj @@ -69,7 +69,7 @@ {:status 200 :body {:color :red :pineapple true}})} - :post {:summary "Create a pizza | Multiple content-types, multiple examples" + :post {:summary "Create a pizza | Multiple content-types, multiple examples | Default response schema" :request {:description "Create a pizza using json or EDN" :content {"application/json" {:schema [:map [:color :keyword] @@ -83,10 +83,16 @@ :pineapple false})}}}}} :responses {200 {:description "Success" :content {:default {:schema [:map [:success :boolean]] - :example {:success true}}}}} + :example {:success true}}}} + :default {:description "Not success" + :content {:default {:schema [:map [:error :string]] + :example {:error "error"}}}}} :handler (fn [_request] - {:status 200 - :body {:success true}})}}] + (if (< (Math/random) 0.5) + {:status 200 + :body {:success true}} + {:status 500 + :body {:error "an error happened"}}))}}] ["/contact"