Skip to main content

salvo_oapi/extract/payload/
form.rs

1use std::fmt::{self, Debug, Display, Formatter};
2use std::ops::{Deref, DerefMut};
3
4use salvo_core::extract::{Extractible, Metadata};
5use salvo_core::{Depot, Request, Writer, async_trait};
6use serde::{Deserialize, Deserializer};
7
8use crate::endpoint::EndpointArgRegister;
9use crate::{Components, Content, Operation, RequestBody, ToRequestBody, ToSchema};
10
11/// Represents the parameters passed by the URI path.
12pub struct FormBody<T>(pub T);
13impl<T> FormBody<T> {
14    /// Consumes self and returns the value of the parameter.
15    pub fn into_inner(self) -> T {
16        self.0
17    }
18}
19
20impl<T> Deref for FormBody<T> {
21    type Target = T;
22
23    fn deref(&self) -> &Self::Target {
24        &self.0
25    }
26}
27
28impl<T> DerefMut for FormBody<T> {
29    fn deref_mut(&mut self) -> &mut Self::Target {
30        &mut self.0
31    }
32}
33
34impl<'de, T> ToRequestBody for FormBody<T>
35where
36    T: Deserialize<'de> + ToSchema,
37{
38    fn to_request_body(components: &mut Components) -> RequestBody {
39        RequestBody::new()
40            .description("Extract form format data from request.")
41            .add_content(
42                "application/x-www-form-urlencoded",
43                Content::new(T::to_schema(components)),
44            )
45            .add_content("multipart/*", Content::new(T::to_schema(components)))
46    }
47}
48
49impl<T> fmt::Debug for FormBody<T>
50where
51    T: Debug,
52{
53    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
54        self.0.fmt(f)
55    }
56}
57
58impl<T: Display> Display for FormBody<T> {
59    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
60        self.0.fmt(f)
61    }
62}
63
64impl<'ex, T> Extractible<'ex> for FormBody<T>
65where
66    T: Deserialize<'ex> + Send,
67{
68    fn metadata() -> &'static Metadata {
69        static METADATA: Metadata = Metadata::new("");
70        &METADATA
71    }
72    async fn extract(
73        req: &'ex mut Request,
74        _depot: &'ex mut Depot,
75    ) -> Result<Self, impl Writer + Send + fmt::Debug + 'static> {
76        req.parse_form().await
77    }
78    async fn extract_with_arg(
79        req: &'ex mut Request,
80        depot: &'ex mut Depot,
81        _arg: &str,
82    ) -> Result<Self, impl Writer + Send + fmt::Debug + 'static> {
83        Self::extract(req, depot).await
84    }
85}
86
87impl<'de, T> Deserialize<'de> for FormBody<T>
88where
89    T: Deserialize<'de>,
90{
91    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
92    where
93        D: Deserializer<'de>,
94    {
95        T::deserialize(deserializer).map(FormBody)
96    }
97}
98
99#[async_trait]
100impl<'de, T> EndpointArgRegister for FormBody<T>
101where
102    T: Deserialize<'de> + ToSchema,
103{
104    fn register(components: &mut Components, operation: &mut Operation, _arg: &str) {
105        let request_body = Self::to_request_body(components);
106        let _ = <T as ToSchema>::to_schema(components);
107        operation.request_body = Some(request_body);
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use std::collections::BTreeMap;
114
115    use assert_json_diff::assert_json_eq;
116    use salvo_core::test::TestClient;
117    use serde_json::json;
118
119    use super::*;
120
121    #[test]
122    fn test_form_body_into_inner() {
123        let form = FormBody::<String>("form_body".to_owned());
124        assert_eq!(form.into_inner(), "form_body".to_owned());
125    }
126
127    #[test]
128    fn test_form_body_deref() {
129        let form = FormBody::<String>("form_body".to_owned());
130        assert_eq!(form.deref(), &"form_body".to_owned());
131    }
132
133    #[test]
134    fn test_form_body_deref_mut() {
135        let mut form = FormBody::<String>("form_body".to_owned());
136        assert_eq!(form.deref_mut(), &mut "form_body".to_owned());
137    }
138
139    #[test]
140    fn test_form_body_to_request_body() {
141        let mut components = Components::default();
142        let request_body = FormBody::<String>::to_request_body(&mut components);
143        assert_json_eq!(
144            request_body,
145            json!({
146                "description": "Extract form format data from request.",
147                "content": {
148                    "application/x-www-form-urlencoded": {
149                        "schema": {
150                            "type": "string"
151                        }
152                    },
153                    "multipart/*": {
154                        "schema": {
155                            "type": "string"
156                        }
157                    }
158                }
159            })
160        );
161    }
162
163    #[test]
164    fn test_form_body_debug() {
165        let form = FormBody::<String>("form_body".to_owned());
166        assert_eq!(format!("{form:?}"), r#""form_body""#);
167    }
168
169    #[test]
170    fn test_form_body_display() {
171        let form = FormBody::<String>("form_body".to_owned());
172        assert_eq!(format!("{form}"), "form_body");
173    }
174
175    #[test]
176    fn test_form_body_metadata() {
177        let metadata = FormBody::<String>::metadata();
178        assert_eq!("", metadata.name);
179    }
180
181    #[tokio::test]
182    async fn test_form_body_extract_with_arg() {
183        let map = BTreeMap::from_iter([("key", "value")]);
184        let mut req = TestClient::post("http://127.0.0.1:8698/")
185            .form(&map)
186            .build();
187        let mut depot = Depot::new();
188        let result =
189            FormBody::<BTreeMap<&str, &str>>::extract_with_arg(&mut req, &mut depot, "key").await;
190        assert_eq!("value", result.unwrap().0["key"]);
191    }
192
193    #[test]
194    fn test_form_body_register() {
195        let mut components = Components::new();
196        let mut operation = Operation::new();
197        FormBody::<String>::register(&mut components, &mut operation, "arg");
198
199        assert_json_eq!(
200            operation,
201            json!({
202                "requestBody": {
203                    "content": {
204                        "application/x-www-form-urlencoded": {
205                            "schema": {
206                                "type": "string"
207                            }
208                        },
209                        "multipart/*": {
210                            "schema": {
211                                "type": "string"
212                            }
213                        }
214                    },
215                    "description": "Extract form format data from request."
216                },
217                "responses": {}
218            })
219        );
220    }
221}