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
@@ -0,0 +1,382 @@
|
||||
use crank_core::HttpMethod;
|
||||
use crank_import::rest::model::NormalizedScalarKind;
|
||||
use crank_import::rest::{
|
||||
ImportFindingSeverity, NormalizationConfig, NormalizedSchemaKind, SourceDigest,
|
||||
normalize_verified_document, validate_normalized_ir,
|
||||
};
|
||||
|
||||
fn normalize(document: &str) -> crank_import::rest::NormalizedIr {
|
||||
normalize_verified_document(
|
||||
document,
|
||||
SourceDigest::parse("5".repeat(64)).unwrap(),
|
||||
&NormalizationConfig::default(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn variants_keep_exact_schema_coverage_and_reject_tampered_provenance() {
|
||||
let document = r#"
|
||||
openapi: 3.1.0
|
||||
info: {title: Exact variants, version: '1'}
|
||||
servers: [{url: https://example.test}]
|
||||
paths:
|
||||
/items:
|
||||
post:
|
||||
operationId: createItem
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
name: {type: string}
|
||||
responses:
|
||||
'201':
|
||||
description: ok
|
||||
content:
|
||||
application/json:
|
||||
schema: {type: object, properties: {id: {type: integer}}}
|
||||
"#;
|
||||
let mut ir = normalize(document);
|
||||
let request = ir.operations[0].request_body_variants[0]
|
||||
.schema
|
||||
.as_ref()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
request.location.pointer,
|
||||
"/paths/~1items/post/requestBody/content/application~1json/schema"
|
||||
);
|
||||
assert!(ir.coverage.iter().any(|entry| {
|
||||
entry.location.pointer
|
||||
== "/paths/~1items/post/requestBody/content/application~1json/schema/properties/name"
|
||||
}));
|
||||
|
||||
ir.operations[0].request_body_variants[0]
|
||||
.schema
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.location
|
||||
.pointer = "/paths/~1items/post/requestBody/content/application~1json".into();
|
||||
assert!(validate_normalized_ir(&ir).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exact_blockers_cover_unknown_method_style_media_and_missing_schema() {
|
||||
let ir = normalize(
|
||||
r#"
|
||||
openapi: 3.0.3
|
||||
info: {title: Findings, version: '1'}
|
||||
servers: [{url: https://example.test}]
|
||||
paths:
|
||||
/items/{id}:
|
||||
propfind: {responses: {'200': {description: nope}}}
|
||||
get:
|
||||
operationId: getItem
|
||||
parameters:
|
||||
- {name: id, in: path, required: false, style: matrix, schema: {type: string}}
|
||||
requestBody:
|
||||
content:
|
||||
text/plain: {schema: {type: string}}
|
||||
responses:
|
||||
'200':
|
||||
description: ok
|
||||
content:
|
||||
application/json: {}
|
||||
"#,
|
||||
);
|
||||
let findings = ir
|
||||
.findings
|
||||
.iter()
|
||||
.chain(
|
||||
ir.operations
|
||||
.iter()
|
||||
.flat_map(|operation| &operation.findings),
|
||||
)
|
||||
.collect::<Vec<_>>();
|
||||
for (code, pointer) in [
|
||||
("unsupported_http_method", "/paths/~1items~1{id}/propfind"),
|
||||
(
|
||||
"unsupported_parameter_style",
|
||||
"/paths/~1items~1{id}/get/parameters/0/style",
|
||||
),
|
||||
(
|
||||
"path_parameter_not_required",
|
||||
"/paths/~1items~1{id}/get/parameters/0",
|
||||
),
|
||||
(
|
||||
"unsupported_request_media_type",
|
||||
"/paths/~1items~1{id}/get/requestBody/content/text~1plain",
|
||||
),
|
||||
(
|
||||
"missing_response_media_schema",
|
||||
"/paths/~1items~1{id}/get/responses/200/content/application~1json",
|
||||
),
|
||||
] {
|
||||
assert!(
|
||||
findings.iter().any(|finding| {
|
||||
finding.code == code
|
||||
&& finding.location.pointer == pointer
|
||||
&& finding.severity == ImportFindingSeverity::Error
|
||||
}),
|
||||
"missing {code} at {pointer}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn form_encoding_and_unserializable_shapes_are_exact_blockers() {
|
||||
let ir = normalize(
|
||||
r#"
|
||||
openapi: 3.1.0
|
||||
info: {title: Form blockers, version: '1'}
|
||||
servers: [{url: https://example.test}]
|
||||
paths:
|
||||
/forms:
|
||||
post:
|
||||
operationId: sendForm
|
||||
requestBody:
|
||||
content:
|
||||
application/x-www-form-urlencoded:
|
||||
schema: {type: string}
|
||||
encoding:
|
||||
tag: {style: form, explode: false}
|
||||
multipart/form-data:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
metadata: {type: object, properties: {name: {type: string}}}
|
||||
nested_tags: {type: array, items: {type: array, items: {type: string}}}
|
||||
composed_metadata:
|
||||
oneOf:
|
||||
- {type: object, properties: {name: {type: string}}}
|
||||
responses: {'204': {description: Empty}}
|
||||
"#,
|
||||
);
|
||||
let findings = &ir.operations[0].findings;
|
||||
for (code, pointer) in [
|
||||
(
|
||||
"unsupported_form_schema",
|
||||
"/paths/~1forms/post/requestBody/content/application~1x-www-form-urlencoded/schema",
|
||||
),
|
||||
(
|
||||
"unsupported_form_encoding",
|
||||
"/paths/~1forms/post/requestBody/content/application~1x-www-form-urlencoded/encoding/tag/explode",
|
||||
),
|
||||
(
|
||||
"unsupported_form_schema",
|
||||
"/paths/~1forms/post/requestBody/content/multipart~1form-data/schema/properties/metadata",
|
||||
),
|
||||
(
|
||||
"unsupported_form_schema",
|
||||
"/paths/~1forms/post/requestBody/content/multipart~1form-data/schema/properties/nested_tags/items",
|
||||
),
|
||||
(
|
||||
"unsupported_form_schema",
|
||||
"/paths/~1forms/post/requestBody/content/multipart~1form-data/schema/properties/composed_metadata/oneOf/0",
|
||||
),
|
||||
] {
|
||||
assert!(
|
||||
findings.iter().any(|finding| {
|
||||
finding.code == code
|
||||
&& finding.location.pointer == pointer
|
||||
&& finding.severity == ImportFindingSeverity::Error
|
||||
}),
|
||||
"missing {code} at {pointer}"
|
||||
);
|
||||
}
|
||||
validate_normalized_ir(&ir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn swagger_form_data_arrays_require_multi_collection_format() {
|
||||
let ir = normalize(
|
||||
r#"
|
||||
swagger: '2.0'
|
||||
info: {title: Swagger form arrays, version: '1'}
|
||||
host: example.test
|
||||
schemes: [https]
|
||||
consumes: [application/x-www-form-urlencoded]
|
||||
paths:
|
||||
/forms:
|
||||
post:
|
||||
operationId: sendForm
|
||||
parameters:
|
||||
- {name: default_csv, in: formData, type: array, items: {type: string}}
|
||||
- {name: explicit_csv, in: formData, type: array, collectionFormat: csv, items: {type: string}}
|
||||
- {name: repeat_keys, in: formData, type: array, collectionFormat: multi, items: {type: string}}
|
||||
responses: {'204': {description: Empty}}
|
||||
"#,
|
||||
);
|
||||
let findings = &ir.operations[0].findings;
|
||||
for pointer in [
|
||||
"/paths/~1forms/post/parameters/0",
|
||||
"/paths/~1forms/post/parameters/1/collectionFormat",
|
||||
] {
|
||||
assert!(
|
||||
findings.iter().any(|finding| {
|
||||
finding.code == "unsupported_parameter_style"
|
||||
&& finding.location.pointer == pointer
|
||||
&& finding.severity == ImportFindingSeverity::Error
|
||||
}),
|
||||
"missing CSV blocker at {pointer}"
|
||||
);
|
||||
}
|
||||
assert!(!findings.iter().any(|finding| {
|
||||
finding.code == "unsupported_parameter_style"
|
||||
&& finding.location.pointer == "/paths/~1forms/post/parameters/2/collectionFormat"
|
||||
}));
|
||||
validate_normalized_ir(&ir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_schema_keywords_and_invalid_server_port_are_exact_blockers() {
|
||||
let ir = normalize(
|
||||
r#"
|
||||
openapi: 3.1.0
|
||||
info: {title: Schema and server blockers, version: '1'}
|
||||
servers:
|
||||
- {url: 'https://example.test:not-a-port'}
|
||||
- {url: "https://control.test/\n"}
|
||||
paths:
|
||||
/items:
|
||||
post:
|
||||
operationId: createItem
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
const: {kind: item}
|
||||
minProperties: 1
|
||||
maxProperties: 2
|
||||
dependentRequired: {kind: [name]}
|
||||
properties:
|
||||
quantity: {type: number, multipleOf: 0.5}
|
||||
responses: {'204': {description: Empty}}
|
||||
"#,
|
||||
);
|
||||
let findings = ir
|
||||
.findings
|
||||
.iter()
|
||||
.chain(ir.operations[0].findings.iter())
|
||||
.collect::<Vec<_>>();
|
||||
for (code, pointer) in [
|
||||
("invalid_server", "/servers/0/url"),
|
||||
("invalid_server", "/servers/1/url"),
|
||||
(
|
||||
"unsupported_schema_keyword",
|
||||
"/paths/~1items/post/requestBody/content/application~1json/schema/const",
|
||||
),
|
||||
(
|
||||
"unsupported_schema_keyword",
|
||||
"/paths/~1items/post/requestBody/content/application~1json/schema/minProperties",
|
||||
),
|
||||
(
|
||||
"unsupported_schema_keyword",
|
||||
"/paths/~1items/post/requestBody/content/application~1json/schema/maxProperties",
|
||||
),
|
||||
(
|
||||
"unsupported_schema_keyword",
|
||||
"/paths/~1items/post/requestBody/content/application~1json/schema/dependentRequired",
|
||||
),
|
||||
(
|
||||
"unsupported_schema_keyword",
|
||||
"/paths/~1items/post/requestBody/content/application~1json/schema/properties/quantity/multipleOf",
|
||||
),
|
||||
] {
|
||||
assert!(
|
||||
findings.iter().any(|finding| {
|
||||
finding.code == code
|
||||
&& finding.location.pointer == pointer
|
||||
&& finding.severity == ImportFindingSeverity::Error
|
||||
}),
|
||||
"missing {code} at {pointer}"
|
||||
);
|
||||
}
|
||||
validate_normalized_ir(&ir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn swagger_merges_path_form_data_and_preserves_file_type() {
|
||||
let ir = normalize(
|
||||
r#"
|
||||
swagger: '2.0'
|
||||
info: {title: Upload, version: '1'}
|
||||
host: example.test
|
||||
schemes: [https]
|
||||
consumes: [multipart/form-data]
|
||||
paths:
|
||||
/upload:
|
||||
parameters:
|
||||
- {name: file, in: formData, type: file, required: true}
|
||||
- {name: title, in: formData, type: string}
|
||||
post:
|
||||
operationId: upload
|
||||
parameters:
|
||||
- {name: title, in: formData, type: integer, required: true}
|
||||
responses:
|
||||
'204': {description: done}
|
||||
"#,
|
||||
);
|
||||
let operation = &ir.operations[0];
|
||||
let variant = &operation.request_body_variants[0];
|
||||
let NormalizedSchemaKind::Object {
|
||||
properties,
|
||||
required,
|
||||
..
|
||||
} = &variant.schema.as_ref().unwrap().kind
|
||||
else {
|
||||
panic!("formData must normalize to an object");
|
||||
};
|
||||
assert_eq!(required, &["file".to_owned(), "title".to_owned()]);
|
||||
assert!(matches!(
|
||||
properties["file"].kind,
|
||||
NormalizedSchemaKind::Scalar {
|
||||
scalar_type: NormalizedScalarKind::String,
|
||||
ref format,
|
||||
..
|
||||
} if format.as_deref() == Some("binary")
|
||||
));
|
||||
assert!(matches!(
|
||||
properties["title"].kind,
|
||||
NormalizedSchemaKind::Scalar {
|
||||
scalar_type: NormalizedScalarKind::Integer,
|
||||
..
|
||||
}
|
||||
));
|
||||
assert_eq!(
|
||||
properties["file"].location.pointer,
|
||||
"/paths/~1upload/parameters/0"
|
||||
);
|
||||
assert_eq!(
|
||||
properties["title"].location.pointer,
|
||||
"/paths/~1upload/post/parameters/0"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn head_declared_body_is_normalized_as_no_content() {
|
||||
let ir = normalize(
|
||||
r#"
|
||||
openapi: 3.0.3
|
||||
info: {title: Head, version: '1'}
|
||||
servers: [{url: https://example.test}]
|
||||
paths:
|
||||
/health:
|
||||
head:
|
||||
operationId: health
|
||||
responses:
|
||||
'200':
|
||||
description: ok
|
||||
content:
|
||||
application/json: {schema: {type: object}}
|
||||
"#,
|
||||
);
|
||||
let operation = &ir.operations[0];
|
||||
assert_eq!(operation.method, HttpMethod::Head);
|
||||
assert!(operation.response_schema.is_none());
|
||||
assert_eq!(operation.response_variants.len(), 1);
|
||||
assert!(operation.response_variants[0].schema.is_none());
|
||||
assert!(operation.response_variants[0].content_type.is_none());
|
||||
}
|
||||
Reference in new issue
Block a user