Единая transport-canonical идентичность внешних документов; normalizer v6 с immutable historical replay; matrix 1.0.4 / corpus 1.0.5. PR CI run840 success, локальная приёмка и independent review выполнены. Co-authored-by: bsodfather <bsodfather@gmail.com>
952 lines
45 KiB
Rust
952 lines
45 KiB
Rust
//! Executable normalizer probes for every normative support-matrix row.
|
|
use super::*;
|
|
use crank_import::rest::{NormalizedSchemaKind, normalize_verified_bundle};
|
|
|
|
fn assert_actionable(ir: &crank_import::rest::NormalizedIr, context: &str) {
|
|
let preview = preview_from_ir(ir);
|
|
assert!(
|
|
!preview.findings.iter().any(|finding| {
|
|
finding.severity == crank_import::rest::ImportFindingSeverity::Error
|
|
}),
|
|
"{context} has a document blocker: {:?}",
|
|
preview.findings
|
|
);
|
|
assert!(
|
|
preview
|
|
.groups
|
|
.iter()
|
|
.flat_map(|group| &group.operations)
|
|
.all(|candidate| {
|
|
!candidate.findings.iter().any(|finding| {
|
|
finding.severity == crank_import::rest::ImportFindingSeverity::Error
|
|
})
|
|
}),
|
|
"{context} unexpectedly blocked: {:?}",
|
|
preview.groups
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn ac_1_support_matrix_rows_have_exact_construct_contracts() {
|
|
let matrix_text = fs::read_to_string(tests_root().join("support-matrix.yaml"))
|
|
.expect("support matrix exists");
|
|
let matrix: serde_yaml::Value = serde_yaml::from_str(&matrix_text).expect("valid YAML");
|
|
assert_eq!(matrix["matrix_version"].as_str(), Some("1.0.4"));
|
|
assert!(
|
|
matrix["compatibility_impact"]
|
|
.as_str()
|
|
.is_some_and(|s| !s.trim().is_empty())
|
|
);
|
|
let rows = matrix["constructs"].as_sequence().expect("construct rows");
|
|
assert!(rows.len() >= 375);
|
|
let mut construct_keys = std::collections::BTreeSet::new();
|
|
for row in rows {
|
|
assert!(
|
|
row["specification_version"].as_str().is_some_and(|s| [
|
|
"OpenAPI 3.0",
|
|
"OpenAPI 3.1",
|
|
"Swagger 2.0"
|
|
]
|
|
.contains(&s))
|
|
);
|
|
assert!(row["construct_id"].as_str().is_some_and(|s| !s.is_empty()));
|
|
assert!(matches!(
|
|
row["status"].as_str(),
|
|
Some("supported" | "unsupported")
|
|
));
|
|
assert!(
|
|
row["preserved_semantics"]
|
|
.as_str()
|
|
.is_some_and(|s| !s.trim().is_empty())
|
|
);
|
|
assert!(
|
|
row["expected_finding"]
|
|
.as_str()
|
|
.is_some_and(|s| !s.trim().is_empty())
|
|
);
|
|
let key = format!(
|
|
"{}:{}",
|
|
row["specification_version"].as_str().unwrap(),
|
|
row["construct_id"].as_str().unwrap()
|
|
);
|
|
assert!(
|
|
construct_keys.insert(key.clone()),
|
|
"duplicate normative construct row: {key}"
|
|
);
|
|
if row["status"] == "supported" {
|
|
assert_eq!(
|
|
row["expected_finding"], "none",
|
|
"supported construct has a finding: {key}"
|
|
);
|
|
} else {
|
|
assert_ne!(
|
|
row["expected_finding"], "none",
|
|
"unsupported construct has no finding: {key}"
|
|
);
|
|
}
|
|
}
|
|
for version in ["OpenAPI 3.0", "OpenAPI 3.1", "Swagger 2.0"] {
|
|
// Exhaustive accounting: every normative row of this format must be
|
|
// matched by an executed probe before the loop completes.
|
|
let mut unprobed = rows
|
|
.iter()
|
|
.filter(|row| row["specification_version"] == version)
|
|
.map(|row| {
|
|
(
|
|
version.to_owned(),
|
|
row["construct_id"].as_str().unwrap().to_owned(),
|
|
)
|
|
})
|
|
.collect::<std::collections::BTreeSet<_>>();
|
|
let mut probed = |construct: &str| {
|
|
let taken = unprobed.remove(&(version.to_owned(), construct.to_owned()));
|
|
assert!(taken, "{version} double-probes or misses {construct}");
|
|
};
|
|
let ids = rows
|
|
.iter()
|
|
.filter(|r| r["specification_version"] == version)
|
|
.filter_map(|r| r["construct_id"].as_str())
|
|
.collect::<Vec<_>>();
|
|
for id in [
|
|
"http.method.get",
|
|
"http.method.post",
|
|
"http.method.put",
|
|
"http.method.patch",
|
|
"http.method.delete",
|
|
"http.method.head",
|
|
"http.method.options",
|
|
"parameter.location.path",
|
|
"parameter.location.query",
|
|
"parameter.location.header",
|
|
"parameter.location.cookie",
|
|
"request_body.application/json",
|
|
"request_body.application/x-www-form-urlencoded",
|
|
"request_body.multipart/form-data",
|
|
"reference.local",
|
|
"reference.external_allowlisted",
|
|
"composition.allOf",
|
|
"composition.oneOf",
|
|
"composition.anyOf",
|
|
"security.bearer",
|
|
"security.basic",
|
|
"security.api_key",
|
|
"media_type.application_xml.request",
|
|
"media_type.application_xml.response",
|
|
"http.method.trace",
|
|
"http.method.connect",
|
|
"parameter.path.declaration",
|
|
"schema.default.compound",
|
|
] {
|
|
assert!(ids.contains(&id), "{version} matrix misses {id}");
|
|
}
|
|
for security_kind in ["bearer", "basic", "api_key"] {
|
|
let construct = format!("security.{security_kind}");
|
|
let row = rows
|
|
.iter()
|
|
.find(|row| {
|
|
row["specification_version"] == version && row["construct_id"] == construct
|
|
})
|
|
.unwrap();
|
|
let probe = if version == "Swagger 2.0" {
|
|
json!({
|
|
"swagger":"2.0", "info":{"title":"Security probe","version":"1"},
|
|
"securityDefinitions":{"probe": if security_kind == "bearer" {
|
|
json!({"type":"oauth2","flow":"implicit","authorizationUrl":"https://auth.example.test","scopes":{}})
|
|
} else if security_kind == "api_key" {
|
|
json!({"type":"apiKey","name":"X-Key","in":"header"})
|
|
} else {
|
|
json!({"type":"basic"})
|
|
}},
|
|
"paths":{"/probe":{"get":{"security":[{"probe":[]}],"operationId":"securityProbe","responses":{"200":{"description":"ok"}}}}}
|
|
})
|
|
} else {
|
|
let scheme = match security_kind {
|
|
"bearer" => json!({"type":"http","scheme":"bearer"}),
|
|
"basic" => json!({"type":"http","scheme":"basic"}),
|
|
"api_key" => json!({"type":"apiKey","name":"X-Key","in":"header"}),
|
|
_ => unreachable!(),
|
|
};
|
|
json!({
|
|
"openapi": if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"},
|
|
"info":{"title":"Security probe","version":"1"},
|
|
"servers":[{"url":"https://matrix.example.test"}],
|
|
"components":{"securitySchemes":{"probe":scheme}},
|
|
"paths":{"/probe":{"get":{"security":[{"probe":[]}],"operationId":"securityProbe","responses":{"200":{"description":"ok"}}}}}
|
|
})
|
|
};
|
|
let source = serde_json::to_string(&probe).unwrap();
|
|
let ir = normalize(&source).unwrap();
|
|
validate_normalized_ir(&ir).unwrap();
|
|
let expected_finding = row["expected_finding"].as_str().unwrap();
|
|
let preview = preview_from_ir(&ir);
|
|
let candidate = &preview.groups[0].operations[0];
|
|
let requirement = &ir.operations[0].security_requirements.alternatives[0];
|
|
if expected_finding == "none" {
|
|
assert!(
|
|
requirement.supported,
|
|
"{version} {construct} is not selectable"
|
|
);
|
|
assert!(
|
|
candidate.findings.iter().all(|finding| {
|
|
finding.severity != crank_import::rest::ImportFindingSeverity::Error
|
|
}),
|
|
"{version} {construct} unexpectedly blocked: {:?}",
|
|
candidate.findings
|
|
);
|
|
assert!(ir.security_schemes.contains_key("probe"));
|
|
assert!(
|
|
ir.coverage.iter().any(|entry| {
|
|
entry.location.pointer == "/paths/~1probe/get/security/0"
|
|
&& entry.disposition == CoverageDisposition::Mapped
|
|
}),
|
|
"{version} {construct} requirement has no mapped source coverage"
|
|
);
|
|
} else {
|
|
let finding = ir
|
|
.findings
|
|
.iter()
|
|
.chain(
|
|
ir.operations
|
|
.iter()
|
|
.flat_map(|operation| operation.findings.iter()),
|
|
)
|
|
.find(|finding| {
|
|
finding.code == expected_finding
|
|
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error
|
|
})
|
|
.unwrap_or_else(|| {
|
|
panic!("{version} {construct} missing blocker: {:?}", ir.findings)
|
|
});
|
|
assert_eq!(finding.operation_key.as_deref(), Some("GET /probe"));
|
|
assert_eq!(finding.location.pointer, "/paths/~1probe/get/security");
|
|
assert!(
|
|
candidate
|
|
.findings
|
|
.iter()
|
|
.any(|finding| finding.code == expected_finding
|
|
&& finding.severity
|
|
== crank_import::rest::ImportFindingSeverity::Error)
|
|
);
|
|
assert!(ir.coverage.iter().any(|entry| entry.location.pointer
|
|
== "/paths/~1probe/get/security"
|
|
&& entry.disposition == CoverageDisposition::Finding));
|
|
}
|
|
probed(&construct);
|
|
}
|
|
{
|
|
let construct = "security.override_empty";
|
|
let override_probe = |path: &str| {
|
|
if version == "Swagger 2.0" {
|
|
json!({
|
|
"swagger":"2.0", "info":{"title":"Security override probe","version":"1"},
|
|
"securityDefinitions":{"probe":{"type":"basic"}},
|
|
"security":[{"probe":[]}],
|
|
"paths":{path:{"get":{"security":[],"operationId":"securityOverride","responses":{"200":{"description":"ok"}}}}}
|
|
})
|
|
} else {
|
|
json!({
|
|
"openapi": if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"},
|
|
"info":{"title":"Security override probe","version":"1"},
|
|
"servers":[{"url":"https://matrix.example.test"}],
|
|
"components":{"securitySchemes":{"probe":{"type":"http","scheme":"basic"}}},
|
|
"security":[{"probe":[]}],
|
|
"paths":{path:{"get":{"security":[],"operationId":"securityOverride","responses":{"200":{"description":"ok"}}}}}
|
|
})
|
|
}
|
|
};
|
|
let source = serde_json::to_string(&override_probe("/override")).unwrap();
|
|
let ir = normalize(&source).unwrap();
|
|
validate_normalized_ir(&ir).unwrap();
|
|
assert!(
|
|
ir.findings
|
|
.iter()
|
|
.all(|finding| finding.code != "unsupported_security_requirement"),
|
|
"explicit empty security override must disable inheritance: {:?}",
|
|
ir.findings
|
|
);
|
|
let preview = preview_from_ir(&ir);
|
|
let candidate = &preview.groups[0].operations[0];
|
|
assert!(
|
|
candidate
|
|
.findings
|
|
.iter()
|
|
.all(|finding| finding.severity
|
|
!= crank_import::rest::ImportFindingSeverity::Error),
|
|
"overridden operation must remain actionable: {:?}",
|
|
candidate.findings
|
|
);
|
|
probed(construct);
|
|
}
|
|
{
|
|
let construct = "security.mixed_operations";
|
|
let mixed_probe = if version == "Swagger 2.0" {
|
|
json!({
|
|
"swagger":"2.0", "info":{"title":"Mixed security probe","version":"1"},
|
|
"securityDefinitions":{"probe":{"type":"basic"}},
|
|
"security":[{"probe":[]}],
|
|
"paths":{
|
|
"/public":{"get":{"security":[],"operationId":"publicProbe","responses":{"200":{"description":"ok"}}}},
|
|
"/secured":{"get":{"operationId":"securedProbe","responses":{"200":{"description":"ok"}}}}
|
|
}
|
|
})
|
|
} else {
|
|
json!({
|
|
"openapi": if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"},
|
|
"info":{"title":"Mixed security probe","version":"1"},
|
|
"servers":[{"url":"https://matrix.example.test"}],
|
|
"components":{"securitySchemes":{"probe":{"type":"http","scheme":"basic"}}},
|
|
"security":[{"probe":[]}],
|
|
"paths":{
|
|
"/public":{"get":{"security":[],"operationId":"publicProbe","responses":{"200":{"description":"ok"}}}},
|
|
"/secured":{"get":{"operationId":"securedProbe","responses":{"200":{"description":"ok"}}}}
|
|
}
|
|
})
|
|
};
|
|
let source = serde_json::to_string(&mixed_probe).unwrap();
|
|
let ir = normalize(&source).unwrap();
|
|
validate_normalized_ir(&ir).unwrap();
|
|
let expected_finding = rows
|
|
.iter()
|
|
.find(|row| {
|
|
row["specification_version"] == version && row["construct_id"] == construct
|
|
})
|
|
.unwrap()["expected_finding"]
|
|
.as_str()
|
|
.unwrap();
|
|
assert_eq!(expected_finding, "none");
|
|
let secured_operation = ir
|
|
.operations
|
|
.iter()
|
|
.find(|operation| operation.key == "GET /secured")
|
|
.unwrap();
|
|
assert!(secured_operation.security_requirements.alternatives[0].supported);
|
|
assert_eq!(
|
|
secured_operation.security_requirements.alternatives[0]
|
|
.source_location
|
|
.as_ref()
|
|
.unwrap()
|
|
.pointer,
|
|
"/security/0"
|
|
);
|
|
let preview = preview_from_ir(&ir);
|
|
let mut by_key = std::collections::BTreeMap::new();
|
|
for group in &preview.groups {
|
|
for candidate in &group.operations {
|
|
by_key.insert(candidate.key.clone(), candidate);
|
|
}
|
|
}
|
|
let public = by_key.get("GET /public").expect("public candidate present");
|
|
assert!(
|
|
public
|
|
.findings
|
|
.iter()
|
|
.all(|finding| finding.severity
|
|
!= crank_import::rest::ImportFindingSeverity::Error),
|
|
"public operation must stay actionable: {:?}",
|
|
public.findings
|
|
);
|
|
let secured = by_key
|
|
.get("GET /secured")
|
|
.expect("secured candidate present");
|
|
assert!(
|
|
secured
|
|
.findings
|
|
.iter()
|
|
.all(|finding| finding.severity
|
|
!= crank_import::rest::ImportFindingSeverity::Error),
|
|
"supported secured operation must remain selectable before API binding: {:?}",
|
|
secured.findings
|
|
);
|
|
assert!(
|
|
ir.coverage
|
|
.iter()
|
|
.any(|entry| entry.location.pointer == "/security/0"
|
|
&& entry.disposition == CoverageDisposition::Mapped),
|
|
"mixed probe has no mapped security coverage"
|
|
);
|
|
probed(construct);
|
|
}
|
|
for method in [
|
|
"get", "post", "put", "patch", "delete", "head", "options", "trace", "connect",
|
|
] {
|
|
let construct = format!("http.method.{method}");
|
|
let row = rows
|
|
.iter()
|
|
.find(|row| {
|
|
row["specification_version"] == version && row["construct_id"] == construct
|
|
})
|
|
.unwrap();
|
|
let mut path_item = serde_json::Map::new();
|
|
path_item.insert(
|
|
method.to_owned(),
|
|
json!({
|
|
"operationId":"methodProbe", "responses":{"200":{"description":"ok"}}
|
|
}),
|
|
);
|
|
let probe = if version == "Swagger 2.0" {
|
|
json!({"swagger":"2.0","info":{"title":"Method probe","version":"1"},"paths":{"/probe":path_item,"/normal":{"get":{"operationId":"normalProbe","responses":{"200":{"description":"ok"}}}}}})
|
|
} else {
|
|
json!({"openapi":if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"},
|
|
"info":{"title":"Method probe","version":"1"},"servers":[{"url":"https://matrix.example.test"}],"paths":{"/probe":path_item,"/normal":{"get":{"operationId":"normalProbe","responses":{"200":{"description":"ok"}}}}}})
|
|
};
|
|
let source = serde_json::to_string(&probe).unwrap();
|
|
let ir = normalize(&source).unwrap();
|
|
validate_normalized_ir(&ir).unwrap();
|
|
if row["status"] == "supported" {
|
|
assert!(
|
|
ir.operations
|
|
.iter()
|
|
.any(
|
|
|operation| serde_json::to_value(operation.method).unwrap().as_str()
|
|
== Some(&method.to_ascii_uppercase())
|
|
),
|
|
"{version} {construct} was not preserved"
|
|
);
|
|
assert_eq!(row["expected_finding"], "none");
|
|
assert_actionable(&ir, &format!("{version} {construct}"));
|
|
} else {
|
|
let expected_finding = row["expected_finding"].as_str().unwrap();
|
|
assert!(
|
|
ir.operations
|
|
.iter()
|
|
.any(|operation| operation.path == "/normal"),
|
|
"{version} {construct} must not discard a supported neighbour"
|
|
);
|
|
assert!(
|
|
ir.findings
|
|
.iter()
|
|
.any(|finding| finding.code == expected_finding
|
|
&& finding.severity
|
|
== crank_import::rest::ImportFindingSeverity::Error),
|
|
"{version} {construct} missing exact finding {expected_finding}: {:?}",
|
|
ir.findings
|
|
);
|
|
assert!(
|
|
ir.coverage
|
|
.iter()
|
|
.any(|entry| entry.construct_id.ends_with(&format!("/{method}"))
|
|
&& entry.disposition == CoverageDisposition::Finding),
|
|
"{version} {construct} has no finding coverage"
|
|
);
|
|
}
|
|
probed(&construct);
|
|
}
|
|
for location in ["path", "query", "header", "cookie"] {
|
|
let construct = format!("parameter.location.{location}");
|
|
let row = rows
|
|
.iter()
|
|
.find(|row| {
|
|
row["specification_version"] == version && row["construct_id"] == construct
|
|
})
|
|
.unwrap();
|
|
let parameter = if version == "Swagger 2.0" {
|
|
json!({"name":"probe","in":location,"required":location == "path","type":"string"})
|
|
} else {
|
|
json!({"name":"probe","in":location,"required":location == "path","schema":{"type":"string"}})
|
|
};
|
|
let path = if location == "path" {
|
|
"/probe/{probe}"
|
|
} else {
|
|
"/probe"
|
|
};
|
|
let operation = json!({"operationId":"parameterProbe","parameters":[parameter],"responses":{"200":{"description":"ok"}}});
|
|
let probe = if version == "Swagger 2.0" {
|
|
json!({"swagger":"2.0","info":{"title":"Parameter probe","version":"1"},"paths":{path:{"get":operation}}})
|
|
} else {
|
|
json!({"openapi":if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"},
|
|
"info":{"title":"Parameter probe","version":"1"},"servers":[{"url":"https://matrix.example.test"}],"paths":{path:{"get":operation}}})
|
|
};
|
|
let source = serde_json::to_string(&probe).unwrap();
|
|
let ir = normalize(&source).unwrap();
|
|
validate_normalized_ir(&ir).unwrap();
|
|
if row["status"] == "supported" {
|
|
let parameter = ir.operations[0]
|
|
.parameters
|
|
.iter()
|
|
.find(|parameter| parameter.name == "probe")
|
|
.unwrap();
|
|
assert_eq!(
|
|
serde_json::to_value(parameter.location).unwrap(),
|
|
location,
|
|
"{version} {construct} location was changed"
|
|
);
|
|
assert!(
|
|
parameter.schema.is_some(),
|
|
"{version} {construct} schema was lost"
|
|
);
|
|
assert_eq!(parameter.required, location == "path");
|
|
let preview = preview_from_ir(&ir);
|
|
let candidate = &preview.groups[0].operations[0];
|
|
let expected_root = match location {
|
|
"path" => "$.request.path.",
|
|
"query" => "$.request.query.",
|
|
"header" => "$.request.headers.",
|
|
"cookie" => "$.request.cookies.",
|
|
_ => unreachable!(),
|
|
};
|
|
assert!(
|
|
candidate
|
|
.draft
|
|
.input_mapping
|
|
.rules
|
|
.iter()
|
|
.any(|rule| rule.source == "$.mcp.probe"
|
|
&& rule.target.starts_with(expected_root)),
|
|
"{version} {construct} input mapping missing {expected_root}: {:?}",
|
|
candidate.draft.input_mapping
|
|
);
|
|
assert_actionable(&ir, &format!("{version} {construct}"));
|
|
} else {
|
|
let expected_finding = row["expected_finding"].as_str().unwrap();
|
|
assert!(
|
|
ir.operations[0].findings.iter().any(|finding| finding.code
|
|
== expected_finding
|
|
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error
|
|
&& finding.location.pointer == "/paths/~1probe/get/parameters/0"),
|
|
"{version} {construct} missing exact finding {expected_finding}"
|
|
);
|
|
}
|
|
probed(&construct);
|
|
}
|
|
for media_type in [
|
|
"application/json",
|
|
"application/x-www-form-urlencoded",
|
|
"multipart/form-data",
|
|
] {
|
|
let construct = format!("request_body.{media_type}");
|
|
let row = rows
|
|
.iter()
|
|
.find(|row| {
|
|
row["specification_version"] == version && row["construct_id"] == construct
|
|
})
|
|
.unwrap();
|
|
let operation = if version == "Swagger 2.0" {
|
|
let mut operation =
|
|
json!({"operationId":"bodyProbe","responses":{"200":{"description":"ok"}}});
|
|
operation["consumes"] = json!([media_type]);
|
|
if media_type == "application/json" {
|
|
operation["parameters"] = json!([{"name":"body","in":"body","required":true,"schema":{"type":"object","properties":{"value":{"type":"string"}}}}]);
|
|
} else {
|
|
operation["parameters"] =
|
|
json!([{"name":"value","in":"formData","required":true,"type":"string"}]);
|
|
}
|
|
operation
|
|
} else {
|
|
json!({"operationId":"bodyProbe","requestBody":{"required":true,"content":{
|
|
(media_type):{"schema":{"type":"object","properties":{"value":{"type":"string"}}}}
|
|
}},"responses":{"200":{"description":"ok"}}})
|
|
};
|
|
let probe = if version == "Swagger 2.0" {
|
|
json!({"swagger":"2.0","info":{"title":"Body probe","version":"1"},"paths":{"/probe":{"post":operation}}})
|
|
} else {
|
|
json!({"openapi":if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"},
|
|
"info":{"title":"Body probe","version":"1"},"servers":[{"url":"https://matrix.example.test"}],"paths":{"/probe":{"post":operation}}})
|
|
};
|
|
let source = serde_json::to_string(&probe).unwrap();
|
|
let ir = normalize(&source).unwrap();
|
|
validate_normalized_ir(&ir).unwrap();
|
|
if row["status"] == "supported" {
|
|
let variant = ir.operations[0]
|
|
.request_body_variants
|
|
.iter()
|
|
.find(|variant| variant.content_type == media_type)
|
|
.expect("request body variant retained");
|
|
assert!(
|
|
variant.schema.is_some(),
|
|
"{version} {construct} schema was lost: {:?}",
|
|
variant
|
|
);
|
|
assert!(
|
|
variant.required,
|
|
"{version} {construct} required request body contract was lost: {variant:?}"
|
|
);
|
|
let preview = preview_from_ir(&ir);
|
|
let contract = preview.groups[0].operations[0]
|
|
.draft
|
|
.target
|
|
.request_contract
|
|
.as_ref()
|
|
.expect("draft request contract");
|
|
assert!(contract.body_required);
|
|
assert!(
|
|
contract
|
|
.variants
|
|
.iter()
|
|
.any(|variant| variant.media_type == media_type)
|
|
);
|
|
assert_actionable(&ir, &format!("{version} {construct}"));
|
|
} else {
|
|
let expected_finding = row["expected_finding"].as_str().unwrap();
|
|
assert!(
|
|
ir.operations[0].findings.iter().any(|finding| finding.code
|
|
== expected_finding
|
|
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error)
|
|
);
|
|
}
|
|
probed(&construct);
|
|
}
|
|
for (construct, is_request) in [
|
|
("media_type.application_xml.request", true),
|
|
("media_type.application_xml.response", false),
|
|
] {
|
|
let row = rows
|
|
.iter()
|
|
.find(|row| {
|
|
row["specification_version"] == version && row["construct_id"] == construct
|
|
})
|
|
.unwrap();
|
|
let operation = if version == "Swagger 2.0" {
|
|
let mut operation = json!({"operationId":"xmlProbe","responses":{"200":{"description":"ok","schema":{"type":"string"}}}});
|
|
if is_request {
|
|
operation["consumes"] = json!(["application/xml"]);
|
|
operation["parameters"] = json!([{"name":"body","in":"body","required":true,"schema":{"type":"string"}}]);
|
|
} else {
|
|
operation["produces"] = json!(["application/xml"]);
|
|
}
|
|
operation
|
|
} else if is_request {
|
|
json!({"operationId":"xmlProbe","requestBody":{"content":{"application/xml":{"schema":{"type":"string"}}}},"responses":{"200":{"description":"ok"}}})
|
|
} else {
|
|
json!({"operationId":"xmlProbe","responses":{"200":{"description":"ok","content":{"application/xml":{"schema":{"type":"string"}}}}}})
|
|
};
|
|
let probe = if version == "Swagger 2.0" {
|
|
json!({"swagger":"2.0","info":{"title":"XML probe","version":"1"},"paths":{"/probe":{"post":operation}}})
|
|
} else {
|
|
json!({"openapi":if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"},
|
|
"info":{"title":"XML probe","version":"1"},"servers":[{"url":"https://matrix.example.test"}],"paths":{"/probe":{"post":operation}}})
|
|
};
|
|
let source = serde_json::to_string(&probe).unwrap();
|
|
let ir = normalize(&source).unwrap();
|
|
validate_normalized_ir(&ir).unwrap();
|
|
let expected_finding = row["expected_finding"].as_str().unwrap();
|
|
assert_eq!(row["status"], "unsupported");
|
|
let expected_pointer = if version == "Swagger 2.0" {
|
|
if is_request {
|
|
"/paths/~1probe/post/consumes/0"
|
|
} else {
|
|
"/paths/~1probe/post/produces/0"
|
|
}
|
|
} else if is_request {
|
|
"/paths/~1probe/post/requestBody/content/application~1xml"
|
|
} else {
|
|
"/paths/~1probe/post/responses/200/content/application~1xml"
|
|
};
|
|
assert!(
|
|
ir.operations[0]
|
|
.findings
|
|
.iter()
|
|
.any(|finding| finding.code == expected_finding
|
|
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error
|
|
&& finding.location.pointer == expected_pointer),
|
|
"{version} {construct} missing exact blocker {expected_finding}: {:?}",
|
|
ir.operations[0].findings
|
|
);
|
|
probed(construct);
|
|
}
|
|
for composition in ["allOf", "oneOf", "anyOf"] {
|
|
let construct = format!("composition.{composition}");
|
|
let row = rows
|
|
.iter()
|
|
.find(|row| {
|
|
row["specification_version"] == version && row["construct_id"] == construct
|
|
})
|
|
.unwrap();
|
|
let schema = json!({(composition):[{"type":"object","properties":{"id":{"type":"string"}}},{"type":"object","properties":{"name":{"type":"string"}}}]});
|
|
let response = if version == "Swagger 2.0" {
|
|
json!({"description":"ok","schema":schema})
|
|
} else {
|
|
json!({"description":"ok","content":{"application/json":{"schema":schema}}})
|
|
};
|
|
let probe = if version == "Swagger 2.0" {
|
|
json!({"swagger":"2.0","info":{"title":"Composition probe","version":"1"},"paths":{"/probe":{"get":{"operationId":"compositionProbe","responses":{"200":response}}}}})
|
|
} else {
|
|
json!({"openapi":if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"},
|
|
"info":{"title":"Composition probe","version":"1"},"servers":[{"url":"https://matrix.example.test"}],"paths":{"/probe":{"get":{"operationId":"compositionProbe","responses":{"200":response}}}}})
|
|
};
|
|
let source = serde_json::to_string(&probe).unwrap();
|
|
let ir = normalize(&source).unwrap();
|
|
validate_normalized_ir(&ir).unwrap();
|
|
assert_eq!(row["status"], "supported");
|
|
assert!(ir.operations[0].response_variants[0].schema.as_ref().is_some_and(|schema|
|
|
if composition == "allOf" {
|
|
matches!(&schema.kind, NormalizedSchemaKind::Object { properties, .. }
|
|
if properties.contains_key("id") && properties.contains_key("name"))
|
|
} else {
|
|
matches!(&schema.kind, NormalizedSchemaKind::Composition { operator, variants }
|
|
if operator == composition && variants.len() == 2
|
|
&& matches!(&variants[0].kind, NormalizedSchemaKind::Object { properties, .. }
|
|
if properties.contains_key("id"))
|
|
&& matches!(&variants[1].kind, NormalizedSchemaKind::Object { properties, .. }
|
|
if properties.contains_key("name")))
|
|
}),
|
|
"{version} {construct} composition was not retained: {:?}", ir.operations[0].response_variants);
|
|
assert_eq!(row["expected_finding"], "none");
|
|
assert_actionable(&ir, &format!("{version} {construct}"));
|
|
probed(&construct);
|
|
}
|
|
for external in [false, true] {
|
|
let construct = if external {
|
|
"reference.external_allowlisted"
|
|
} else {
|
|
"reference.local"
|
|
};
|
|
let row = rows
|
|
.iter()
|
|
.find(|row| {
|
|
row["specification_version"] == version && row["construct_id"] == construct
|
|
})
|
|
.unwrap();
|
|
// The allowlisted-external row must exercise the same transport
|
|
// canonicalization the materializer keys snapshots with. The
|
|
// expected keys below are static literals, independent of the
|
|
// production `canonical_external_document_uri` helper: a
|
|
// percent-encoded dot segment collapses to its parent path and an
|
|
// apostrophe in the query is percent-encoded (`%27`).
|
|
let external_document = "Item: { type: object, properties: { id: { type: string } } }";
|
|
let external_variants: [(&str, &str); 3] = [
|
|
(
|
|
"https://schemas.example.test/root.yaml#/Item",
|
|
"https://schemas.example.test/root.yaml",
|
|
),
|
|
(
|
|
"https://schemas.example.test/a/%2e%2e/root.yaml#/Item",
|
|
"https://schemas.example.test/root.yaml",
|
|
),
|
|
(
|
|
"https://schemas.example.test/schema.json?filter='x'#/Item",
|
|
"https://schemas.example.test/schema.json?filter=%27x%27",
|
|
),
|
|
];
|
|
let local_reference = if version == "Swagger 2.0" {
|
|
"#/definitions/Item"
|
|
} else {
|
|
"#/components/schemas/Item"
|
|
};
|
|
let variants = if external {
|
|
external_variants
|
|
.iter()
|
|
.map(|(reference, canonical_uri)| (*reference, Some(*canonical_uri)))
|
|
.collect::<Vec<_>>()
|
|
} else {
|
|
vec![(local_reference, None)]
|
|
};
|
|
for (reference, canonical_uri) in variants {
|
|
let schema = json!({"$ref":reference});
|
|
let response = if version == "Swagger 2.0" {
|
|
json!({"description":"ok","schema":schema})
|
|
} else {
|
|
json!({"description":"ok","content":{"application/json":{"schema":schema}}})
|
|
};
|
|
let probe = if version == "Swagger 2.0" {
|
|
let mut root = json!({"swagger":"2.0","info":{"title":"Reference probe","version":"1"},"paths":{"/probe":{"get":{"operationId":"referenceProbe","responses":{"200":response}}}}});
|
|
if !external {
|
|
root["definitions"] =
|
|
json!({"Item":{"type":"object","properties":{"id":{"type":"string"}}}});
|
|
}
|
|
root
|
|
} else {
|
|
let mut root = json!({"openapi":if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"},
|
|
"info":{"title":"Reference probe","version":"1"},"servers":[{"url":"https://matrix.example.test"}],"paths":{"/probe":{"get":{"operationId":"referenceProbe","responses":{"200":response}}}}});
|
|
if !external {
|
|
root["components"] = json!({"schemas":{"Item":{"type":"object","properties":{"id":{"type":"string"}}}}});
|
|
}
|
|
root
|
|
};
|
|
let source = serde_json::to_string(&probe).unwrap();
|
|
let ir = if let Some(canonical_uri) = canonical_uri {
|
|
let snapshot = crank_import::rest::ExternalDocumentSnapshot {
|
|
canonical_uri: canonical_uri.to_owned(),
|
|
digest: SourceDigest::parse(hex(external_document.as_bytes())).unwrap(),
|
|
document: external_document.to_owned(),
|
|
};
|
|
let digest = SourceDigest::parse(hex(source.as_bytes())).unwrap();
|
|
normalize_verified_bundle(
|
|
&source,
|
|
digest,
|
|
&[snapshot],
|
|
&NormalizationConfig::default(),
|
|
)
|
|
.unwrap()
|
|
} else {
|
|
normalize(&source).unwrap()
|
|
};
|
|
validate_normalized_ir(&ir).unwrap();
|
|
assert_eq!(row["status"], "supported");
|
|
assert!(
|
|
!ir.reference_graph.edges.is_empty(),
|
|
"{version} {construct} did not resolve through a source-backed edge ({reference})"
|
|
);
|
|
assert!(
|
|
ir.unresolved_references.is_empty(),
|
|
"{version} {construct} remains unresolved ({reference})"
|
|
);
|
|
assert!(
|
|
matches!(
|
|
ir.operations[0].response_variants[0].schema.as_ref().map(|schema| &schema.kind),
|
|
Some(NormalizedSchemaKind::Object { properties, .. }) if properties.contains_key("id")
|
|
),
|
|
"{version} {construct} resolved schema was not projected into the operation ({reference})"
|
|
);
|
|
assert_actionable(&ir, &format!("{version} {construct} {reference}"));
|
|
assert_eq!(row["expected_finding"], "none");
|
|
}
|
|
probed(construct);
|
|
}
|
|
for code in 200..300 {
|
|
let construct = format!("response.{code}");
|
|
let response_row = rows
|
|
.iter()
|
|
.find(|row| {
|
|
row["specification_version"] == version && row["construct_id"] == construct
|
|
})
|
|
.unwrap_or_else(|| panic!("{version} matrix misses {construct}"));
|
|
assert!(
|
|
ids.contains(&construct.as_str()),
|
|
"{version} matrix misses {construct}"
|
|
);
|
|
assert_eq!(response_row["status"], "supported");
|
|
assert_eq!(response_row["expected_finding"], "none");
|
|
let has_body = !matches!(code, 204 | 205);
|
|
let source = if version == "Swagger 2.0" {
|
|
json!({
|
|
"swagger":"2.0", "info":{"title":"Matrix probe","version":"1"},
|
|
"paths":{"/probe":{"get":{"operationId":"matrixProbe","responses":{
|
|
code.to_string(): if has_body {json!({"description":"success","schema":{"type":"object","properties":{"id":{"type":"string"}}}})} else {json!({"description":"success"})}
|
|
}}}}
|
|
})
|
|
} else {
|
|
json!({
|
|
"openapi": if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"},
|
|
"info":{"title":"Matrix probe","version":"1"},
|
|
"servers":[{"url":"https://matrix.example.test"}],
|
|
"paths":{"/probe":{"get":{"operationId":"matrixProbe","responses":{
|
|
code.to_string(): if has_body {json!({"description":"success","content":{"application/json":{"schema":{"type":"object","properties":{"id":{"type":"string"}}}}}})} else {json!({"description":"success"})}
|
|
}}}}
|
|
})
|
|
};
|
|
let source = serde_json::to_string(&source).unwrap();
|
|
let ir = normalize(&source)
|
|
.unwrap_or_else(|error| panic!("{version} {construct} probe rejected: {error}"));
|
|
validate_normalized_ir(&ir).unwrap_or_else(|error| {
|
|
panic!("{version} {construct} probe has invalid ledger: {error}")
|
|
});
|
|
let response = ir.operations[0]
|
|
.response_variants
|
|
.iter()
|
|
.find(|variant| variant.status_code == code)
|
|
.unwrap_or_else(|| panic!("{version} {construct} was not preserved"));
|
|
assert_eq!(
|
|
response.schema.is_some(),
|
|
has_body,
|
|
"{version} {construct} body contract differs from 204/205 bodyless response semantics"
|
|
);
|
|
assert!(
|
|
!ir.operations[0]
|
|
.findings
|
|
.iter()
|
|
.any(|finding| finding.severity
|
|
== crank_import::rest::ImportFindingSeverity::Error),
|
|
"{version} {construct} unexpectedly blocked: {:?}",
|
|
ir.operations[0].findings
|
|
);
|
|
probed(&construct);
|
|
}
|
|
for construct in ["parameter.path.declaration", "schema.default.compound"] {
|
|
let row = rows
|
|
.iter()
|
|
.find(|row| {
|
|
row["specification_version"] == version && row["construct_id"] == construct
|
|
})
|
|
.unwrap();
|
|
assert_eq!(row["status"], "unsupported");
|
|
let operation = if construct == "parameter.path.declaration" {
|
|
json!({
|
|
"operationId":"pathDeclarationProbe",
|
|
"responses":{"200":{"description":"ok"}}
|
|
})
|
|
} else {
|
|
let schema = json!({
|
|
"type":"object",
|
|
"properties":{
|
|
"compound":{"type":"object","default":{"nested":"value"}},
|
|
"list":{"type":"array","items":{"type":"string"},"default":["a","b"]},
|
|
"scalar":{"type":"string","default":"kept"}
|
|
}
|
|
});
|
|
if version == "Swagger 2.0" {
|
|
json!({
|
|
"operationId":"compoundDefaultProbe",
|
|
"responses":{"200":{"description":"ok","schema":schema}}
|
|
})
|
|
} else {
|
|
json!({
|
|
"operationId":"compoundDefaultProbe",
|
|
"responses":{"200":{"description":"ok","content":{"application/json":{"schema":schema}}}}
|
|
})
|
|
}
|
|
};
|
|
let path = if construct == "parameter.path.declaration" {
|
|
"/probe/{item_id}"
|
|
} else {
|
|
"/probe"
|
|
};
|
|
let probe = if version == "Swagger 2.0" {
|
|
json!({
|
|
"swagger":"2.0", "info":{"title":"Matrix probe","version":"1"},
|
|
"paths":{path:{"get":operation}}
|
|
})
|
|
} else {
|
|
json!({
|
|
"openapi":if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"},
|
|
"info":{"title":"Matrix probe","version":"1"},
|
|
"servers":[{"url":"https://matrix.example.test"}],
|
|
"paths":{path:{"get":operation}}
|
|
})
|
|
};
|
|
let source = serde_json::to_string(&probe).unwrap();
|
|
let ir = normalize(&source)
|
|
.unwrap_or_else(|error| panic!("{version} {construct} probe rejected: {error}"));
|
|
validate_normalized_ir(&ir).unwrap_or_else(|error| {
|
|
panic!("{version} {construct} probe has invalid ledger: {error}")
|
|
});
|
|
let expected_finding = row["expected_finding"].as_str().unwrap();
|
|
assert!(
|
|
ir.operations[0]
|
|
.findings
|
|
.iter()
|
|
.any(|finding| finding.code == expected_finding
|
|
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error),
|
|
"{version} {construct} lacks {expected_finding}: {:?}",
|
|
ir.operations[0].findings
|
|
);
|
|
if construct == "schema.default.compound" {
|
|
assert_eq!(
|
|
ir.operations[0]
|
|
.findings
|
|
.iter()
|
|
.filter(|finding| finding.code == expected_finding)
|
|
.count(),
|
|
2,
|
|
"{version} must block the object and array defaults"
|
|
);
|
|
let schema = ir.operations[0].response_variants[0]
|
|
.schema
|
|
.as_ref()
|
|
.unwrap();
|
|
assert!(matches!(
|
|
&schema.kind,
|
|
NormalizedSchemaKind::Object { properties, .. }
|
|
if matches!(&properties["scalar"].kind,
|
|
NormalizedSchemaKind::Scalar { default_value: Some(value), .. }
|
|
if value == &crank_import::rest::model::NormalizedLiteral::String("kept".to_owned()))
|
|
));
|
|
}
|
|
probed(construct);
|
|
}
|
|
assert!(
|
|
unprobed.is_empty(),
|
|
"normative {version} rows lack an executed probe: {unprobed:?}"
|
|
);
|
|
}
|
|
}
|