mirror of
https://github.com/metosin/reitit.git
synced 2025-12-21 10:01:11 +00:00
doc: document :responses :default
This commit is contained in:
parent
dd835e73a8
commit
8058cecae0
2 changed files with 13 additions and 6 deletions
|
|
@ -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.
|
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`.
|
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}
|
:parameters {:query {:x s/Int}
|
||||||
:body {:y s/Int}
|
:body {:y s/Int}
|
||||||
:path {:z 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]}]
|
:handler (fn [{:keys [parameters]}]
|
||||||
(let [total (+ (-> parameters :query :x)
|
(let [total (+ (-> parameters :query :x)
|
||||||
(-> parameters :body :y)
|
(-> parameters :body :y)
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,7 @@
|
||||||
{:status 200
|
{:status 200
|
||||||
:body {:color :red
|
:body {:color :red
|
||||||
:pineapple true}})}
|
: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"
|
:request {:description "Create a pizza using json or EDN"
|
||||||
:content {"application/json" {:schema [:map
|
:content {"application/json" {:schema [:map
|
||||||
[:color :keyword]
|
[:color :keyword]
|
||||||
|
|
@ -83,10 +83,16 @@
|
||||||
:pineapple false})}}}}}
|
:pineapple false})}}}}}
|
||||||
:responses {200 {:description "Success"
|
:responses {200 {:description "Success"
|
||||||
:content {:default {:schema [:map [:success :boolean]]
|
: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]
|
:handler (fn [_request]
|
||||||
|
(if (< (Math/random) 0.5)
|
||||||
{:status 200
|
{:status 200
|
||||||
:body {:success true}})}}]
|
:body {:success true}}
|
||||||
|
{:status 500
|
||||||
|
:body {:error "an error happened"}}))}}]
|
||||||
|
|
||||||
|
|
||||||
["/contact"
|
["/contact"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue