paperclip_core/v3/models/
response.rs

1use super::{v2, Either};
2
3struct OperationResponse<'a> {
4    operation: &'a v2::DefaultOperationRaw,
5    response: &'a v2::DefaultResponseRaw,
6}
7
8impl From<OperationResponse<'_>> for openapiv3::Response {
9    fn from(v2: OperationResponse<'_>) -> Self {
10        openapiv3::Response {
11            description: v2.response.description.clone().unwrap_or_default(),
12            headers: v2
13                .response
14                .headers
15                .iter()
16                .fold(Default::default(), |mut i, b| {
17                    i.insert(
18                        b.0.to_string(),
19                        openapiv3::ReferenceOr::Item(b.1.clone().into()),
20                    );
21                    i
22                }),
23            content: {
24                match v2.response.schema.clone() {
25                    Some(response) => {
26                        let is_file = v2
27                            .response
28                            .schema
29                            .as_ref()
30                            .and_then(|s| s.data_type.map(|d| d == v2::DataType::File))
31                            .unwrap_or_default();
32                        let media = openapiv3::MediaType {
33                            schema: Some(response.into()),
34                            ..Default::default()
35                        };
36
37                        let mut map = openapiv3::Response::default().content;
38                        match v2.operation.produces.as_ref() {
39                            Some(range) => {
40                                for mime in range {
41                                    map.insert(mime.0.to_string(), media.clone());
42                                }
43                            }
44                            None => {
45                                if is_file {
46                                    // perhaps we should be conservative and use "*/*" instead?
47                                    map.insert("multipart/form-data".to_string(), media);
48                                } else {
49                                    map.insert(v2::SpecFormat::Json.mime().0.to_string(), media);
50                                }
51                            }
52                        }
53                        map
54                    }
55                    None => Default::default(),
56                }
57            },
58            extensions: Default::default(),
59            links: Default::default(),
60        }
61    }
62}
63
64pub(crate) struct OperationEitherResponse<'a> {
65    pub(crate) operation: &'a v2::DefaultOperationRaw,
66    pub(crate) response: &'a Either<v2::Reference, v2::DefaultResponseRaw>,
67}
68
69impl From<OperationEitherResponse<'_>> for openapiv3::ReferenceOr<openapiv3::Response> {
70    fn from(v2: OperationEitherResponse<'_>) -> Self {
71        match v2.response {
72            Either::Left(reference) => {
73                let response = openapiv3::Response {
74                    description: "".to_string(),
75                    headers: Default::default(),
76                    content: {
77                        let media = openapiv3::MediaType {
78                            schema: Some(reference.into()),
79                            ..Default::default()
80                        };
81                        let mut map = openapiv3::Response::default().content;
82                        match v2.operation.produces.as_ref() {
83                            Some(range) => {
84                                for mime in range {
85                                    map.insert(mime.0.to_string(), media.clone());
86                                }
87                            }
88                            None => {
89                                // perhaps we should be conservative and use "*/*" instead?
90                                map.insert(v2::SpecFormat::Json.mime().0.to_string(), media);
91                            }
92                        }
93                        map
94                    },
95                    links: Default::default(),
96                    extensions: Default::default(),
97                };
98                openapiv3::ReferenceOr::Item(response)
99            }
100            Either::Right(response) => {
101                let response = OperationResponse {
102                    operation: v2.operation,
103                    response,
104                };
105                openapiv3::ReferenceOr::Item(response.into())
106            }
107        }
108    }
109}
110
111impl From<v2::DefaultResponseRaw> for openapiv3::ReferenceOr<openapiv3::Response> {
112    fn from(v2: v2::DefaultResponseRaw) -> Self {
113        let fake_op = v2::DefaultOperationRaw::default();
114        let response = OperationResponse {
115            operation: &fake_op,
116            response: &v2,
117        };
118        openapiv3::ReferenceOr::Item(response.into())
119    }
120}