Skip to main content

salvo_oapi/openapi/
encoding.rs

1//! Implements encoding object for content.
2
3use serde::{Deserialize, Serialize};
4
5use super::parameter::ParameterStyle;
6use super::{Header, PropMap};
7
8/// A single encoding definition applied to a single schema [`Object
9/// property`](crate::openapi::schema::Object::properties).
10#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
11#[serde(rename_all = "camelCase")]
12#[non_exhaustive]
13pub struct Encoding {
14    /// The Content-Type for encoding a specific property. Default value depends on the property
15    /// type: for string with format being binary – `application/octet-stream`; for other primitive
16    /// types – `text/plain`; for object - `application/json`; for array – the default is defined
17    /// based on the inner type. The value can be a specific media type (e.g. `application/json`),
18    /// a wildcard media type (e.g. `image/*`), or a comma-separated list of the two types.
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub content_type: Option<String>,
21
22    /// A map allowing additional information to be provided as headers, for example
23    /// Content-Disposition. Content-Type is described separately and SHALL be ignored in this
24    /// section. This property SHALL be ignored if the request body media type is not a multipart.
25    #[serde(skip_serializing_if = "PropMap::is_empty")]
26    pub headers: PropMap<String, Header>,
27
28    /// Describes how a specific property value will be serialized depending on its type. See
29    /// Parameter Object for details on the style property. The behavior follows the same values as
30    /// query parameters, including default values. This property SHALL be ignored if the request
31    /// body media type is not `application/x-www-form-urlencoded`.
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub style: Option<ParameterStyle>,
34
35    /// When this is true, property values of type array or object generate separate parameters for
36    /// each value of the array, or key-value-pair of the map. For other types of properties this
37    /// property has no effect. When style is form, the default value is true. For all other
38    /// styles, the default value is false. This property SHALL be ignored if the request body
39    /// media type is not `application/x-www-form-urlencoded`.
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub explode: Option<bool>,
42
43    /// Determines whether the parameter value SHOULD allow reserved characters, as defined by
44    /// RFC3986 `:/?#[]@!$&'()*+,;=` to be included without percent-encoding. The default value is
45    /// false. This property SHALL be ignored if the request body media type is not
46    /// `application/x-www-form-urlencoded`.
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub allow_reserved: Option<bool>,
49}
50
51impl Encoding {
52    /// Set the content type. See [`Encoding::content_type`].
53    pub fn content_type<S: Into<String>>(mut self, content_type: S) -> Self {
54        self.content_type = Some(content_type.into());
55        self
56    }
57
58    /// Add a [`Header`]. See [`Encoding::headers`].
59    pub fn header<S: Into<String>, H: Into<Header>>(mut self, header_name: S, header: H) -> Self {
60        self.headers.insert(header_name.into(), header.into());
61
62        self
63    }
64
65    /// Set the style [`ParameterStyle`]. See [`Encoding::style`].
66    pub fn style(mut self, style: ParameterStyle) -> Self {
67        self.style = Some(style);
68        self
69    }
70
71    /// Set the explode. See [`Encoding::explode`].
72    pub fn explode(mut self, explode: bool) -> Self {
73        self.explode = Some(explode);
74        self
75    }
76
77    /// Set the allow reserved. See [`Encoding::allow_reserved`].
78    pub fn allow_reserved(mut self, allow_reserved: bool) -> Self {
79        self.allow_reserved = Some(allow_reserved);
80        self
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use assert_json_diff::assert_json_eq;
87    use serde_json::json;
88
89    use super::*;
90
91    #[test]
92    fn test_encoding_default() {
93        let encoding = Encoding::default();
94        assert_json_eq!(encoding, json!({}));
95    }
96
97    #[test]
98    fn test_build_encoding() {
99        let encoding = Encoding::default()
100            .content_type("application/json")
101            .header("header1", Header::default())
102            .style(ParameterStyle::Simple)
103            .explode(true)
104            .allow_reserved(false);
105
106        assert_json_eq!(
107            encoding,
108            json!({
109              "contentType": "application/json",
110              "headers": {
111                "header1": {
112                  "schema": {
113                    "type": "string"
114                  }
115                }
116              },
117              "style": "simple",
118              "explode": true,
119              "allowReserved": false
120            })
121        );
122    }
123}