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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#[macro_use]
extern crate serde_derive;
use std::{fs::File, io::Read, path::Path, result::Result as StdResult};
mod error;
pub use error::Error;
mod components;
mod contact;
mod encoding;
mod example;
mod external_doc;
mod flows;
mod header;
mod info;
mod license;
mod link;
mod media_type;
mod media_type_examples;
mod operation;
mod parameter;
mod path_item;
mod request_body;
mod response;
mod schema;
mod security_scheme;
mod server;
mod spec;
mod tag;
mod url;
pub use self::url::*;
pub use components::*;
pub use contact::*;
pub use encoding::*;
pub use example::*;
pub use external_doc::*;
pub use flows::*;
pub use header::*;
pub use info::*;
pub use license::*;
pub use link::*;
pub use media_type::*;
pub use media_type_examples::*;
pub use operation::*;
pub use parameter::*;
pub use path_item::*;
pub use request_body::*;
pub use response::*;
pub use schema::*;
pub use security_scheme::*;
pub use server::*;
pub use spec::*;
pub use tag::*;
pub type Result<T> = StdResult<T, Error>;
pub type OpenApiV3 = Spec;
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(untagged)]
pub enum ObjectOrReference<T> {
Ref {
#[serde(rename = "$ref")]
ref_path: String,
},
Object(T),
}
impl<T> ObjectOrReference<T>
where
T: FromRef,
{
pub fn resolve(&self, spec: &Spec) -> Option<T> {
match self {
Self::Object(component) => Some(component.clone()),
Self::Ref { ref_path } => T::from_ref(&spec, &ref_path),
}
}
}
pub struct RefPath {
kind: String,
name: String,
}
impl RefPath {
pub fn from_str<'a>(path: &'a str) -> Self {
let path = &path[13..];
println!("path = {:?}", &path);
let parts = &path.split('/').collect::<Vec<_>>();
Self {
kind: parts[0].to_owned(),
name: parts[1].to_owned(),
}
}
}
pub trait FromRef: Clone {
fn from_ref(spec: &Spec, path: &str) -> Option<Self>
where
Self: Sized;
}
pub fn from_path<P>(path: P) -> Result<OpenApiV3>
where
P: AsRef<Path>,
{
from_reader(File::open(path)?)
}
pub fn from_reader<R>(read: R) -> Result<OpenApiV3>
where
R: Read,
{
Ok(serde_yaml::from_reader::<R, OpenApiV3>(read)?)
}
pub fn to_yaml(spec: &OpenApiV3) -> Result<String> { Ok(serde_yaml::to_string(spec)?) }
pub fn to_json(spec: &OpenApiV3) -> Result<String> { Ok(serde_json::to_string_pretty(spec)?) }
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
use std::{
fs::{self, read_to_string, File},
io::Write,
};
fn write_to_file<P>(path: P, filename: &str, data: &str)
where
P: AsRef<Path> + std::fmt::Debug,
{
println!(" Saving string to {:?}...", path);
std::fs::create_dir_all(&path).unwrap();
let full_filename = path.as_ref().to_path_buf().join(filename);
let mut f = File::create(&full_filename).unwrap();
f.write_all(data.as_bytes()).unwrap();
}
fn convert_yaml_str_to_json(yaml_str: &str) -> String {
let yaml: serde_yaml::Value = serde_yaml::from_str(yaml_str).unwrap();
let json: serde_json::Value = serde_yaml::from_value(yaml).unwrap();
serde_json::to_string_pretty(&json).unwrap()
}
fn compare_spec_through_json(
input_file: &Path,
save_path_base: &Path,
) -> (String, String, String) {
let spec_yaml_str = read_to_string(&input_file)
.unwrap_or_else(|e| panic!("failed to read contents of {:?}: {}", input_file, e));
let spec_json_str = convert_yaml_str_to_json(&spec_yaml_str);
let parsed_spec = from_path(&input_file).unwrap();
let parsed_spec_json = serde_json::to_value(parsed_spec).unwrap();
let parsed_spec_json_str: String = serde_json::to_string_pretty(&parsed_spec_json).unwrap();
let api_filename = input_file
.file_name()
.unwrap()
.to_str()
.unwrap()
.replace(".yaml", ".json");
let mut save_path = save_path_base.to_path_buf();
save_path.push("yaml_to_json");
write_to_file(&save_path, &api_filename, &spec_json_str);
let mut save_path = save_path_base.to_path_buf();
save_path.push("yaml_to_spec_to_json");
write_to_file(&save_path, &api_filename, &parsed_spec_json_str);
(api_filename, parsed_spec_json_str, spec_json_str)
}
#[test]
fn can_deserialize() {
for entry in fs::read_dir("data/v2").unwrap() {
let path = entry.unwrap().path();
println!("Testing if {:?} is deserializable", path);
from_path(path).unwrap();
}
}
#[test]
fn can_deserialize_and_reserialize_v2() {
let save_path_base: std::path::PathBuf =
["target", "tests", "can_deserialize_and_reserialize_v2"]
.iter()
.collect();
for entry in fs::read_dir("data/v2").unwrap() {
let path = entry.unwrap().path();
println!("Testing if {:?} is deserializable", path);
let (api_filename, parsed_spec_json_str, spec_json_str) =
compare_spec_through_json(&path, &save_path_base);
assert_eq!(
parsed_spec_json_str.lines().collect::<Vec<_>>(),
spec_json_str.lines().collect::<Vec<_>>(),
"contents did not match for api {}",
api_filename
);
}
}
#[test]
fn can_deserialize_and_reserialize_v3() {
let save_path_base: std::path::PathBuf =
["target", "tests", "can_deserialize_and_reserialize_v3"]
.iter()
.collect();
for entry in fs::read_dir("data/v3.0").unwrap() {
let entry = entry.unwrap();
let path = entry.path();
println!("Testing if {:?} is deserializable", path);
let (api_filename, parsed_spec_json_str, spec_json_str) =
compare_spec_through_json(&path, &save_path_base);
assert_eq!(
parsed_spec_json_str.lines().collect::<Vec<_>>(),
spec_json_str.lines().collect::<Vec<_>>(),
"contents did not match for api {}",
api_filename
);
}
}
}