utoipa/openapi/example.rs
1//! Implements [OpenAPI Example Object][example] can be used to define examples for [`Response`][response]s and
2//! [`RequestBody`][request_body]s.
3//!
4//! [example]: https://spec.openapis.org/oas/latest.html#example-object
5//! [response]: response/struct.Response.html
6//! [request_body]: request_body/struct.RequestBody.html
7use serde::{Deserialize, Serialize};
8
9use super::{builder, set_value, RefOr};
10
11builder! {
12 /// # Examples
13 ///
14 /// _**Construct a new [`Example`] via builder**_
15 /// ```rust
16 /// # use utoipa::openapi::example::ExampleBuilder;
17 /// let example = ExampleBuilder::new()
18 /// .summary("Example string response")
19 /// .value(Some(serde_json::json!("Example value")))
20 /// .build();
21 /// ```
22 ExampleBuilder;
23
24 /// Implements [OpenAPI Example Object][example].
25 ///
26 /// Example is used on path operations to describe possible response bodies.
27 ///
28 /// [example]: https://spec.openapis.org/oas/latest.html#example-object
29 #[non_exhaustive]
30 #[derive(Serialize, Deserialize, Default, Clone, PartialEq, Eq)]
31 #[cfg_attr(feature = "debug", derive(Debug))]
32 #[serde(rename_all = "camelCase")]
33 pub struct Example {
34 /// Short description for the [`Example`].
35 #[serde(skip_serializing_if = "String::is_empty")]
36 pub summary: String,
37
38 /// Long description for the [`Example`]. Value supports markdown syntax for rich text
39 /// representation.
40 #[serde(skip_serializing_if = "String::is_empty")]
41 pub description: String,
42
43 /// Embedded literal example value. [`Example::value`] and [`Example::external_value`] are
44 /// mutually exclusive.
45 #[serde(skip_serializing_if = "Option::is_none")]
46 pub value: Option<serde_json::Value>,
47
48 /// An URI that points to a literal example value. [`Example::external_value`] provides the
49 /// capability to references an example that cannot be easily included in JSON or YAML.
50 /// [`Example::value`] and [`Example::external_value`] are mutually exclusive.
51 #[serde(skip_serializing_if = "String::is_empty")]
52 pub external_value: String,
53 }
54}
55
56impl Example {
57 /// Construct a new empty [`Example`]. This is effectively same as calling
58 /// [`Example::default`].
59 pub fn new() -> Self {
60 Self::default()
61 }
62}
63
64impl ExampleBuilder {
65 /// Add or change a short description for the [`Example`]. Setting this to empty `String`
66 /// will make it not render in the generated OpenAPI document.
67 pub fn summary<S: Into<String>>(mut self, summary: S) -> Self {
68 set_value!(self summary summary.into())
69 }
70
71 /// Add or change a long description for the [`Example`]. Markdown syntax is supported for rich
72 /// text representation.
73 ///
74 /// Setting this to empty `String` will make it not render in the generated
75 /// OpenAPI document.
76 pub fn description<D: Into<String>>(mut self, description: D) -> Self {
77 set_value!(self description description.into())
78 }
79
80 /// Add or change embedded literal example value. [`Example::value`] and [`Example::external_value`]
81 /// are mutually exclusive.
82 pub fn value(mut self, value: Option<serde_json::Value>) -> Self {
83 set_value!(self value value)
84 }
85
86 /// Add or change an URI that points to a literal example value. [`Example::external_value`]
87 /// provides the capability to references an example that cannot be easily included
88 /// in JSON or YAML. [`Example::value`] and [`Example::external_value`] are mutually exclusive.
89 ///
90 /// Setting this to an empty String will make the field not to render in the generated OpenAPI
91 /// document.
92 pub fn external_value<E: Into<String>>(mut self, external_value: E) -> Self {
93 set_value!(self external_value external_value.into())
94 }
95}
96
97impl From<ExampleBuilder> for RefOr<Example> {
98 fn from(example_builder: ExampleBuilder) -> Self {
99 Self::T(example_builder.build())
100 }
101}