mrapids 0.1.31

Your OpenAPI, but executable
Documentation
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
use super::errors::CoreError;
use anyhow::{Context, Result};
use openapiv3::{OpenAPI, Operation};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::fs;
use std::path::Path;

pub fn load_openapi_spec(path: &Path) -> Result<OpenAPI, CoreError> {
    // Check if file exists
    if !path.exists() {
        let parent = path.parent().unwrap_or(Path::new("."));
        let available = fs::read_dir(parent)
            .map(|entries| {
                entries
                    .filter_map(|e| e.ok())
                    .filter_map(|e| e.file_name().to_str().map(String::from))
                    .filter(|name| {
                        name.ends_with(".yaml") || name.ends_with(".yml") || name.ends_with(".json")
                    })
                    .collect()
            })
            .unwrap_or_default();

        return Err(CoreError::SpecNotFound {
            path: path.to_path_buf(),
            available,
        });
    }

    // Read file content
    let content = fs::read_to_string(path).map_err(|e| CoreError::SpecParseFailed {
        reason: format!("Cannot read file: {}", e),
    })?;

    // Parse based on extension
    let spec = if path.extension().and_then(|s| s.to_str()) == Some("json") {
        serde_json::from_str(&content).map_err(|e| CoreError::SpecParseFailed {
            reason: format!("Invalid JSON: {}", e),
        })?
    } else {
        serde_yaml::from_str(&content).map_err(|e| CoreError::SpecParseFailed {
            reason: format!("Invalid YAML: {}", e),
        })?
    };

    Ok(spec)
}

pub fn find_operation<'a>(
    spec: &'a OpenAPI,
    operation_id: &str,
) -> Result<&'a Operation, CoreError> {
    // Search through all paths for the operation
    for (_path, path_item) in &spec.paths.paths {
        let path_item = match path_item {
            openapiv3::ReferenceOr::Item(item) => item,
            _ => continue,
        };

        // Check each HTTP method
        let operations = [
            (&path_item.get, "GET"),
            (&path_item.post, "POST"),
            (&path_item.put, "PUT"),
            (&path_item.delete, "DELETE"),
            (&path_item.patch, "PATCH"),
        ];

        for (op, _method) in operations {
            let Some(operation) = op else { continue };
            if operation.operation_id.as_deref() == Some(operation_id) {
                return Ok(operation);
            }
        }
    }

    // Operation not found - list available ones
    let available = list_operations(spec);
    Err(CoreError::OperationNotFound {
        operation: operation_id.to_string(),
        available,
    })
}

pub fn list_operations(spec: &OpenAPI) -> Vec<String> {
    let mut operations = Vec::new();

    for (_path, path_item) in &spec.paths.paths {
        let path_item = match path_item {
            openapiv3::ReferenceOr::Item(item) => item,
            _ => continue,
        };

        let ops = [
            &path_item.get,
            &path_item.post,
            &path_item.put,
            &path_item.delete,
            &path_item.patch,
        ];

        for op in ops.into_iter().flatten() {
            if let Some(id) = &op.operation_id {
                operations.push(id.clone());
            }
        }
    }

    operations
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    #[test]
    fn test_load_valid_openapi_spec() {
        let path = PathBuf::from("examples/petstore.yaml");
        let result = load_openapi_spec(&path);
        assert!(result.is_ok());

        let spec = result.unwrap();
        assert_eq!(spec.info.title, "Pet Store API");
        assert_eq!(spec.info.version, "1.0.0");
    }

    #[test]
    fn test_load_nonexistent_file() {
        let path = PathBuf::from("nonexistent.yaml");
        let result = load_openapi_spec(&path);
        assert!(result.is_err());

        match result.unwrap_err() {
            CoreError::SpecNotFound { .. } => (),
            _ => panic!("Expected SpecNotFound error"),
        }
    }

    #[test]
    fn test_find_operation_exists() {
        let path = PathBuf::from("examples/petstore.yaml");
        let spec = load_openapi_spec(&path).unwrap();

        let result = find_operation(&spec, "getPetById");
        assert!(result.is_ok());

        let operation = result.unwrap();
        assert_eq!(operation.operation_id.as_deref(), Some("getPetById"));
    }

    #[test]
    fn test_find_operation_not_exists() {
        let path = PathBuf::from("examples/petstore.yaml");
        let spec = load_openapi_spec(&path).unwrap();

        let result = find_operation(&spec, "nonexistentOperation");
        assert!(result.is_err());

        match result.unwrap_err() {
            CoreError::OperationNotFound { available, .. } => {
                assert!(available.contains(&"getPetById".to_string()));
            }
            _ => panic!("Expected OperationNotFound error"),
        }
    }

    #[test]
    fn test_list_operations() {
        let path = PathBuf::from("examples/petstore.yaml");
        let spec = load_openapi_spec(&path).unwrap();

        let operations = list_operations(&spec);
        assert!(operations.contains(&"getPetById".to_string()));
        assert!(operations.contains(&"addPet".to_string()));
        assert!(operations.contains(&"findPetsByStatus".to_string()));
    }
}

