Skip to main content

kynos_openapi/model/body/
encoding.rs

1//! The Encoding Object.
2
3use serde::{Deserialize, Serialize};
4
5use crate::{
6    Map,
7    model::{
8        extensions::Extensions,
9        parameter::{header::Header, style::EncodingStyle},
10        reference::RefOr,
11    },
12};
13
14/// How a single property of a `multipart` or form-urlencoded body is encoded.
15#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
16pub struct Encoding {
17    /// The media type of the property, or a comma-separated list of them.
18    #[serde(
19        rename = "contentType",
20        default,
21        skip_serializing_if = "Option::is_none"
22    )]
23    pub content_type: Option<String>,
24
25    /// Headers accompanying this part. `multipart` only.
26    ///
27    /// A `Content-Type` entry here is ignored, since
28    /// [`content_type`](Encoding::content_type) states it.
29    #[serde(default, skip_serializing_if = "Map::is_empty")]
30    pub headers: Map<RefOr<Header>>,
31
32    /// How the property value is serialized.
33    ///
34    /// The specification gives this the query parameter styles, so a style a
35    /// query parameter could not take is one this field cannot hold.
36    #[serde(default, skip_serializing_if = "Option::is_none")]
37    pub style: Option<EncodingStyle>,
38
39    /// Whether an array or object generates one entry per member.
40    #[serde(default, skip_serializing_if = "Option::is_none")]
41    pub explode: Option<bool>,
42
43    /// Whether reserved URI characters may appear unencoded.
44    #[serde(
45        rename = "allowReserved",
46        default,
47        skip_serializing_if = "Option::is_none"
48    )]
49    pub allow_reserved: Option<bool>,
50
51    /// Encoding for the properties of a nested `multipart` part.
52    ///
53    /// Introduced in OpenAPI 3.2.
54    #[cfg(feature = "openapi32")]
55    #[serde(default, skip_serializing_if = "Map::is_empty")]
56    pub encoding: Map<Encoding>,
57
58    /// Positional encoding for the leading items of a nested part.
59    ///
60    /// Introduced in OpenAPI 3.2.
61    #[cfg(feature = "openapi32")]
62    #[serde(
63        rename = "prefixEncoding",
64        default,
65        skip_serializing_if = "Option::is_none"
66    )]
67    pub prefix_encoding: Option<Vec<Encoding>>,
68
69    /// Encoding for every remaining item of a nested part.
70    ///
71    /// Introduced in OpenAPI 3.2.
72    #[cfg(feature = "openapi32")]
73    #[serde(
74        rename = "itemEncoding",
75        default,
76        skip_serializing_if = "Option::is_none"
77    )]
78    pub item_encoding: Option<Box<Encoding>>,
79
80    /// Specification extensions.
81    #[serde(flatten)]
82    pub extensions: Extensions,
83}
84
85impl Encoding {
86    /// Encodes a property with the given media type.
87    pub fn new(content_type: impl Into<String>) -> Self {
88        Self {
89            content_type: Some(content_type.into()),
90            ..Self::default()
91        }
92    }
93
94    /// Declares a header accompanying this part.
95    #[must_use]
96    pub fn with_header(mut self, name: impl Into<String>, header: Header) -> Self {
97        self.headers.insert(name.into(), RefOr::Item(header));
98        self
99    }
100}