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
use openapi_type::{
indexmap::IndexMap,
openapiv3::{
self, Components, OpenAPI, PathItem, ReferenceOr,
ReferenceOr::{Item, Reference},
Schema, Server
},
OpenapiSchema
};
use parking_lot::RwLock;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct OpenapiInfo {
pub title: String,
pub version: String,
pub urls: Vec<String>
}
#[derive(Clone, Debug)]
pub(crate) struct OpenapiBuilder {
pub(crate) openapi: Arc<RwLock<OpenAPI>>
}
impl OpenapiBuilder {
pub(crate) fn new(info: OpenapiInfo) -> Self {
Self {
openapi: Arc::new(RwLock::new(OpenAPI {
openapi: "3.0.2".to_string(),
info: openapiv3::Info {
title: info.title,
version: info.version,
..Default::default()
},
servers: info
.urls
.into_iter()
.map(|url| Server {
url,
..Default::default()
})
.collect(),
..Default::default()
}))
}
}
pub(crate) fn remove_path(&mut self, path: &str) -> PathItem {
let mut openapi = self.openapi.write();
match openapi.paths.paths.swap_remove(path) {
Some(Item(item)) => item,
_ => PathItem::default()
}
}
pub(crate) fn add_path<Path: ToString>(&mut self, path: Path, item: PathItem) {
let mut openapi = self.openapi.write();
openapi.paths.paths.insert(path.to_string(), Item(item));
}
fn add_schema_impl(&mut self, name: String, mut schema: OpenapiSchema) {
self.add_schema_dependencies(&mut schema.dependencies);
let mut openapi = self.openapi.write();
match &mut openapi.components {
Some(comp) => {
comp.schemas.insert(name, Item(schema.schema));
},
None => {
let mut comp = Components::default();
comp.schemas.insert(name, Item(schema.schema));
openapi.components = Some(comp);
}
};
}
fn add_schema_dependencies(&mut self, dependencies: &mut IndexMap<String, OpenapiSchema>) {
let keys: Vec<String> = dependencies.keys().map(|k| k.to_string()).collect();
for dep in keys {
let dep_schema = dependencies.swap_remove(&dep);
if let Some(dep_schema) = dep_schema {
self.add_schema_impl(dep, dep_schema);
}
}
}
pub(crate) fn add_schema(&mut self, mut schema: OpenapiSchema) -> ReferenceOr<Schema> {
match schema.schema.schema_data.title.clone() {
Some(name) => {
let reference = Reference {
reference: format!("#/components/schemas/{name}")
};
self.add_schema_impl(name, schema);
reference
},
None => {
self.add_schema_dependencies(&mut schema.dependencies);
Item(schema.schema)
}
}
}
}
#[cfg(test)]
#[allow(dead_code)]
mod test {
use super::*;
use openapi_type::OpenapiType;
#[derive(OpenapiType)]
struct Message {
msg: String
}
#[derive(OpenapiType)]
struct Messages {
msgs: Vec<Message>
}
fn info() -> OpenapiInfo {
OpenapiInfo {
title: "TEST CASE".to_owned(),
version: "1.2.3".to_owned(),
urls: vec![
"http://localhost:1234".to_owned(),
"https://example.org".to_owned(),
]
}
}
fn openapi(builder: OpenapiBuilder) -> OpenAPI {
Arc::try_unwrap(builder.openapi).unwrap().into_inner()
}
#[test]
fn new_builder() {
let info = info();
let builder = OpenapiBuilder::new(info.clone());
let openapi = openapi(builder);
assert_eq!(info.title, openapi.info.title);
assert_eq!(info.version, openapi.info.version);
assert_eq!(info.urls.len(), openapi.servers.len());
}
#[test]
fn add_schema() {
let mut builder = OpenapiBuilder::new(info());
builder.add_schema(<Option<Messages>>::schema());
let openapi = openapi(builder);
assert_eq!(
openapi.components.clone().unwrap_or_default().schemas["Message"],
ReferenceOr::Item(Message::schema().schema)
);
assert_eq!(
openapi.components.clone().unwrap_or_default().schemas["Messages"],
ReferenceOr::Item(Messages::schema().schema)
);
}
}