-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathopenapi.rs
More file actions
471 lines (426 loc) · 14.6 KB
/
openapi.rs
File metadata and controls
471 lines (426 loc) · 14.6 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! `OpenAPI` document structure definitions
use crate::route::PathItem;
use crate::schema::{Components, ExternalDocumentation};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};
/// `OpenAPI` document version
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum OpenApiVersion {
#[serde(rename = "3.0.0")]
V3_0_0,
#[serde(rename = "3.0.1")]
V3_0_1,
#[serde(rename = "3.0.2")]
V3_0_2,
#[serde(rename = "3.0.3")]
V3_0_3,
#[serde(rename = "3.1.0")]
#[default]
V3_1_0,
}
/// Contact information
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Contact {
/// Contact name
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
/// Contact URL
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
/// Contact email
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
}
/// License information
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct License {
/// License name
pub name: String,
/// License URL
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
}
/// API information
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Info {
/// API title
pub title: String,
/// API version
pub version: String,
/// API description
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// Terms of service URL
#[serde(skip_serializing_if = "Option::is_none")]
pub terms_of_service: Option<String>,
/// Contact information
#[serde(skip_serializing_if = "Option::is_none")]
pub contact: Option<Contact>,
/// License information
#[serde(skip_serializing_if = "Option::is_none")]
pub license: Option<License>,
/// Summary
#[serde(skip_serializing_if = "Option::is_none")]
pub summary: Option<String>,
}
/// Server variable
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ServerVariable {
/// Default value
pub default: String,
/// Enum values
#[serde(skip_serializing_if = "Option::is_none")]
pub r#enum: Option<Vec<String>>,
/// Description
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}
/// Server information
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Server {
/// Server URL
pub url: String,
/// Server description
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// Server variables
#[serde(skip_serializing_if = "Option::is_none")]
pub variables: Option<HashMap<String, ServerVariable>>,
}
/// Tag definition
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Tag {
/// Tag name
pub name: String,
/// Tag description
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// External documentation
#[serde(skip_serializing_if = "Option::is_none")]
pub external_docs: Option<ExternalDocumentation>,
}
/// `OpenAPI` document (root structure)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OpenApi {
/// `OpenAPI` version
pub openapi: OpenApiVersion,
/// API information
pub info: Info,
/// Server list
#[serde(skip_serializing_if = "Option::is_none")]
pub servers: Option<Vec<Server>>,
/// Path definitions
pub paths: BTreeMap<String, PathItem>,
/// Components (reusable components)
#[serde(skip_serializing_if = "Option::is_none")]
pub components: Option<Components>,
/// Security requirements
#[serde(skip_serializing_if = "Option::is_none")]
pub security: Option<Vec<HashMap<String, Vec<String>>>>,
/// Tag definitions
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<Vec<Tag>>,
/// External documentation
#[serde(skip_serializing_if = "Option::is_none")]
pub external_docs: Option<ExternalDocumentation>,
}
impl OpenApi {
/// Merge another `OpenAPI` document into this one.
/// Paths, schemas, and tags from `other` are added to `self`.
/// If there are conflicts, `self` takes precedence.
pub fn merge(&mut self, other: Self) {
// Merge paths (self takes precedence on conflict)
for (path, item) in other.paths {
self.paths.entry(path).or_insert(item);
}
// Merge components
if let Some(other_components) = other.components {
let self_components = self.components.get_or_insert(Components {
schemas: None,
responses: None,
parameters: None,
examples: None,
request_bodies: None,
headers: None,
security_schemes: None,
});
// Merge schemas
if let Some(other_schemas) = other_components.schemas {
let self_schemas = self_components.schemas.get_or_insert_with(BTreeMap::new);
for (name, schema) in other_schemas {
self_schemas.entry(name).or_insert(schema);
}
}
// Merge security schemes
if let Some(other_security_schemes) = other_components.security_schemes {
let self_security_schemes = self_components
.security_schemes
.get_or_insert_with(HashMap::new);
for (name, scheme) in other_security_schemes {
self_security_schemes.entry(name).or_insert(scheme);
}
}
}
// Merge tags (deduplicate by name)
if let Some(other_tags) = other.tags {
let self_tags = self.tags.get_or_insert_with(Vec::new);
for tag in other_tags {
if !self_tags.iter().any(|t| t.name == tag.name) {
self_tags.push(tag);
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::route::{Operation, PathItem};
use crate::schema::{Components, Schema, SchemaType, SecurityScheme, SecuritySchemeType};
fn create_base_openapi() -> OpenApi {
OpenApi {
openapi: OpenApiVersion::V3_1_0,
info: Info {
title: "Base API".to_string(),
version: "1.0.0".to_string(),
description: None,
terms_of_service: None,
contact: None,
license: None,
summary: None,
},
servers: None,
paths: BTreeMap::new(),
components: None,
security: None,
tags: None,
external_docs: None,
}
}
fn create_path_item(summary: &str) -> PathItem {
PathItem {
get: Some(Operation {
summary: Some(summary.to_string()),
description: None,
operation_id: None,
tags: None,
parameters: None,
request_body: None,
responses: BTreeMap::new(),
security: None,
}),
..Default::default()
}
}
#[test]
fn test_merge_paths() {
let mut base = create_base_openapi();
base.paths
.insert("/users".to_string(), create_path_item("Get users"));
let mut other = create_base_openapi();
other
.paths
.insert("/posts".to_string(), create_path_item("Get posts"));
other
.paths
.insert("/users".to_string(), create_path_item("Other users")); // Conflict
base.merge(other);
// Both paths should exist
assert!(base.paths.contains_key("/users"));
assert!(base.paths.contains_key("/posts"));
// Self takes precedence on conflict
assert_eq!(
base.paths
.get("/users")
.unwrap()
.get
.as_ref()
.unwrap()
.summary,
Some("Get users".to_string())
);
}
#[test]
fn test_merge_schemas() {
let mut base = create_base_openapi();
let mut base_schemas = BTreeMap::new();
base_schemas.insert("User".to_string(), Schema::object());
base.components = Some(Components {
schemas: Some(base_schemas),
responses: None,
parameters: None,
examples: None,
request_bodies: None,
headers: None,
security_schemes: None,
});
let mut other = create_base_openapi();
let mut other_schemas = BTreeMap::new();
other_schemas.insert("Post".to_string(), Schema::object());
other_schemas.insert("User".to_string(), Schema::string()); // Conflict
other.components = Some(Components {
schemas: Some(other_schemas),
responses: None,
parameters: None,
examples: None,
request_bodies: None,
headers: None,
security_schemes: None,
});
base.merge(other);
let schemas = base.components.as_ref().unwrap().schemas.as_ref().unwrap();
assert!(schemas.contains_key("User"));
assert!(schemas.contains_key("Post"));
// Self takes precedence on conflict
assert_eq!(
schemas.get("User").unwrap().schema_type,
Some(SchemaType::Object)
);
}
#[test]
fn test_merge_schemas_when_self_has_no_components() {
let mut base = create_base_openapi();
assert!(base.components.is_none());
let mut other = create_base_openapi();
let mut other_schemas = BTreeMap::new();
other_schemas.insert("Post".to_string(), Schema::object());
other.components = Some(Components {
schemas: Some(other_schemas),
responses: None,
parameters: None,
examples: None,
request_bodies: None,
headers: None,
security_schemes: None,
});
base.merge(other);
assert!(base.components.is_some());
let schemas = base.components.as_ref().unwrap().schemas.as_ref().unwrap();
assert!(schemas.contains_key("Post"));
}
#[test]
fn test_merge_security_schemes() {
let mut base = create_base_openapi();
let mut base_security_schemes = HashMap::new();
base_security_schemes.insert(
"bearerAuth".to_string(),
SecurityScheme {
r#type: SecuritySchemeType::Http,
description: None,
name: None,
r#in: None,
scheme: Some("bearer".to_string()),
bearer_format: Some("JWT".to_string()),
},
);
base.components = Some(Components {
schemas: None,
responses: None,
parameters: None,
examples: None,
request_bodies: None,
headers: None,
security_schemes: Some(base_security_schemes),
});
let mut other = create_base_openapi();
let mut other_security_schemes = HashMap::new();
other_security_schemes.insert(
"apiKey".to_string(),
SecurityScheme {
r#type: SecuritySchemeType::ApiKey,
description: None,
name: Some("X-API-Key".to_string()),
r#in: Some("header".to_string()),
scheme: None,
bearer_format: None,
},
);
other.components = Some(Components {
schemas: None,
responses: None,
parameters: None,
examples: None,
request_bodies: None,
headers: None,
security_schemes: Some(other_security_schemes),
});
base.merge(other);
let security_schemes = base
.components
.as_ref()
.unwrap()
.security_schemes
.as_ref()
.unwrap();
assert!(security_schemes.contains_key("bearerAuth"));
assert!(security_schemes.contains_key("apiKey"));
}
#[test]
fn test_merge_tags() {
let mut base = create_base_openapi();
base.tags = Some(vec![Tag {
name: "users".to_string(),
description: Some("User operations".to_string()),
external_docs: None,
}]);
let mut other = create_base_openapi();
other.tags = Some(vec![
Tag {
name: "posts".to_string(),
description: Some("Post operations".to_string()),
external_docs: None,
},
Tag {
name: "users".to_string(),
description: Some("Duplicate users tag".to_string()),
external_docs: None,
}, // Duplicate
]);
base.merge(other);
let tags = base.tags.as_ref().unwrap();
assert_eq!(tags.len(), 2); // No duplicates
assert!(tags.iter().any(|t| t.name == "users"));
assert!(tags.iter().any(|t| t.name == "posts"));
// Self's description takes precedence
let users_tag = tags.iter().find(|t| t.name == "users").unwrap();
assert_eq!(users_tag.description, Some("User operations".to_string()));
}
#[test]
fn test_merge_tags_when_self_has_none() {
let mut base = create_base_openapi();
assert!(base.tags.is_none());
let mut other = create_base_openapi();
other.tags = Some(vec![Tag {
name: "posts".to_string(),
description: None,
external_docs: None,
}]);
base.merge(other);
assert!(base.tags.is_some());
assert_eq!(base.tags.as_ref().unwrap().len(), 1);
}
#[test]
fn test_merge_empty_other() {
let mut base = create_base_openapi();
base.paths
.insert("/users".to_string(), create_path_item("Get users"));
base.tags = Some(vec![Tag {
name: "users".to_string(),
description: None,
external_docs: None,
}]);
let other = create_base_openapi(); // Empty paths, no components, no tags
base.merge(other);
// Base should remain unchanged
assert_eq!(base.paths.len(), 1);
assert!(base.paths.contains_key("/users"));
assert_eq!(base.tags.as_ref().unwrap().len(), 1);
}
}