Add OpenAPI import backend
This commit is contained in:
1 parent
87cdb95b80
commit
a8aa3248c9
34 files changed
+2569
-27
No files matched your search
@@ -0,0 +1,111 @@
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use serde_json::Value;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::rest::{
|
||||
model::{ImportGroupPreview, ImportPreview, RestImportDocument},
|
||||
openapi3,
|
||||
payload::candidate_from_operation,
|
||||
swagger2,
|
||||
};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ImportParseError {
|
||||
#[error("document is not valid YAML or JSON: {0}")]
|
||||
InvalidDocument(String),
|
||||
#[error("unsupported OpenAPI document")]
|
||||
UnsupportedDocument,
|
||||
}
|
||||
|
||||
pub fn preview_document(document: &str) -> Result<ImportPreview, ImportParseError> {
|
||||
let yaml: serde_yaml::Value = serde_yaml::from_str(document)
|
||||
.map_err(|error| ImportParseError::InvalidDocument(error.to_string()))?;
|
||||
let root = serde_json::to_value(yaml)
|
||||
.map_err(|error| ImportParseError::InvalidDocument(error.to_string()))?;
|
||||
|
||||
let normalized = if root.get("openapi").is_some() {
|
||||
openapi3::parse_document(&root)?
|
||||
} else if root.get("swagger").and_then(Value::as_str) == Some("2.0") {
|
||||
swagger2::parse_document(&root)?
|
||||
} else {
|
||||
return Err(ImportParseError::UnsupportedDocument);
|
||||
};
|
||||
|
||||
Ok(preview_from_document(normalized))
|
||||
}
|
||||
|
||||
fn preview_from_document(document: RestImportDocument) -> ImportPreview {
|
||||
let mut groups: BTreeMap<String, ImportGroupPreview> = BTreeMap::new();
|
||||
let mut used_names = BTreeSet::new();
|
||||
|
||||
for operation in &document.operations {
|
||||
let candidate = candidate_from_operation(operation, &document.servers, &mut used_names);
|
||||
let group_title = operation
|
||||
.tags
|
||||
.first()
|
||||
.cloned()
|
||||
.unwrap_or_else(|| "Без группы".to_owned());
|
||||
let group_key = crate::rest::naming::snake_name(&group_title);
|
||||
groups
|
||||
.entry(group_key.clone())
|
||||
.or_insert_with(|| ImportGroupPreview {
|
||||
key: group_key,
|
||||
title: group_title,
|
||||
operations: Vec::new(),
|
||||
})
|
||||
.operations
|
||||
.push(candidate);
|
||||
}
|
||||
|
||||
ImportPreview {
|
||||
source: crate::rest::model::ImportSourcePreview {
|
||||
format: document.format,
|
||||
version: document.version,
|
||||
title: document.title,
|
||||
servers: document.servers,
|
||||
},
|
||||
groups: groups.into_values().collect(),
|
||||
findings: document.findings,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn resolve_local_ref(root: &Value, value: &Value, depth: usize) -> Value {
|
||||
if depth > 12 {
|
||||
return value.clone();
|
||||
}
|
||||
if let Some(reference) = value.get("$ref").and_then(Value::as_str) {
|
||||
if let Some(resolved) = pointer(root, reference) {
|
||||
return resolve_local_ref(root, resolved, depth + 1);
|
||||
}
|
||||
return value.clone();
|
||||
}
|
||||
match value {
|
||||
Value::Object(map) => {
|
||||
let mut out = serde_json::Map::new();
|
||||
for (key, item) in map {
|
||||
out.insert(key.clone(), resolve_local_ref(root, item, depth + 1));
|
||||
}
|
||||
Value::Object(out)
|
||||
}
|
||||
Value::Array(items) => Value::Array(
|
||||
items
|
||||
.iter()
|
||||
.map(|item| resolve_local_ref(root, item, depth + 1))
|
||||
.collect(),
|
||||
),
|
||||
_ => value.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn pointer<'a>(root: &'a Value, reference: &str) -> Option<&'a Value> {
|
||||
if !reference.starts_with("#/") {
|
||||
return None;
|
||||
}
|
||||
let mut current = root;
|
||||
for part in reference.trim_start_matches("#/").split('/') {
|
||||
let part = part.replace("~1", "/").replace("~0", "~");
|
||||
current = current.get(&part)?;
|
||||
}
|
||||
Some(current)
|
||||
}
|
||||
Reference in new issue
Block a user