utoipa/openapi/
server.rs

1//! Implements [OpenAPI Server Object][server] types to configure target servers.
2//!
3//! OpenAPI will implicitly add [`Server`] with `url = "/"` to [`OpenApi`][openapi] when no servers
4//! are defined.
5//!
6//! [`Server`] can be used to alter connection url for _**path operations**_. It can be a
7//! relative path e.g `/api/v1` or valid http url e.g. `http://alternative.api.com/api/v1`.
8//!
9//! Relative path will append to the **sever address** so the connection url for _**path operations**_
10//! will become `server address + relative path`.
11//!
12//! Optionally it also supports parameter substitution with `{variable}` syntax.
13//!
14//! See [`Modify`][modify] trait for details how add servers to [`OpenApi`][openapi].
15//!
16//! # Examples
17//!
18//! Create new server with relative path.
19//! ```rust
20//! # use utoipa::openapi::server::Server;
21//! Server::new("/api/v1");
22//! ```
23//!
24//! Create server with custom url using a builder.
25//! ```rust
26//! # use utoipa::openapi::server::ServerBuilder;
27//! ServerBuilder::new().url("https://alternative.api.url.test/api").build();
28//! ```
29//!
30//! Create server with builder and variable substitution.
31//! ```rust
32//! # use utoipa::openapi::server::{ServerBuilder, ServerVariableBuilder};
33//! ServerBuilder::new().url("/api/{version}/{username}")
34//!     .parameter("version", ServerVariableBuilder::new()
35//!         .enum_values(Some(["v1", "v2"]))
36//!         .default_value("v1"))
37//!     .parameter("username", ServerVariableBuilder::new()
38//!         .default_value("the_user")).build();
39//! ```
40//!
41//! [server]: https://spec.openapis.org/oas/latest.html#server-object
42//! [openapi]: ../struct.OpenApi.html
43//! [modify]: ../../trait.Modify.html
44use std::{collections::BTreeMap, iter};
45
46use serde::{Deserialize, Serialize};
47
48use super::{builder, set_value};
49
50builder! {
51    ServerBuilder;
52
53    /// Represents target server object. It can be used to alter server connection for
54    /// _**path operations**_.
55    ///
56    /// By default OpenAPI will implicitly implement [`Server`] with `url = "/"` if no servers is provided to
57    /// the [`OpenApi`][openapi].
58    ///
59    /// [openapi]: ../struct.OpenApi.html
60    #[non_exhaustive]
61    #[derive(Serialize, Deserialize, Default, Clone, PartialEq, Eq)]
62    #[cfg_attr(feature = "debug", derive(Debug))]
63    #[serde(rename_all = "camelCase")]
64    pub struct Server {
65        /// Target url of the [`Server`]. It can be valid http url or relative path.
66        ///
67        /// Url also supports variable substitution with `{variable}` syntax. The substitutions
68        /// then can be configured with [`Server::variables`] map.
69        pub url: String,
70
71        /// Optional description describing the target server url. Description supports markdown syntax.
72        #[serde(skip_serializing_if = "Option::is_none")]
73        pub description: Option<String>,
74
75        /// Optional map of variable name and its substitution value used in [`Server::url`].
76        #[serde(skip_serializing_if = "Option::is_none")]
77        pub variables: Option<BTreeMap<String, ServerVariable>>,
78    }
79}
80
81impl Server {
82    /// Construct a new [`Server`] with given url. Url can be valid http url or context path of the url.
83    ///
84    /// If url is valid http url then all path operation request's will be forwarded to the selected [`Server`].
85    ///
86    /// If url is path of url e.g. `/api/v1` then the url will be appended to the servers address and the
87    /// operations will be forwarded to location `server address + url`.
88    ///
89    ///
90    /// # Examples
91    ///
92    /// Create new server with url path.
93    /// ```rust
94    /// # use utoipa::openapi::server::Server;
95    ///  Server::new("/api/v1");
96    /// ```
97    ///
98    /// Create new server with alternative server.
99    /// ```rust
100    /// # use utoipa::openapi::server::Server;
101    ///  Server::new("https://alternative.pet-api.test/api/v1");
102    /// ```
103    pub fn new<S: Into<String>>(url: S) -> Self {
104        Self {
105            url: url.into(),
106            ..Default::default()
107        }
108    }
109}
110
111impl ServerBuilder {
112    /// Add url to the target [`Server`].
113    pub fn url<U: Into<String>>(mut self, url: U) -> Self {
114        set_value!(self url url.into())
115    }
116
117    /// Add or change description of the [`Server`].
118    pub fn description<S: Into<String>>(mut self, description: Option<S>) -> Self {
119        set_value!(self description description.map(|description| description.into()))
120    }
121
122    /// Add parameter to [`Server`] which is used to substitute values in [`Server::url`].
123    ///
124    /// * `name` Defines name of the parameter which is being substituted within the url. If url has
125    ///   `{username}` substitution then the name should be `username`.
126    /// * `parameter` Use [`ServerVariableBuilder`] to define how the parameter is being substituted
127    ///   within the url.
128    pub fn parameter<N: Into<String>, V: Into<ServerVariable>>(
129        mut self,
130        name: N,
131        variable: V,
132    ) -> Self {
133        match self.variables {
134            Some(ref mut variables) => {
135                variables.insert(name.into(), variable.into());
136            }
137            None => {
138                self.variables = Some(BTreeMap::from_iter(iter::once((
139                    name.into(),
140                    variable.into(),
141                ))))
142            }
143        }
144
145        self
146    }
147}
148
149builder! {
150    ServerVariableBuilder;
151
152    /// Implements [OpenAPI Server Variable][server_variable] used to substitute variables in [`Server::url`].
153    ///
154    /// [server_variable]: https://spec.openapis.org/oas/latest.html#server-variable-object
155    #[non_exhaustive]
156    #[derive(Serialize, Deserialize, Default, Clone, PartialEq, Eq)]
157    #[cfg_attr(feature = "debug", derive(Debug))]
158    pub struct ServerVariable {
159        /// Default value used to substitute parameter if no other value is being provided.
160        #[serde(rename = "default")]
161        default_value: String,
162
163        /// Optional description describing the variable of substitution. Markdown syntax is supported.
164        #[serde(skip_serializing_if = "Option::is_none")]
165        description: Option<String>,
166
167        /// Enum values can be used to limit possible options for substitution. If enum values is used
168        /// the [`ServerVariable::default_value`] must contain one of the enum values.
169        #[serde(rename = "enum", skip_serializing_if = "Option::is_none")]
170        enum_values: Option<Vec<String>>,
171    }
172}
173
174impl ServerVariableBuilder {
175    /// Add default value for substitution.
176    pub fn default_value<S: Into<String>>(mut self, default_value: S) -> Self {
177        set_value!(self default_value default_value.into())
178    }
179
180    /// Add or change description of substituted parameter.
181    pub fn description<S: Into<String>>(mut self, description: Option<S>) -> Self {
182        set_value!(self description description.map(|description| description.into()))
183    }
184
185    /// Add or change possible values used to substitute parameter.
186    pub fn enum_values<I: IntoIterator<Item = V>, V: Into<String>>(
187        mut self,
188        enum_values: Option<I>,
189    ) -> Self {
190        set_value!(self enum_values enum_values
191            .map(|enum_values| enum_values.into_iter().map(|value| value.into()).collect()))
192    }
193}
194
195#[cfg(test)]
196mod tests {
197    use super::*;
198
199    macro_rules! test_fn {
200        ($name:ident: $schema:expr; $expected:literal) => {
201            #[test]
202            fn $name() {
203                let value = serde_json::to_value($schema).unwrap();
204                let expected_value: serde_json::Value = serde_json::from_str($expected).unwrap();
205
206                assert_eq!(
207                    value,
208                    expected_value,
209                    "testing serializing \"{}\": \nactual:\n{}\nexpected:\n{}",
210                    stringify!($name),
211                    value,
212                    expected_value
213                );
214
215                println!("{}", &serde_json::to_string_pretty(&$schema).unwrap());
216            }
217        };
218    }
219
220    test_fn! {
221    create_server_with_builder_and_variable_substitution:
222    ServerBuilder::new().url("/api/{version}/{username}")
223        .parameter("version", ServerVariableBuilder::new()
224            .enum_values(Some(["v1", "v2"]))
225            .description(Some("api version"))
226            .default_value("v1"))
227        .parameter("username", ServerVariableBuilder::new()
228            .default_value("the_user")).build();
229    r###"{
230  "url": "/api/{version}/{username}",
231  "variables": {
232      "version": {
233          "enum": ["v1", "v2"],
234          "default": "v1",
235          "description": "api version"
236      },
237      "username": {
238          "default": "the_user"
239      }
240  }
241}"###
242    }
243}