oapi/objects/
server_variable.rs

1use super::*;
2
3/// ## The OpenApi [server variables](https://swagger.io/specification/#server-variable-object)
4#[derive(
5    Debug, PartialEq, Serialize, Deserialize, Clone, Getters, Sparsable, OApiCheck, OApiExt,
6)]
7#[getset(get = "pub")]
8#[serde(rename_all = "camelCase")]
9#[oapi(handler = "self._oapi_check")]
10pub struct OApiServerVariable {
11    /// The possible values for the variable
12    #[serde(default)]
13    #[serde(rename = "enum")]
14    enum_: Vec<String>,
15    /// The default value for this variable
16    default: String,
17    /// A description of this variable
18    description: Option<String>,
19    /// Extensions, if any
20    #[serde(flatten)]
21    #[getset(get)]
22    _extension: HashMap<String, Value>,
23}
24
25impl OApiServerVariable {
26    fn _oapi_check(
27        &self,
28        _root: &SparseRoot<OApiDocument>,
29        bread_crumb: &mut Vec<String>,
30    ) -> Result<(), OApiError> {
31        if self.enum_.is_empty() {
32            bread_crumb.push("enum".to_string());
33            return Err(OApiError::OApiCheck(
34                crate::check::connect_bread_crumbs(bread_crumb),
35                "Enum shoud not be empty".to_string(),
36            ));
37        }
38        if !self.enum_.contains(&self.default) {
39            bread_crumb.push("default".to_string());
40            return Err(OApiError::OApiCheck(
41                crate::check::connect_bread_crumbs(bread_crumb),
42                "default value should be present in `enum`".to_string(),
43            ));
44        }
45        Ok(())
46    }
47}