1use crate::error::{BenchError, Result};
4use mockforge_openapi::spec::OpenApiSpec;
5use openapiv3::{OpenAPI, Operation, Parameter, PathItem, ReferenceOr};
6use std::path::Path;
7
8#[derive(Debug, Clone)]
10pub struct ApiOperation {
11 pub method: String,
12 pub path: String,
13 pub operation: Operation,
14 pub operation_id: Option<String>,
15}
16
17impl ApiOperation {
18 pub fn display_name(&self) -> String {
20 self.operation_id
21 .clone()
22 .unwrap_or_else(|| format!("{} {}", self.method.to_uppercase(), self.path))
23 }
24}
25
26pub struct SpecParser {
28 spec: OpenAPI,
29}
30
31impl SpecParser {
32 pub async fn from_file(path: &Path) -> Result<Self> {
34 let spec = OpenApiSpec::from_file(path)
35 .await
36 .map_err(|e| BenchError::SpecParseError(e.to_string()))?;
37
38 Ok(Self {
39 spec: spec.spec.clone(),
40 })
41 }
42
43 pub fn from_spec(spec: OpenApiSpec) -> Self {
45 Self { spec: spec.spec }
46 }
47
48 pub fn spec(&self) -> &OpenAPI {
50 &self.spec
51 }
52
53 pub fn get_operations(&self) -> Vec<ApiOperation> {
55 let mut operations = Vec::new();
56
57 for (path, path_item) in &self.spec.paths.paths {
58 if let ReferenceOr::Item(item) = path_item {
59 self.extract_operations_from_path(path, item, &mut operations);
60 }
61 }
62
63 operations
64 }
65
66 pub fn filter_operations(&self, filter: &str) -> Result<Vec<ApiOperation>> {
68 let all_ops = self.get_operations();
69
70 if filter.is_empty() {
71 return Ok(all_ops);
72 }
73
74 let filters: Vec<&str> = filter.split(',').map(|s| s.trim()).collect();
75 let mut filtered = Vec::new();
76
77 for filter_str in filters {
78 let parts: Vec<&str> = filter_str.splitn(2, ' ').collect();
82 let (method, path_pattern) = if parts.len() == 2 {
83 (parts[0].to_uppercase(), Some(parts[1]))
84 } else if parts.len() == 1 && !parts[0].is_empty() {
85 (parts[0].to_uppercase(), None)
86 } else {
87 return Err(BenchError::Other(format!(
88 "Invalid operation filter format: '{}'. Expected 'METHOD /path' or 'METHOD'",
89 filter_str
90 )));
91 };
92
93 for op in &all_ops {
94 let method_matches = op.method.to_uppercase() == method;
95 let path_matches =
96 path_pattern.map(|p| Self::matches_path(&op.path, p)).unwrap_or(true);
97 if method_matches && path_matches {
98 filtered.push(op.clone());
99 }
100 }
101 }
102
103 if filtered.is_empty() {
104 return Err(BenchError::OperationNotFound(filter.to_string()));
105 }
106
107 Ok(filtered)
108 }
109
110 pub fn exclude_operations(
115 &self,
116 operations: Vec<ApiOperation>,
117 exclude: &str,
118 ) -> Result<Vec<ApiOperation>> {
119 if exclude.is_empty() {
120 return Ok(operations);
121 }
122
123 let exclusions: Vec<&str> = exclude.split(',').map(|s| s.trim()).collect();
124 let mut result = Vec::new();
125
126 for op in operations {
127 let mut should_exclude = false;
128
129 for exclude_str in &exclusions {
130 let parts: Vec<&str> = exclude_str.splitn(2, ' ').collect();
132
133 let (method, path_pattern) = if parts.len() == 2 {
134 (parts[0].to_uppercase(), Some(parts[1]))
135 } else {
136 (parts[0].to_uppercase(), None)
138 };
139
140 let method_matches = op.method.to_uppercase() == method;
141 let path_matches =
142 path_pattern.map(|p| Self::matches_path(&op.path, p)).unwrap_or(true); if method_matches && path_matches {
145 should_exclude = true;
146 break;
147 }
148 }
149
150 if !should_exclude {
151 result.push(op);
152 }
153 }
154
155 Ok(result)
156 }
157
158 fn extract_operations_from_path(
160 &self,
161 path: &str,
162 path_item: &PathItem,
163 operations: &mut Vec<ApiOperation>,
164 ) {
165 let path_level_params: Vec<ReferenceOr<Parameter>> = path_item
167 .parameters
168 .iter()
169 .filter_map(|p| self.resolve_parameter(p).map(ReferenceOr::Item))
170 .collect();
171
172 let method_ops = vec![
173 ("get", &path_item.get),
174 ("post", &path_item.post),
175 ("put", &path_item.put),
176 ("delete", &path_item.delete),
177 ("patch", &path_item.patch),
178 ("head", &path_item.head),
179 ("options", &path_item.options),
180 ];
181
182 for (method, op_ref) in method_ops {
183 if let Some(op) = op_ref {
184 let mut resolved_op = op.clone();
186 let mut resolved_params: Vec<ReferenceOr<Parameter>> = Vec::new();
187
188 resolved_params.extend(path_level_params.clone());
190
191 for param_ref in &op.parameters {
193 if let Some(resolved) = self.resolve_parameter(param_ref) {
194 resolved_params.push(ReferenceOr::Item(resolved));
195 }
196 }
197
198 resolved_op.parameters = resolved_params;
199
200 operations.push(ApiOperation {
201 method: method.to_string(),
202 path: path.to_string(),
203 operation: resolved_op,
204 operation_id: op.operation_id.clone(),
205 });
206 }
207 }
208 }
209
210 fn resolve_parameter(&self, param_ref: &ReferenceOr<Parameter>) -> Option<Parameter> {
212 match param_ref {
213 ReferenceOr::Item(param) => Some(param.clone()),
214 ReferenceOr::Reference { reference } => {
215 let parts: Vec<&str> = reference.split('/').collect();
217 if parts.len() >= 4 && parts[1] == "components" && parts[2] == "parameters" {
218 let param_name = parts[3];
219 if let Some(components) = &self.spec.components {
220 if let ReferenceOr::Item(param) = components.parameters.get(param_name)? {
221 return Some(param.clone());
222 }
223 }
224 }
225 None
226 }
227 }
228 }
229
230 fn matches_path(path: &str, pattern: &str) -> bool {
232 if pattern == path {
233 return true;
234 }
235
236 if let Some(prefix) = pattern.strip_suffix('*') {
238 return path.starts_with(prefix);
239 }
240
241 false
242 }
243
244 pub fn get_base_url(&self) -> Option<String> {
246 self.spec.servers.first().map(|server| server.url.clone())
247 }
248
249 pub fn get_base_path(&self) -> Option<String> {
259 let server_url = self.spec.servers.first().map(|s| &s.url)?;
260
261 if server_url.starts_with('/') {
263 let path = server_url.trim_end_matches('/');
264 if !path.is_empty() && path != "/" {
265 return Some(path.to_string());
266 }
267 return None;
268 }
269
270 if let Ok(parsed) = url::Url::parse(server_url) {
272 let path = parsed.path().trim_end_matches('/');
273 if !path.is_empty() && path != "/" {
274 return Some(path.to_string());
275 }
276 }
277
278 None
279 }
280
281 pub fn get_info(&self) -> (String, String) {
283 (self.spec.info.title.clone(), self.spec.info.version.clone())
284 }
285}
286
287#[cfg(test)]
288mod tests {
289 use super::*;
290 use openapiv3::Operation;
291
292 fn create_test_operation(method: &str, path: &str, operation_id: Option<&str>) -> ApiOperation {
294 ApiOperation {
295 method: method.to_string(),
296 path: path.to_string(),
297 operation: Operation::default(),
298 operation_id: operation_id.map(|s| s.to_string()),
299 }
300 }
301
302 #[test]
303 fn test_matches_path() {
304 assert!(SpecParser::matches_path("/users", "/users"));
305 assert!(SpecParser::matches_path("/users/123", "/users/*"));
306 assert!(!SpecParser::matches_path("/posts", "/users"));
307 }
308
309 #[test]
310 fn test_exclude_operations_by_method() {
311 let operations = vec![
313 create_test_operation("get", "/users", Some("getUsers")),
314 create_test_operation("post", "/users", Some("createUser")),
315 create_test_operation("delete", "/users/{id}", Some("deleteUser")),
316 create_test_operation("get", "/posts", Some("getPosts")),
317 create_test_operation("delete", "/posts/{id}", Some("deletePost")),
318 ];
319
320 let spec = OpenAPI::default();
322 let parser = SpecParser { spec };
323 let result = parser.exclude_operations(operations.clone(), "DELETE").unwrap();
324
325 assert_eq!(result.len(), 3);
326 assert!(result.iter().all(|op| op.method.to_uppercase() != "DELETE"));
327 }
328
329 #[test]
330 fn test_exclude_operations_by_method_and_path() {
331 let operations = vec![
332 create_test_operation("get", "/users", Some("getUsers")),
333 create_test_operation("post", "/users", Some("createUser")),
334 create_test_operation("delete", "/users/{id}", Some("deleteUser")),
335 create_test_operation("get", "/posts", Some("getPosts")),
336 create_test_operation("delete", "/posts/{id}", Some("deletePost")),
337 ];
338
339 let spec = OpenAPI::default();
340 let parser = SpecParser { spec };
341
342 let result = parser.exclude_operations(operations.clone(), "DELETE /users/{id}").unwrap();
344
345 assert_eq!(result.len(), 4);
346 assert!(result.iter().any(|op| op.operation_id == Some("deletePost".to_string())));
348 assert!(!result.iter().any(|op| op.operation_id == Some("deleteUser".to_string())));
350 }
351
352 #[test]
353 fn test_exclude_operations_multiple_methods() {
354 let operations = vec![
355 create_test_operation("get", "/users", Some("getUsers")),
356 create_test_operation("post", "/users", Some("createUser")),
357 create_test_operation("delete", "/users/{id}", Some("deleteUser")),
358 create_test_operation("put", "/users/{id}", Some("updateUser")),
359 ];
360
361 let spec = OpenAPI::default();
362 let parser = SpecParser { spec };
363
364 let result = parser.exclude_operations(operations.clone(), "DELETE,POST").unwrap();
366
367 assert_eq!(result.len(), 2);
368 assert!(result.iter().all(|op| op.method.to_uppercase() != "DELETE"));
369 assert!(result.iter().all(|op| op.method.to_uppercase() != "POST"));
370 }
371
372 #[test]
373 fn test_exclude_operations_empty_string() {
374 let operations = vec![
375 create_test_operation("get", "/users", Some("getUsers")),
376 create_test_operation("delete", "/users/{id}", Some("deleteUser")),
377 ];
378
379 let spec = OpenAPI::default();
380 let parser = SpecParser { spec };
381
382 let result = parser.exclude_operations(operations.clone(), "").unwrap();
384
385 assert_eq!(result.len(), 2);
386 }
387
388 #[test]
389 fn test_exclude_operations_with_wildcard() {
390 let operations = vec![
391 create_test_operation("get", "/users", Some("getUsers")),
392 create_test_operation("delete", "/users/{id}", Some("deleteUser")),
393 create_test_operation("delete", "/posts/{id}", Some("deletePost")),
394 ];
395
396 let spec = OpenAPI::default();
397 let parser = SpecParser { spec };
398
399 let result = parser.exclude_operations(operations.clone(), "DELETE /users/*").unwrap();
401
402 assert_eq!(result.len(), 2);
403 assert!(result.iter().any(|op| op.operation_id == Some("deletePost".to_string())));
405 assert!(!result.iter().any(|op| op.operation_id == Some("deleteUser".to_string())));
407 }
408
409 #[test]
410 fn test_get_base_path_with_full_url() {
411 let mut spec = OpenAPI::default();
412 spec.servers.push(openapiv3::Server {
413 url: "https://api.example.com/api/v1".to_string(),
414 description: None,
415 variables: Default::default(),
416 extensions: Default::default(),
417 });
418
419 let parser = SpecParser { spec };
420 let base_path = parser.get_base_path();
421
422 assert_eq!(base_path, Some("/api/v1".to_string()));
423 }
424
425 #[test]
426 fn test_get_base_path_with_relative_path() {
427 let mut spec = OpenAPI::default();
428 spec.servers.push(openapiv3::Server {
429 url: "/api/v2".to_string(),
430 description: None,
431 variables: Default::default(),
432 extensions: Default::default(),
433 });
434
435 let parser = SpecParser { spec };
436 let base_path = parser.get_base_path();
437
438 assert_eq!(base_path, Some("/api/v2".to_string()));
439 }
440
441 #[test]
442 fn test_get_base_path_no_path_in_url() {
443 let mut spec = OpenAPI::default();
444 spec.servers.push(openapiv3::Server {
445 url: "https://api.example.com".to_string(),
446 description: None,
447 variables: Default::default(),
448 extensions: Default::default(),
449 });
450
451 let parser = SpecParser { spec };
452 let base_path = parser.get_base_path();
453
454 assert_eq!(base_path, None);
455 }
456
457 #[test]
458 fn test_get_base_path_no_servers() {
459 let spec = OpenAPI::default();
460 let parser = SpecParser { spec };
461 let base_path = parser.get_base_path();
462
463 assert_eq!(base_path, None);
464 }
465
466 #[test]
467 fn test_get_base_path_trailing_slash_removed() {
468 let mut spec = OpenAPI::default();
469 spec.servers.push(openapiv3::Server {
470 url: "https://api.example.com/api/v1/".to_string(),
471 description: None,
472 variables: Default::default(),
473 extensions: Default::default(),
474 });
475
476 let parser = SpecParser { spec };
477 let base_path = parser.get_base_path();
478
479 assert_eq!(base_path, Some("/api/v1".to_string()));
481 }
482}