// Add unified spec structures for auth detection
/// Unified specification structure that abstracts over OpenAPI/Swagger differences
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct UnifiedSpec {
    pub info: ApiInfo,
    pub servers: Vec<Server>,
    pub paths: HashMap<String, PathItem>,
    pub security_schemes: HashMap<String, UnifiedSecurityScheme>,
    pub security: Option<Vec<HashMap<String, Vec<String>>>>,
}

#[derive(Debug, Clone)]
pub struct ApiInfo {
    #[allow(dead_code)]
    pub title: String,
    #[allow(dead_code)]
    pub version: String,
    #[allow(dead_code)]
    pub description: Option<String>,
}

#[derive(Debug, Clone)]
pub struct Server {
    pub url: String,
    #[allow(dead_code)]
    pub description: Option<String>,
}

#[derive(Debug, Clone)]
pub struct PathItem {
    pub operations: HashMap<String, UnifiedOperation>,
}

#[derive(Debug, Clone)]
pub struct UnifiedOperation {
    pub operation_id: Option<String>,
    #[allow(dead_code)]
    pub summary: Option<String>,
    #[allow(dead_code)]
    pub description: Option<String>,
    pub security: Option<Vec<HashMap<String, Vec<String>>>>,
}

/// Unified security scheme that normalizes between OpenAPI 2.0 and 3.0
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnifiedSecurityScheme {
    #[serde(rename = "type")]
    pub scheme_type: String,
    pub description: Option<String>,
    pub name: Option<String>,
    #[serde(rename = "in")]
    pub location: Option<String>,
    pub scheme: Option<String>,
    pub bearer_format: Option<String>,
    pub flows: Option<Value>,
    pub openid_connect_url: Option<String>,

    // OAuth2 specific fields
    pub flow: Option<String>,
    pub authorization_url: Option<String>,
    pub token_url: Option<String>,
    pub refresh_url: Option<String>,
    pub scopes: Option<HashMap<String, String>>,
}

impl UnifiedSpec {
    /// Load a unified spec from a file
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let content =
            std::fs::read_to_string(path.as_ref()).context("Failed to read specification file")?;

        // Parse as JSON or YAML
        let value: Value = if path
            .as_ref()
            .extension()
            .and_then(|s| s.to_str())
            .map(|s| s.ends_with("json"))
            .unwrap_or(false)
        {
            serde_json::from_str(&content)?
        } else {
            serde_yaml::from_str(&content)?
        };

