feat(import): preserve supported HTTP contracts
This commit is contained in:
1 parent
083974c27f
commit
f8ee7c9bfe
87 files changed
+7496
-534
No files matched your search
@@ -8,10 +8,9 @@ use crate::rest::{
|
||||
CoverageDisposition, CoverageEntry, ExternalDocumentSnapshot, ImportFinding,
|
||||
ImportFindingSeverity, ImportGroupPreview, ImportPreview, ImportSourcePreview,
|
||||
NORMALIZER_VERSION, NormalizationConfig, NormalizedApiMetadata, NormalizedFinding,
|
||||
NormalizedIr, NormalizedLiteral, NormalizedOperation, NormalizedParameter,
|
||||
NormalizedScalarKind, NormalizedSchema, NormalizedSchemaKind, PROJECTION_VERSION,
|
||||
RestImportDocument, RestImportOperation, RestImportParameter, SourceDigest, SourceIdentity,
|
||||
SourceLocation, SourceSyntax,
|
||||
NormalizedIr, NormalizedOperation, NormalizedParameter, NormalizedRequestBodyVariant,
|
||||
NormalizedResponseVariant, NormalizedSchema, NormalizedSchemaKind, PROJECTION_VERSION,
|
||||
RestImportDocument, SourceDigest, SourceIdentity, SourceLocation, SourceSyntax,
|
||||
},
|
||||
openapi3,
|
||||
payload::candidate_from_operation,
|
||||
@@ -139,7 +138,7 @@ pub fn normalize_verified_bundle(
|
||||
let root = resolution.root;
|
||||
validate_value_limits(&root, config)?;
|
||||
|
||||
let parsed = match root.get("openapi").and_then(Value::as_str) {
|
||||
let mut parsed = match root.get("openapi").and_then(Value::as_str) {
|
||||
Some(version) if supported_oas_version(version) => openapi3::parse_document(&root)?,
|
||||
Some(_) => return Err(ImportParseError::UnsupportedDocument),
|
||||
None if root.get("swagger").and_then(Value::as_str) == Some("2.0") => {
|
||||
@@ -147,8 +146,20 @@ pub fn normalize_verified_bundle(
|
||||
}
|
||||
None => return Err(ImportParseError::UnsupportedDocument),
|
||||
};
|
||||
super::metadata_findings::collect_secret_like_examples(
|
||||
&root,
|
||||
"",
|
||||
false,
|
||||
&mut parsed.findings,
|
||||
&mut parsed.internal_finding_locations,
|
||||
);
|
||||
|
||||
if parsed.operations.is_empty() {
|
||||
if parsed.operations.is_empty()
|
||||
&& !parsed
|
||||
.findings
|
||||
.iter()
|
||||
.any(|finding| finding.code == "unsupported_http_method")
|
||||
{
|
||||
return Err(ImportParseError::NoMethods);
|
||||
}
|
||||
canonicalize(
|
||||
@@ -166,7 +177,7 @@ pub fn preview_from_ir(ir: &NormalizedIr) -> ImportPreview {
|
||||
let operations = ir
|
||||
.operations
|
||||
.iter()
|
||||
.map(legacy_operation_from_normalized)
|
||||
.map(super::normalize_projection::operation_from_normalized)
|
||||
.collect::<Vec<_>>();
|
||||
let findings = ir
|
||||
.findings
|
||||
@@ -178,7 +189,7 @@ pub fn preview_from_ir(ir: &NormalizedIr) -> ImportPreview {
|
||||
operation_key: finding.operation_key.clone(),
|
||||
})
|
||||
.collect();
|
||||
preview_from_operations(&ir.source, operations.iter(), findings)
|
||||
preview_from_operations(&ir.source, operations.iter(), findings, false)
|
||||
}
|
||||
|
||||
fn preview_from_document(document: RestImportDocument) -> ImportPreview {
|
||||
@@ -188,19 +199,28 @@ fn preview_from_document(document: RestImportDocument) -> ImportPreview {
|
||||
title: document.title,
|
||||
servers: document.servers,
|
||||
};
|
||||
preview_from_operations(&source, document.operations.iter(), document.findings)
|
||||
preview_from_operations(&source, document.operations.iter(), document.findings, true)
|
||||
}
|
||||
|
||||
fn preview_from_operations<'a>(
|
||||
source: &ImportSourcePreview,
|
||||
operations: impl Iterator<Item = &'a crate::rest::model::RestImportOperation>,
|
||||
mut findings: Vec<ImportFinding>,
|
||||
legacy: bool,
|
||||
) -> ImportPreview {
|
||||
let mut groups: BTreeMap<String, ImportGroupPreview> = BTreeMap::new();
|
||||
let mut used_names = BTreeSet::new();
|
||||
|
||||
for operation in operations {
|
||||
let candidate = candidate_from_operation(operation, &source.servers, &mut used_names);
|
||||
let candidate = if legacy {
|
||||
super::legacy_payload::candidate_from_operation(
|
||||
operation,
|
||||
&source.servers,
|
||||
&mut used_names,
|
||||
)
|
||||
} else {
|
||||
candidate_from_operation(operation, &source.servers, &mut used_names)
|
||||
};
|
||||
let group_title = operation
|
||||
.tags
|
||||
.first()
|
||||
@@ -379,14 +399,27 @@ fn canonicalize(
|
||||
disposition: CoverageDisposition::Mapped,
|
||||
});
|
||||
}
|
||||
operation_findings.extend(operation.findings.iter().map(|finding| NormalizedFinding {
|
||||
code: finding.code.clone(),
|
||||
severity: finding.severity.clone(),
|
||||
message: finding.message.clone(),
|
||||
construct_id: stable_id.clone(),
|
||||
location: location.clone(),
|
||||
operation_key: Some(operation.key.clone()),
|
||||
}));
|
||||
operation_findings.extend(operation.findings.iter().enumerate().map(
|
||||
|(index, finding)| {
|
||||
let finding_location = operation
|
||||
.internal_finding_locations
|
||||
.get(index)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| location.clone());
|
||||
NormalizedFinding {
|
||||
code: finding.code.clone(),
|
||||
severity: finding.severity.clone(),
|
||||
message: finding.message.clone(),
|
||||
construct_id: if finding_location == location {
|
||||
stable_id.clone()
|
||||
} else {
|
||||
format!("{version}:{}", finding_location.pointer)
|
||||
},
|
||||
location: finding_location,
|
||||
operation_key: Some(operation.key.clone()),
|
||||
}
|
||||
},
|
||||
));
|
||||
let parameters = operation
|
||||
.parameters
|
||||
.into_iter()
|
||||
@@ -465,6 +498,52 @@ fn canonicalize(
|
||||
&mut coverage,
|
||||
)
|
||||
});
|
||||
let request_body_variants = operation
|
||||
.request_body_variants
|
||||
.iter()
|
||||
.map(|variant| NormalizedRequestBodyVariant {
|
||||
content_type: variant.content_type.clone(),
|
||||
required: variant.required,
|
||||
schema: variant.schema.as_ref().map(|schema| {
|
||||
let schema_location = variant
|
||||
.schema_source_location
|
||||
.as_ref()
|
||||
.unwrap_or(&variant.source_location);
|
||||
normalize_coverage::typed_schema_with_property_locations(
|
||||
schema,
|
||||
&schema_location.pointer,
|
||||
&variant.schema_property_source_locations,
|
||||
&mut coverage,
|
||||
)
|
||||
}),
|
||||
multipart_field_mime_types: variant.multipart_field_mime_types.clone(),
|
||||
source_location: variant.source_location.clone(),
|
||||
})
|
||||
.collect();
|
||||
let response_variants = operation
|
||||
.response_variants
|
||||
.iter()
|
||||
.map(|variant| NormalizedResponseVariant {
|
||||
status_code: variant.status_code,
|
||||
content_type: variant.content_type.clone(),
|
||||
schema: variant.schema.as_ref().map(|schema| {
|
||||
let schema_location = variant
|
||||
.schema_source_location
|
||||
.as_ref()
|
||||
.unwrap_or(&variant.source_location);
|
||||
normalize_coverage::typed_schema(
|
||||
schema,
|
||||
&schema_location.pointer,
|
||||
&format!(
|
||||
"{stable_id}:response_variant:{}",
|
||||
normalize_coverage::escape_pointer(&schema_location.pointer)
|
||||
),
|
||||
&mut coverage,
|
||||
)
|
||||
}),
|
||||
source_location: variant.source_location.clone(),
|
||||
})
|
||||
.collect();
|
||||
if normalize_coverage::has_reference_schema(request_body_schema.as_ref())
|
||||
|| normalize_coverage::has_reference_schema(response_schema.as_ref())
|
||||
|| parameters.iter().any(|parameter| {
|
||||
@@ -499,6 +578,8 @@ fn canonicalize(
|
||||
parameters,
|
||||
request_body_schema,
|
||||
response_schema,
|
||||
request_body_variants,
|
||||
response_variants,
|
||||
servers: operation.servers,
|
||||
findings: operation_findings,
|
||||
})
|
||||
@@ -599,6 +680,18 @@ fn canonicalize(
|
||||
.filter_map(|parameter| parameter.schema.as_ref())
|
||||
.chain(operation.request_body_schema.iter())
|
||||
.chain(operation.response_schema.iter())
|
||||
.chain(
|
||||
operation
|
||||
.request_body_variants
|
||||
.iter()
|
||||
.filter_map(|variant| variant.schema.as_ref()),
|
||||
)
|
||||
.chain(
|
||||
operation
|
||||
.response_variants
|
||||
.iter()
|
||||
.filter_map(|variant| variant.schema.as_ref()),
|
||||
)
|
||||
.any(|schema| schema_contains_reference(schema, &reference.location))
|
||||
})
|
||||
})
|
||||
@@ -738,115 +831,13 @@ fn coverage_disposition_rank(disposition: &CoverageDisposition) -> u8 {
|
||||
}
|
||||
}
|
||||
|
||||
fn legacy_operation_from_normalized(operation: &NormalizedOperation) -> RestImportOperation {
|
||||
RestImportOperation {
|
||||
key: operation.key.clone(),
|
||||
method: operation.method,
|
||||
path: operation.path.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(legacy_schema_value),
|
||||
source_location: parameter.source_location.clone(),
|
||||
schema_source_location: None,
|
||||
})
|
||||
.collect(),
|
||||
request_body_schema: operation
|
||||
.request_body_schema
|
||||
.as_ref()
|
||||
.map(legacy_schema_value),
|
||||
request_body_schema_location: None,
|
||||
response_schema: operation.response_schema.as_ref().map(legacy_schema_value),
|
||||
response_schema_location: None,
|
||||
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(),
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn legacy_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(legacy_schema_value).collect::<Vec<_>>(),
|
||||
"x-crank-lossless-composition": true,
|
||||
})
|
||||
}
|
||||
NormalizedSchemaKind::Object {
|
||||
properties,
|
||||
required,
|
||||
} => {
|
||||
serde_json::json!({"type":"object", "properties": properties.iter().map(|(name, schema)| (name.clone(), legacy_schema_value(schema))).collect::<serde_json::Map<_, _>>(), "required": required})
|
||||
}
|
||||
NormalizedSchemaKind::Array { items } => {
|
||||
let mut value = serde_json::json!({"type":"array"});
|
||||
if let Some(items) = items {
|
||||
value["items"] = legacy_schema_value(items);
|
||||
}
|
||||
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",
|
||||
};
|
||||
serde_json::json!({"type":scalar_type, "format":format, "nullable":nullable, "default":default_value.as_ref().map(legacy_literal_value), "enum":enum_values.iter().map(legacy_literal_value).collect::<Vec<_>>()})
|
||||
}
|
||||
NormalizedSchemaKind::Unknown => serde_json::json!({}),
|
||||
};
|
||||
if let Some(description) = &schema.description {
|
||||
value["description"] = Value::String(description.clone());
|
||||
}
|
||||
value
|
||||
}
|
||||
|
||||
fn legacy_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,
|
||||
}
|
||||
}
|
||||
|
||||
fn schema_contains_reference(schema: &NormalizedSchema, location: &SourceLocation) -> bool {
|
||||
match &schema.kind {
|
||||
NormalizedSchemaKind::Reference { reference } => reference.location == *location,
|
||||
NormalizedSchemaKind::Object { properties, .. } => properties
|
||||
.values()
|
||||
.any(|schema| schema_contains_reference(schema, location)),
|
||||
NormalizedSchemaKind::Array { items } => items
|
||||
NormalizedSchemaKind::Array { items, .. } => items
|
||||
.as_deref()
|
||||
.is_some_and(|schema| schema_contains_reference(schema, location)),
|
||||
NormalizedSchemaKind::Composition { variants, .. } => variants
|
||||
@@ -908,6 +899,8 @@ fn method_name(method: crank_core::HttpMethod) -> &'static str {
|
||||
crank_core::HttpMethod::Put => "PUT",
|
||||
crank_core::HttpMethod::Patch => "PATCH",
|
||||
crank_core::HttpMethod::Delete => "DELETE",
|
||||
crank_core::HttpMethod::Head => "HEAD",
|
||||
crank_core::HttpMethod::Options => "OPTIONS",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -918,6 +911,8 @@ fn method_rank(method: crank_core::HttpMethod) -> u8 {
|
||||
crank_core::HttpMethod::Put => 2,
|
||||
crank_core::HttpMethod::Patch => 3,
|
||||
crank_core::HttpMethod::Delete => 4,
|
||||
crank_core::HttpMethod::Head => 5,
|
||||
crank_core::HttpMethod::Options => 6,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user