paperclip_core/v3/models/
operation.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use super::{v2, Either, OperationEitherResponse};

impl From<v2::Operation<v2::DefaultParameterRaw, v2::DefaultResponseRaw>> for openapiv3::Operation {
    fn from(v2: v2::Operation<v2::DefaultParameterRaw, v2::DefaultResponseRaw>) -> Self {
        let mut request_body: Option<openapiv3::RequestBody> = None;
        let mut form_data: Option<openapiv3::AnySchema> = None;
        let v2v = v2.clone();

        let parameters = v2
            .parameters
            .iter()
            .filter_map(|p| match p {
                Either::Left(reference) => Some(reference.into()),
                Either::Right(parameter) => {
                    let either: Either<
                        openapiv3::Parameter,
                        Either<openapiv3::RequestBody, Option<openapiv3::Schema>>,
                    > = parameter.clone().into();
                    match either {
                        Either::Right(r) => match r {
                            Either::Left(l) => {
                                request_body = Some(l);
                                None
                            }
                            Either::Right(Some(schema)) => {
                                if let Some(any) = form_data.as_mut() {
                                    any.properties.insert(
                                        parameter.name.clone(),
                                        openapiv3::ReferenceOr::Item(Box::new(schema)),
                                    );
                                } else {
                                    let mut any = openapiv3::AnySchema::default();
                                    if parameter.required {
                                        any.required.push(parameter.name.clone());
                                    }
                                    any.properties.insert(
                                        parameter.name.clone(),
                                        openapiv3::ReferenceOr::Item(Box::new(schema)),
                                    );
                                    form_data = Some(any);
                                }
                                None
                            }
                            Either::Right(None) => None,
                        },
                        Either::Left(parameter) => Some(openapiv3::ReferenceOr::Item(parameter)),
                    }
                }
            })
            .collect();

        let request_body = if let Some(request_body) = request_body {
            Some(openapiv3::ReferenceOr::Item(request_body))
        } else if let Some(form_data) = form_data {
            let mut request_body = openapiv3::RequestBody::default();
            match v2.consumes {
                None => None,
                Some(consumes) => {
                    for media in consumes {
                        request_body.content.insert(media.0.to_string(), {
                            openapiv3::MediaType {
                                schema: Some(openapiv3::ReferenceOr::Item(openapiv3::Schema {
                                    schema_data: Default::default(),
                                    schema_kind: openapiv3::SchemaKind::Any(form_data.clone()),
                                })),
                                ..Default::default()
                            }
                        });
                    }

                    Some(openapiv3::ReferenceOr::Item(request_body))
                }
            }
        } else {
            None
        };

        openapiv3::Operation {
            tags: v2.tags,
            summary: v2.summary,
            description: v2.description,
            external_docs: None,
            operation_id: v2.operation_id,
            parameters,
            request_body,
            responses: openapiv3::Responses {
                default: None,
                responses: v2.responses.iter().fold(
                    openapiv3::Responses::default().responses,
                    |mut i, (k, v)| {
                        if let Ok(code) = k.parse::<u16>() {
                            let code = openapiv3::StatusCode::Code(code);
                            i.insert(
                                code,
                                OperationEitherResponse {
                                    operation: &v2v,
                                    response: v,
                                }
                                .into(),
                            );
                        }
                        i
                    },
                ),
                ..Default::default()
            },
            deprecated: v2.deprecated,
            security: if v2.security.is_empty() {
                None
            } else {
                Some(
                    v2.security
                        .iter()
                        .map(|s| {
                            s.iter().fold(
                                openapiv3::SecurityRequirement::default(),
                                |mut i, (k, v)| {
                                    i.insert(k.to_string(), v.clone());
                                    i
                                },
                            )
                        })
                        .collect(),
                )
            },
            servers: vec![],
            extensions: Default::default(),
            callbacks: Default::default(),
        }
    }
}