        Self::from_value(value)
    }

    /// Parse from a JSON value
    pub fn from_value(value: Value) -> Result<Self> {
        // Detect OpenAPI version
        let is_openapi_3 = value.get("openapi").is_some();

        let info = Self::parse_info(&value)?;
        let servers = Self::parse_servers(&value, is_openapi_3)?;
        let paths = Self::parse_paths(&value)?;
        let security_schemes = Self::parse_security_schemes(&value, is_openapi_3)?;
        let security = Self::parse_security(&value)?;

        Ok(Self {
            info,
            servers,
            paths,
            security_schemes,
            security,
        })
    }

    fn parse_info(value: &Value) -> Result<ApiInfo> {
        let info = value.get("info").context("Missing 'info' field")?;

        Ok(ApiInfo {
            title: info
                .get("title")
                .and_then(|v| v.as_str())
                .unwrap_or("API")
                .to_string(),
            version: info
                .get("version")
                .and_then(|v| v.as_str())
                .unwrap_or("1.0.0")
                .to_string(),
            description: info
                .get("description")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
        })
    }

    fn parse_servers(value: &Value, is_openapi_3: bool) -> Result<Vec<Server>> {
        if is_openapi_3 {
            if let Some(servers) = value.get("servers").and_then(|v| v.as_array()) {
                return Ok(servers
                    .iter()
                    .filter_map(|s| {
                        s.get("url").and_then(|u| u.as_str()).map(|url| Server {
                            url: url.to_string(),
                            description: s
                                .get("description")
                                .and_then(|d| d.as_str())
                                .map(|s| s.to_string()),
                        })
                    })
                    .collect());
            }
        } else {
            // OpenAPI 2.0 / Swagger
            let mut url = String::new();

            if let Some(schemes) = value.get("schemes").and_then(|v| v.as_array()) {
                if let Some(scheme) = schemes.get(0).and_then(|s| s.as_str()) {
                    url.push_str(scheme);
                    url.push_str("://");
                }
            } else {
                url.push_str("https://");
            }

            if let Some(host) = value.get("host").and_then(|v| v.as_str()) {
                url.push_str(host);
            } else {
                url.push_str("localhost");
            }

            if let Some(base_path) = value.get("basePath").and_then(|v| v.as_str()) {
                if !base_path.starts_with('/') {
                    url.push('/');
                }
                url.push_str(base_path);
            }

            return Ok(vec![Server {
                url,
                description: None,
            }]);
        }

        Ok(vec![Server {
            url: "http://localhost".to_string(),
            description: None,
        }])
    }

    fn parse_paths(value: &Value) -> Result<HashMap<String, PathItem>> {
        let mut paths = HashMap::new();

        if let Some(paths_obj) = value.get("paths").and_then(|v| v.as_object()) {
            for (path, path_value) in paths_obj {
                let mut operations = HashMap::new();

                if let Some(path_obj) = path_value.as_object() {
                    for (method, op_value) in path_obj {
                        if ["get", "post", "put", "delete", "patch", "head", "options"]
                            .contains(&method.as_str())
                        {
                            if let Ok(operation) = Self::parse_operation(op_value) {
                                operations.insert(method.clone(), operation);
                            }
                        }
                    }
                }

                if !operations.is_empty() {
                    paths.insert(path.clone(), PathItem { operations });
                }
            }
        }

        Ok(paths)
    }

    fn parse_operation(value: &Value) -> Result<UnifiedOperation> {
        Ok(UnifiedOperation {
            operation_id: value
                .get("operationId")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            summary: value
                .get("summary")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            description: value
                .get("description")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            security: Self::parse_security(value)?,
        })
    }

    fn parse_security(value: &Value) -> Result<Option<Vec<HashMap<String, Vec<String>>>>> {
        if let Some(security) = value.get("security").and_then(|v| v.as_array()) {
            let mut result = Vec::new();

            for item in security {
                if let Some(obj) = item.as_object() {
                    let mut requirement = HashMap::new();

                    for (scheme, scopes) in obj {
                        let scope_vec = if let Some(arr) = scopes.as_array() {
                            arr.iter()
                                .filter_map(|s| s.as_str().map(|s| s.to_string()))
                                .collect()
                        } else {
                            Vec::new()
                        };

                        requirement.insert(scheme.clone(), scope_vec);
                    }

                    result.push(requirement);
                }
            }

            if !result.is_empty() {
                return Ok(Some(result));
            }
        }

        Ok(None)
    }

    fn parse_security_schemes(
        value: &Value,
        is_openapi_3: bool,
    ) -> Result<HashMap<String, UnifiedSecurityScheme>> {
        let mut schemes = HashMap::new();

        let security_defs = if is_openapi_3 {
            value
                .get("components")
                .and_then(|c| c.get("securitySchemes"))
        } else {
            value.get("securityDefinitions")
        };

        if let Some(defs) = security_defs.and_then(|v| v.as_object()) {
            for (name, scheme_value) in defs {
                if let Ok(scheme) =
                    serde_json::from_value::<UnifiedSecurityScheme>(scheme_value.clone())
                {
                    schemes.insert(name.clone(), scheme);
                }
            }
        }

        Ok(schemes)
    }
}