259 lines
9.4 KiB
Rust
259 lines
9.4 KiB
Rust
use std::collections::{BTreeMap, BTreeSet};
|
|
|
|
use serde_json::Value;
|
|
|
|
use crate::rest::model::{
|
|
ImportFinding, ImportGroupPreview, ImportOperationCandidate, ImportPreview,
|
|
ImportSourcePreview, NormalizedLiteral, NormalizedOperation, NormalizedScalarKind,
|
|
NormalizedSchema, NormalizedSchemaKind, NormalizedSecurityScheme, RestImportDocument,
|
|
RestImportOperation, RestImportParameter, RestRequestBodyVariant, RestResponseVariant,
|
|
};
|
|
|
|
pub(super) fn preview_from_ir(ir: &crate::rest::model::NormalizedIr) -> ImportPreview {
|
|
let operations = ir
|
|
.operations
|
|
.iter()
|
|
.map(operation_from_normalized)
|
|
.collect::<Vec<_>>();
|
|
let findings = ir
|
|
.findings
|
|
.iter()
|
|
.map(|finding| ImportFinding {
|
|
code: finding.code.clone(),
|
|
severity: finding.severity.clone(),
|
|
message: finding.message.clone(),
|
|
operation_key: finding.operation_key.clone(),
|
|
source_location: Some(finding.location.clone()),
|
|
})
|
|
.collect();
|
|
preview_from_operations(
|
|
&ir.source,
|
|
operations.iter(),
|
|
findings,
|
|
&ir.security_schemes,
|
|
false,
|
|
)
|
|
}
|
|
|
|
pub(super) fn preview_from_document(document: RestImportDocument) -> ImportPreview {
|
|
let security_schemes = document.security_schemes.clone();
|
|
let source = ImportSourcePreview {
|
|
format: document.format,
|
|
version: document.version,
|
|
title: document.title,
|
|
servers: document.servers,
|
|
};
|
|
preview_from_operations(
|
|
&source,
|
|
document.operations.iter(),
|
|
document.findings,
|
|
&security_schemes,
|
|
true,
|
|
)
|
|
}
|
|
|
|
fn preview_from_operations<'a>(
|
|
source: &ImportSourcePreview,
|
|
operations: impl Iterator<Item = &'a RestImportOperation>,
|
|
mut findings: Vec<ImportFinding>,
|
|
security_schemes: &BTreeMap<String, NormalizedSecurityScheme>,
|
|
legacy: bool,
|
|
) -> ImportPreview {
|
|
let mut groups: BTreeMap<String, ImportGroupPreview> = BTreeMap::new();
|
|
let mut used_names = BTreeSet::new();
|
|
|
|
for operation in operations {
|
|
let candidate = if legacy {
|
|
super::legacy_payload::candidate_from_operation(
|
|
operation,
|
|
&source.servers,
|
|
&mut used_names,
|
|
)
|
|
} else {
|
|
crate::rest::payload::candidate_from_operation(
|
|
operation,
|
|
security_schemes,
|
|
&source.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::<ImportOperationCandidate>::new(),
|
|
})
|
|
.operations
|
|
.push(candidate);
|
|
}
|
|
|
|
super::normalize_security::attach_operation_findings(&mut groups, &findings);
|
|
super::normalize::sort_findings(&mut findings);
|
|
ImportPreview {
|
|
source: source.clone(),
|
|
groups: groups.into_values().collect(),
|
|
findings,
|
|
conflicts: Vec::new(),
|
|
}
|
|
}
|
|
|
|
pub(super) fn operation_from_normalized(operation: &NormalizedOperation) -> RestImportOperation {
|
|
RestImportOperation {
|
|
key: operation.key.clone(),
|
|
method: operation.method,
|
|
path: operation.path.clone(),
|
|
source_location: Some(operation.location.clone()),
|
|
security_requirements: operation.security_requirements.clone(),
|
|
operation_id: operation.operation_id.clone(),
|
|
summary: operation.summary.clone(),
|
|
description: operation.description.clone(),
|
|
tags: operation.tags.clone(),
|
|
parameters: operation
|
|
.parameters
|
|
.iter()
|
|
.map(|parameter| RestImportParameter {
|
|
name: parameter.name.clone(),
|
|
location: parameter.location,
|
|
required: parameter.required,
|
|
description: parameter.description.clone(),
|
|
schema: parameter.schema.as_ref().map(schema_value),
|
|
source_location: parameter.source_location.clone(),
|
|
schema_source_location: None,
|
|
})
|
|
.collect(),
|
|
request_body_schema: operation.request_body_schema.as_ref().map(schema_value),
|
|
request_body_schema_location: None,
|
|
request_body_variants: operation
|
|
.request_body_variants
|
|
.iter()
|
|
.map(|variant| RestRequestBodyVariant {
|
|
content_type: variant.content_type.clone(),
|
|
required: variant.required,
|
|
schema: variant.schema.as_ref().map(schema_value),
|
|
multipart_field_mime_types: variant.multipart_field_mime_types.clone(),
|
|
source_location: variant.source_location.clone(),
|
|
schema_source_location: None,
|
|
schema_property_source_locations: BTreeMap::new(),
|
|
})
|
|
.collect(),
|
|
response_schema: operation.response_schema.as_ref().map(schema_value),
|
|
response_schema_location: None,
|
|
response_variants: operation
|
|
.response_variants
|
|
.iter()
|
|
.map(|variant| RestResponseVariant {
|
|
status_code: variant.status_code,
|
|
content_type: variant.content_type.clone(),
|
|
schema: variant.schema.as_ref().map(schema_value),
|
|
source_location: variant.source_location.clone(),
|
|
schema_source_location: None,
|
|
})
|
|
.collect(),
|
|
servers: operation.servers.clone(),
|
|
findings: operation
|
|
.findings
|
|
.iter()
|
|
.map(|finding| ImportFinding {
|
|
code: finding.code.clone(),
|
|
severity: finding.severity.clone(),
|
|
message: finding.message.clone(),
|
|
operation_key: finding.operation_key.clone(),
|
|
source_location: Some(finding.location.clone()),
|
|
})
|
|
.collect(),
|
|
internal_finding_locations: Vec::new(),
|
|
}
|
|
}
|
|
|
|
fn schema_value(schema: &NormalizedSchema) -> Value {
|
|
let mut value = match &schema.kind {
|
|
NormalizedSchemaKind::Reference { reference } => serde_json::json!({"$ref": reference.uri}),
|
|
NormalizedSchemaKind::Composition { operator, variants } => serde_json::json!({
|
|
operator: variants.iter().map(schema_value).collect::<Vec<_>>(),
|
|
"x-crank-lossless-composition": true,
|
|
}),
|
|
NormalizedSchemaKind::Object {
|
|
properties,
|
|
required,
|
|
nullable,
|
|
default_value,
|
|
} => {
|
|
let mut value = serde_json::json!({
|
|
"type": "object",
|
|
"properties": properties.iter().map(|(name, schema)| (name.clone(), schema_value(schema))).collect::<serde_json::Map<_, _>>(),
|
|
"required": required,
|
|
"nullable": nullable,
|
|
});
|
|
insert_default(&mut value, default_value.as_ref());
|
|
value
|
|
}
|
|
NormalizedSchemaKind::Array {
|
|
items,
|
|
nullable,
|
|
default_value,
|
|
} => {
|
|
let mut value = serde_json::json!({"type": "array", "nullable": nullable});
|
|
if let Some(items) = items {
|
|
value["items"] = schema_value(items);
|
|
}
|
|
insert_default(&mut value, default_value.as_ref());
|
|
value
|
|
}
|
|
NormalizedSchemaKind::Scalar {
|
|
scalar_type,
|
|
format,
|
|
nullable,
|
|
default_value,
|
|
enum_values,
|
|
} => {
|
|
let scalar_type = match scalar_type {
|
|
NormalizedScalarKind::String => "string",
|
|
NormalizedScalarKind::Integer => "integer",
|
|
NormalizedScalarKind::Number => "number",
|
|
NormalizedScalarKind::Boolean => "boolean",
|
|
NormalizedScalarKind::Null => "null",
|
|
};
|
|
let mut value = serde_json::json!({"type": scalar_type, "nullable": nullable});
|
|
if let Some(format) = format {
|
|
value["format"] = Value::String(format.clone());
|
|
}
|
|
if !enum_values.is_empty() {
|
|
value["enum"] = Value::Array(enum_values.iter().map(literal_value).collect());
|
|
}
|
|
insert_default(&mut value, default_value.as_ref());
|
|
value
|
|
}
|
|
NormalizedSchemaKind::Unknown => serde_json::json!({}),
|
|
};
|
|
if let Some(description) = &schema.description {
|
|
value["description"] = Value::String(description.clone());
|
|
}
|
|
value
|
|
}
|
|
|
|
fn insert_default(value: &mut Value, default: Option<&NormalizedLiteral>) {
|
|
if let Some(default) = default {
|
|
value["default"] = literal_value(default);
|
|
}
|
|
}
|
|
|
|
fn literal_value(value: &NormalizedLiteral) -> Value {
|
|
match value {
|
|
NormalizedLiteral::String(value) => Value::String(value.clone()),
|
|
NormalizedLiteral::Integer(value) => Value::Number((*value).into()),
|
|
NormalizedLiteral::Unsigned(value) => Value::Number((*value).into()),
|
|
NormalizedLiteral::Number(value) => serde_json::Number::from_f64(*value)
|
|
.map(Value::Number)
|
|
.unwrap_or(Value::Null),
|
|
NormalizedLiteral::Boolean(value) => Value::Bool(*value),
|
|
NormalizedLiteral::Null => Value::Null,
|
|
}
|
|
}
|