utoipa/openapi/
encoding.rs

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