feat(import): complete OpenAPI onboarding and verify delivery
This commit is contained in:
1 parent
bb3e376767
commit
e93de7015a
244 files changed
+57020
-5139
No files matched your search
@@ -1,5 +1,5 @@
|
||||
use crank_core::HttpMethod;
|
||||
use crank_import::rest::model::NormalizedScalarKind;
|
||||
use crank_import::rest::model::{NormalizedFinding, NormalizedLiteral, NormalizedScalarKind};
|
||||
use crank_import::rest::{
|
||||
ImportFindingSeverity, NormalizationConfig, NormalizedSchemaKind, SourceDigest,
|
||||
normalize_verified_document, validate_normalized_ir,
|
||||
@@ -14,6 +14,217 @@ fn normalize(document: &str) -> crank_import::rest::NormalizedIr {
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undeclared_path_placeholders_are_exact_blockers_in_all_supported_formats() {
|
||||
let openapi31 = r#"
|
||||
openapi: 3.1.0
|
||||
info: {title: Missing path declaration, version: '1'}
|
||||
servers: [{url: https://example.test}]
|
||||
paths:
|
||||
/items/{item_id}:
|
||||
get:
|
||||
operationId: getItem
|
||||
responses: {'200': {description: ok}}
|
||||
"#;
|
||||
let openapi30 = openapi31.replace("3.1.0", "3.0.3");
|
||||
let swagger2 = r#"
|
||||
swagger: '2.0'
|
||||
info: {title: Missing path declaration, version: '1'}
|
||||
host: example.test
|
||||
schemes: [https]
|
||||
paths:
|
||||
/items/{item_id}:
|
||||
get:
|
||||
operationId: getItem
|
||||
responses: {'200': {description: ok}}
|
||||
"#;
|
||||
let documents = [
|
||||
("OpenAPI 3.1", openapi31),
|
||||
("OpenAPI 3.0", openapi30.as_str()),
|
||||
("Swagger 2.0", swagger2),
|
||||
];
|
||||
|
||||
for (version, document) in documents {
|
||||
let ir = normalize(document);
|
||||
validate_normalized_ir(&ir).expect("the source-attested path blocker must validate");
|
||||
let operation = &ir.operations[0];
|
||||
assert!(
|
||||
operation.findings.iter().any(|finding| {
|
||||
finding.code == "path_parameter_not_declared"
|
||||
&& finding.severity == ImportFindingSeverity::Error
|
||||
&& finding.operation_key.as_deref() == Some("GET /items/{item_id}")
|
||||
&& finding.location.pointer == "/paths/~1items~1{item_id}/get"
|
||||
}),
|
||||
"{version} must report an undeclared path placeholder at the existing operation node: {:?}",
|
||||
operation.findings
|
||||
);
|
||||
}
|
||||
|
||||
let declared = r#"
|
||||
openapi: 3.1.0
|
||||
info: {title: Declared path parameter, version: '1'}
|
||||
servers: [{url: https://example.test}]
|
||||
paths:
|
||||
/items/{item_id}:
|
||||
get:
|
||||
operationId: getItem
|
||||
parameters:
|
||||
- {name: item_id, in: path, required: true, schema: {type: string}}
|
||||
responses: {'200': {description: ok}}
|
||||
"#;
|
||||
let mut ir = normalize(declared);
|
||||
let operation = &mut ir.operations[0];
|
||||
operation.findings.push(NormalizedFinding {
|
||||
code: "path_parameter_not_declared".to_owned(),
|
||||
severity: ImportFindingSeverity::Error,
|
||||
message: "fabricated blocker".to_owned(),
|
||||
construct_id: operation.stable_id.clone(),
|
||||
location: operation.location.clone(),
|
||||
operation_key: Some(operation.key.clone()),
|
||||
});
|
||||
assert!(
|
||||
validate_normalized_ir(&ir).is_err(),
|
||||
"a path blocker must not validate when the canonical source declares the placeholder"
|
||||
);
|
||||
|
||||
let referenced = r#"
|
||||
openapi: 3.1.0
|
||||
info: {title: Referenced path parameter, version: '1'}
|
||||
servers: [{url: https://example.test}]
|
||||
paths:
|
||||
/items/{item_id}:
|
||||
get:
|
||||
operationId: getItem
|
||||
parameters: [{$ref: '#/components/parameters/ItemId'}]
|
||||
responses: {'200': {description: ok}}
|
||||
components:
|
||||
parameters:
|
||||
ItemId: {name: item_id, in: path, required: true, schema: {type: string}}
|
||||
"#;
|
||||
let mut ir = normalize(referenced);
|
||||
validate_normalized_ir(&ir).expect("a resolved referenced path parameter must validate");
|
||||
let operation = &mut ir.operations[0];
|
||||
assert!(operation.parameters.iter().any(|parameter| {
|
||||
parameter.name == "item_id"
|
||||
&& parameter.location == crank_import::rest::model::RestParameterLocation::Path
|
||||
}));
|
||||
operation.findings.push(NormalizedFinding {
|
||||
code: "path_parameter_not_declared".to_owned(),
|
||||
severity: ImportFindingSeverity::Error,
|
||||
message: "fabricated blocker".to_owned(),
|
||||
construct_id: operation.stable_id.clone(),
|
||||
location: operation.location.clone(),
|
||||
operation_key: Some(operation.key.clone()),
|
||||
});
|
||||
assert!(
|
||||
validate_normalized_ir(&ir).is_err(),
|
||||
"a blocker must not validate when a referenced parameter is effective"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compound_schema_defaults_are_blocked_without_changing_scalar_defaults() {
|
||||
let openapi31 = r#"
|
||||
openapi: 3.1.0
|
||||
info: {title: Compound defaults, version: '1'}
|
||||
servers: [{url: https://example.test}]
|
||||
paths:
|
||||
/defaults:
|
||||
get:
|
||||
operationId: getDefaults
|
||||
responses:
|
||||
'200':
|
||||
description: ok
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
compound: {type: object, default: {nested: value}}
|
||||
list: {type: array, items: {type: string}, default: [a, b]}
|
||||
scalar: {type: string, default: original}
|
||||
"#;
|
||||
let openapi30 = openapi31.replace("3.1.0", "3.0.3");
|
||||
let swagger2 = r#"
|
||||
swagger: '2.0'
|
||||
info: {title: Compound defaults, version: '1'}
|
||||
host: example.test
|
||||
schemes: [https]
|
||||
paths:
|
||||
/defaults:
|
||||
get:
|
||||
operationId: getDefaults
|
||||
responses:
|
||||
'200':
|
||||
description: ok
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
compound: {type: object, default: {nested: value}}
|
||||
list: {type: array, items: {type: string}, default: [a, b]}
|
||||
scalar: {type: string, default: original}
|
||||
"#;
|
||||
let documents = [
|
||||
("OpenAPI 3.1", openapi31),
|
||||
("OpenAPI 3.0", openapi30.as_str()),
|
||||
("Swagger 2.0", swagger2),
|
||||
];
|
||||
|
||||
for (version, document) in documents {
|
||||
let ir = normalize(document);
|
||||
validate_normalized_ir(&ir).expect("source-attested compound blockers must validate");
|
||||
let operation = &ir.operations[0];
|
||||
let mut blockers = operation
|
||||
.findings
|
||||
.iter()
|
||||
.filter(|finding| finding.code == "unsupported_compound_default")
|
||||
.collect::<Vec<_>>();
|
||||
blockers.sort_by(|left, right| left.location.pointer.cmp(&right.location.pointer));
|
||||
assert_eq!(
|
||||
blockers.len(),
|
||||
2,
|
||||
"{version} should block only object/array defaults: {:?}",
|
||||
operation.findings
|
||||
);
|
||||
assert!(blockers.iter().all(|finding| {
|
||||
finding.severity == ImportFindingSeverity::Error
|
||||
&& finding.location.pointer.ends_with("/default")
|
||||
&& finding.operation_key.as_deref() == Some("GET /defaults")
|
||||
}));
|
||||
let Some(NormalizedSchemaKind::Object { properties, .. }) = operation
|
||||
.response_variants
|
||||
.first()
|
||||
.and_then(|variant| variant.schema.as_ref())
|
||||
.map(|schema| &schema.kind)
|
||||
else {
|
||||
panic!("{version} response schema should remain an object");
|
||||
};
|
||||
assert!(matches!(
|
||||
&properties["scalar"].kind,
|
||||
NormalizedSchemaKind::Scalar { default_value: Some(value), .. }
|
||||
if value == &NormalizedLiteral::String("original".to_owned())
|
||||
));
|
||||
|
||||
let mut tampered = ir.clone();
|
||||
let operation = &mut tampered.operations[0];
|
||||
let scalar_default = operation
|
||||
.findings
|
||||
.iter_mut()
|
||||
.find(|finding| finding.code == "unsupported_compound_default")
|
||||
.unwrap();
|
||||
let scalar_pointer = if version == "Swagger 2.0" {
|
||||
"/paths/~1defaults/get/responses/200/schema/properties/scalar/default"
|
||||
} else {
|
||||
"/paths/~1defaults/get/responses/200/content/application~1json/schema/properties/scalar/default"
|
||||
};
|
||||
scalar_default.location.pointer = scalar_pointer.to_owned();
|
||||
assert!(
|
||||
validate_normalized_ir(&tampered).is_err(),
|
||||
"{version} must reject a compound-default finding moved onto a scalar source value"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn variants_keep_exact_schema_coverage_and_reject_tampered_provenance() {
|
||||
let document = r#"
|
||||
|
||||
Reference in new issue
Block a user