paperclip_core/v3/models/
parameter.rs1use super::v2;
2
3pub(crate) fn non_body_parameter_to_v3_parameter(
5 form_data: bool,
6 v2: &v2::DefaultParameterRaw,
7) -> Option<openapiv3::Schema> {
8 match v2.data_type {
9 Some(data_type) => {
10 let schema_kind = match data_type {
11 v2::DataType::Integer => {
12 openapiv3::SchemaKind::Type(openapiv3::Type::Integer(openapiv3::IntegerType {
13 format: match &v2.format {
14 None => openapiv3::VariantOrUnknownOrEmpty::Empty,
15 Some(format) => match format {
16 v2::DataTypeFormat::Int32 => {
17 openapiv3::VariantOrUnknownOrEmpty::Item(
18 openapiv3::IntegerFormat::Int32,
19 )
20 }
21 v2::DataTypeFormat::Int64 => {
22 openapiv3::VariantOrUnknownOrEmpty::Item(
23 openapiv3::IntegerFormat::Int64,
24 )
25 }
26 other => {
27 debug_assert!(false, "Invalid data type format: {:?}", other);
28 openapiv3::VariantOrUnknownOrEmpty::Empty
29 }
30 },
31 },
32 multiple_of: v2.multiple_of.map(|v| v as i64),
33 exclusive_minimum: v2.exclusive_minimum.unwrap_or_default(),
34 exclusive_maximum: v2.exclusive_maximum.unwrap_or_default(),
35 minimum: v2.minimum.map(|v| v as i64),
36 maximum: v2.maximum.map(|v| v as i64),
37 enumeration: v2
38 .enum_
39 .iter()
40 .cloned()
41 .map(|v| serde_json::from_value(v).unwrap_or_default())
42 .collect(),
43 }))
44 }
45 v2::DataType::Number => {
46 openapiv3::SchemaKind::Type(openapiv3::Type::Number(openapiv3::NumberType {
47 format: match &v2.format {
48 None => openapiv3::VariantOrUnknownOrEmpty::Empty,
49 Some(format) => match format {
50 v2::DataTypeFormat::Float => {
51 openapiv3::VariantOrUnknownOrEmpty::Item(
52 openapiv3::NumberFormat::Float {},
53 )
54 }
55 v2::DataTypeFormat::Double => {
56 openapiv3::VariantOrUnknownOrEmpty::Item(
57 openapiv3::NumberFormat::Double {},
58 )
59 }
60 other => {
61 debug_assert!(false, "Invalid data type format: {:?}", other);
62 openapiv3::VariantOrUnknownOrEmpty::Empty
63 }
64 },
65 },
66 multiple_of: v2.multiple_of.map(|v| v as f64),
67 exclusive_minimum: v2.exclusive_minimum.unwrap_or_default(),
68 exclusive_maximum: v2.exclusive_maximum.unwrap_or_default(),
69 minimum: v2.minimum.map(|v| v as f64),
70 maximum: v2.maximum.map(|v| v as f64),
71 enumeration: v2
72 .enum_
73 .iter()
74 .cloned()
75 .map(|v| serde_json::from_value(v).unwrap_or_default())
76 .collect(),
77 }))
78 }
79 v2::DataType::String => {
80 openapiv3::SchemaKind::Type(openapiv3::Type::String(openapiv3::StringType {
81 format: match &v2.format {
82 None => openapiv3::VariantOrUnknownOrEmpty::Empty,
83 Some(format) => match format {
84 v2::DataTypeFormat::Byte => {
85 openapiv3::VariantOrUnknownOrEmpty::Item(
86 openapiv3::StringFormat::Byte,
87 )
88 }
89 v2::DataTypeFormat::Binary => {
90 openapiv3::VariantOrUnknownOrEmpty::Item(
91 openapiv3::StringFormat::Binary,
92 )
93 }
94 v2::DataTypeFormat::Date => {
95 openapiv3::VariantOrUnknownOrEmpty::Item(
96 openapiv3::StringFormat::Date,
97 )
98 }
99 v2::DataTypeFormat::DateTime => {
100 openapiv3::VariantOrUnknownOrEmpty::Item(
101 openapiv3::StringFormat::DateTime,
102 )
103 }
104 v2::DataTypeFormat::Password => {
105 openapiv3::VariantOrUnknownOrEmpty::Item(
106 openapiv3::StringFormat::Password,
107 )
108 }
109 v2::DataTypeFormat::Other => {
110 debug_assert!(false, "Invalid data type format: other");
111 openapiv3::VariantOrUnknownOrEmpty::Unknown(
112 v2::DataTypeFormat::Other.to_string(),
113 )
114 }
115 others => {
116 openapiv3::VariantOrUnknownOrEmpty::Unknown(others.to_string())
117 }
118 },
119 },
120 pattern: v2.pattern.clone(),
121 enumeration: v2
122 .enum_
123 .iter()
124 .cloned()
125 .map(|v| serde_json::from_value(v).unwrap_or_default())
126 .collect(),
127 min_length: v2.min_length.map(|v| v as usize),
128 max_length: v2.max_length.map(|v| v as usize),
129 }))
130 }
131 v2::DataType::Boolean => {
132 openapiv3::SchemaKind::Type(openapiv3::Type::Boolean(Default::default()))
133 }
134 v2::DataType::Array => {
135 openapiv3::SchemaKind::Type(openapiv3::Type::Array(openapiv3::ArrayType {
136 items: v2.items.as_ref().map(|items| items.clone().into()),
137 min_items: v2.min_items.map(|v| v as usize),
138 max_items: v2.max_items.map(|v| v as usize),
139 unique_items: v2.unique_items,
140 }))
141 }
142 v2::DataType::Object => {
143 return None;
145 }
146 v2::DataType::File => {
147 if !form_data {
148 return None;
150 }
151 openapiv3::SchemaKind::Type(openapiv3::Type::String(openapiv3::StringType {
152 format: openapiv3::VariantOrUnknownOrEmpty::Item(
153 openapiv3::StringFormat::Binary,
154 ),
155 ..Default::default()
156 }))
157 }
158 };
159 let schema_data = if form_data {
160 openapiv3::SchemaData {
162 description: v2.description.clone(),
163 default: v2.default.clone(),
164 ..Default::default()
165 }
166 } else {
167 openapiv3::SchemaData::default()
169 };
170 Some(openapiv3::Schema {
171 schema_data,
172 schema_kind,
173 })
174 }
175 None => None,
176 }
177}