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
@@ -221,10 +221,10 @@ paths:
|
||||
#[test]
|
||||
fn unsupported_method_has_exact_finding_pointer_and_coverage() {
|
||||
let ir = normalize(
|
||||
"openapi: 3.1.0\ninfo: { title: Unsupported }\npaths: { /ok: { get: { responses: { '200': { description: ok } } }, head: { responses: {} } } }",
|
||||
"openapi: 3.1.0\ninfo: { title: Unsupported }\npaths: { /ok: { get: { responses: { '200': { description: ok } } }, trace: { responses: {} } } }",
|
||||
);
|
||||
validate_normalized_ir(&ir).unwrap();
|
||||
let pointer = "/paths/~1ok/head";
|
||||
let pointer = "/paths/~1ok/trace";
|
||||
let construct_id = format!("3.1.0:{pointer}");
|
||||
assert!(ir.findings.iter().any(|finding| {
|
||||
finding.code == "unsupported_http_method"
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
use crank_import::rest::{
|
||||
NormalizationConfig, NormalizedIr, SourceDigest, normalize_verified_document, preview_from_ir,
|
||||
validate_normalized_ir,
|
||||
};
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn serialized_http_ir_preserves_variants_mime_aliases_and_mapping_without_parser_state() {
|
||||
let document = r#"
|
||||
openapi: 3.0.3
|
||||
info: {title: Replay}
|
||||
servers: [{url: 'https://example.test/api/v2'}]
|
||||
paths:
|
||||
/upload:
|
||||
post:
|
||||
operationId: upload
|
||||
parameters:
|
||||
- {in: query, name: body, schema: {type: string}}
|
||||
- {in: header, name: content_type, schema: {type: string}}
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema: {type: object, required: [name], properties: {name: {type: string}}}
|
||||
multipart/form-data:
|
||||
schema: {type: object, required: [file], properties: {file: {type: string, format: binary}}}
|
||||
encoding: {file: {contentType: 'image/png, image/jpeg'}}
|
||||
responses:
|
||||
'200': {description: OK, content: {application/json: {schema: {type: string}}}}
|
||||
'204': {description: Empty}
|
||||
"#;
|
||||
let ir = normalize_verified_document(
|
||||
document,
|
||||
SourceDigest::parse("0".repeat(64)).unwrap(),
|
||||
&NormalizationConfig::default(),
|
||||
)
|
||||
.unwrap();
|
||||
let restored: NormalizedIr = serde_json::from_slice(&serde_json::to_vec(&ir).unwrap()).unwrap();
|
||||
validate_normalized_ir(&restored).unwrap();
|
||||
let first = preview_from_ir(&ir);
|
||||
let second = preview_from_ir(&restored);
|
||||
assert_eq!(first, second);
|
||||
assert_eq!(second.groups[0].operations.len(), 1);
|
||||
let candidate = &second.groups[0].operations[0];
|
||||
let draft = &candidate.draft;
|
||||
let draft_value = serde_json::to_value(draft).unwrap();
|
||||
let persisted: crank_import::rest::RestImportCandidate =
|
||||
serde_json::from_value(draft_value.clone()).unwrap();
|
||||
assert_eq!(serde_json::to_value(persisted).unwrap(), draft_value);
|
||||
for arguments in [
|
||||
json!({"query_body": "query", "header_content_type": "header", "content_type": "application/json", "body": {"name": "Ada"}}),
|
||||
json!({"query_body": "query", "header_content_type": "header", "content_type": "multipart/form-data", "body": {"file": {"data_base64": "AA==", "content_type": "image/png"}}}),
|
||||
] {
|
||||
draft.input_schema.validate_shape(&arguments).unwrap();
|
||||
let mapped = draft
|
||||
.input_mapping
|
||||
.apply(&json!({"mcp": arguments}))
|
||||
.unwrap();
|
||||
assert_eq!(mapped["request"]["query"]["body"], "query");
|
||||
assert_eq!(mapped["request"]["headers"]["content_type"], "header");
|
||||
}
|
||||
assert_eq!(draft.target.base_url, "https://example.test/api/v2");
|
||||
assert_eq!(draft.target.response_contract.len(), 2);
|
||||
assert_eq!(
|
||||
draft.target.multipart_field_mime_types["multipart/form-data"]["file"],
|
||||
["image/png", "image/jpeg"]
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
use crank_import::rest::preview_document_legacy_v1;
|
||||
use crank_schema::SchemaKind;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn legacy_v1_keeps_reserved_names_flattening_and_scalar_semantics() {
|
||||
let preview = preview_document_legacy_v1(
|
||||
r#"
|
||||
openapi: 3.0.3
|
||||
info: {title: Legacy contract}
|
||||
servers: [{url: 'https://example.test'}]
|
||||
paths:
|
||||
/items/{id}:
|
||||
head:
|
||||
responses: {'204': {description: Empty}}
|
||||
post:
|
||||
operationId: createItem
|
||||
parameters:
|
||||
- {in: path, name: id, schema: {type: string}}
|
||||
- {in: query, name: body, schema: {type: string, default: old, nullable: true}}
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
file: {type: string, format: binary}
|
||||
responses:
|
||||
'200': {description: OK, content: {application/json: {schema: {type: string}}}}
|
||||
'204': {description: Empty}
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(preview.groups[0].operations.len(), 1);
|
||||
let draft = &preview.groups[0].operations[0].draft;
|
||||
assert!(draft.input_schema.fields["id"].required);
|
||||
assert_eq!(draft.input_schema.fields["file"].kind, SchemaKind::String);
|
||||
assert_eq!(draft.input_schema.fields["file"].format, None);
|
||||
assert!(!draft.input_schema.fields["body"].nullable);
|
||||
assert_eq!(draft.input_schema.fields["body"].default_value, None);
|
||||
assert!(draft.target.response_contract.is_empty());
|
||||
let mapped = draft
|
||||
.input_mapping
|
||||
.apply(&json!({"mcp": {"id": "1", "body": "query-value", "file": "legacy-string"}}))
|
||||
.unwrap();
|
||||
assert_eq!(mapped["request"]["query"]["body"], "query-value");
|
||||
assert_eq!(mapped["request"]["body"], json!({"file": "legacy-string"}));
|
||||
assert_eq!(draft.output_schema.kind, SchemaKind::String);
|
||||
}
|
||||
@@ -246,7 +246,7 @@ components:
|
||||
.iter()
|
||||
.map(|parameter| parameter.name.as_str())
|
||||
.collect::<Vec<_>>(),
|
||||
vec!["id", "query"]
|
||||
vec!["path_cookie", "id", "operation_cookie", "query"]
|
||||
);
|
||||
let errors = ir
|
||||
.findings
|
||||
@@ -257,7 +257,6 @@ components:
|
||||
"missing_parameter_name",
|
||||
"missing_parameter_location",
|
||||
"unsupported_parameter_location",
|
||||
"unsupported_cookie_parameter",
|
||||
"unresolved_parameter_reference",
|
||||
]
|
||||
.contains(&finding.code.as_str())
|
||||
@@ -267,10 +266,6 @@ components:
|
||||
assert_eq!(
|
||||
errors,
|
||||
std::collections::BTreeSet::from([
|
||||
(
|
||||
"unsupported_cookie_parameter",
|
||||
"/paths/~1items~1{id}/parameters/1",
|
||||
),
|
||||
("invalid_parameter", "/paths/~1items~1{id}/parameters/2",),
|
||||
(
|
||||
"missing_parameter_name",
|
||||
@@ -284,10 +279,6 @@ components:
|
||||
"unsupported_parameter_location",
|
||||
"/paths/~1items~1{id}/get/parameters/2",
|
||||
),
|
||||
(
|
||||
"unsupported_cookie_parameter",
|
||||
"/paths/~1items~1{id}/get/parameters/3",
|
||||
),
|
||||
])
|
||||
);
|
||||
assert!(
|
||||
@@ -299,7 +290,6 @@ components:
|
||||
"missing_parameter_name",
|
||||
"missing_parameter_location",
|
||||
"unsupported_parameter_location",
|
||||
"unsupported_cookie_parameter",
|
||||
"unresolved_parameter_reference",
|
||||
]
|
||||
.contains(&finding.code.as_str())
|
||||
@@ -363,12 +353,12 @@ parameters:
|
||||
let errors = ir
|
||||
.findings
|
||||
.iter()
|
||||
.chain(ir.operations[0].findings.iter())
|
||||
.filter(|finding| {
|
||||
[
|
||||
"invalid_parameter",
|
||||
"missing_parameter_name",
|
||||
"missing_parameter_location",
|
||||
"unsupported_parameter_location",
|
||||
"unsupported_cookie_parameter",
|
||||
"unresolved_parameter_reference",
|
||||
]
|
||||
@@ -392,10 +382,6 @@ parameters:
|
||||
"missing_parameter_location",
|
||||
"/paths/~1items~1{id}/get/parameters/1",
|
||||
),
|
||||
(
|
||||
"unsupported_parameter_location",
|
||||
"/paths/~1items~1{id}/get/parameters/2",
|
||||
),
|
||||
(
|
||||
"unsupported_cookie_parameter",
|
||||
"/paths/~1items~1{id}/get/parameters/3",
|
||||
@@ -405,12 +391,12 @@ parameters:
|
||||
assert!(
|
||||
ir.findings
|
||||
.iter()
|
||||
.chain(ir.operations[0].findings.iter())
|
||||
.filter(|finding| {
|
||||
[
|
||||
"invalid_parameter",
|
||||
"missing_parameter_name",
|
||||
"missing_parameter_location",
|
||||
"unsupported_parameter_location",
|
||||
"unsupported_cookie_parameter",
|
||||
"unresolved_parameter_reference",
|
||||
]
|
||||
@@ -418,6 +404,13 @@ parameters:
|
||||
})
|
||||
.all(|finding| finding.severity == ImportFindingSeverity::Error)
|
||||
);
|
||||
assert!(
|
||||
ir.operations[0].findings.iter().any(|finding| {
|
||||
finding.code == "missing_parameter_name"
|
||||
&& finding.location.pointer == "/paths/~1items~1{id}/get/parameters/0"
|
||||
}),
|
||||
"operation parameter errors must remain scoped to the affected operation"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
mod unit {
|
||||
use crank_core::HttpMethod;
|
||||
use crank_import::rest::{ImportParseError, preview_document, preview_document_legacy_v1};
|
||||
use crank_import::rest::{
|
||||
ImportParseError, NormalizationConfig, SourceDigest, normalize_verified_document,
|
||||
preview_document, preview_document_legacy_v1,
|
||||
};
|
||||
use crank_schema::SchemaKind;
|
||||
use serde_json::json;
|
||||
|
||||
@@ -174,7 +177,8 @@ paths:
|
||||
assert_eq!(labels.kind, SchemaKind::Array);
|
||||
assert_eq!(labels.items.as_ref().unwrap().kind, SchemaKind::String);
|
||||
|
||||
let payload = operation.draft.input_schema.field("payload").unwrap();
|
||||
let body = operation.draft.input_schema.field("body").unwrap();
|
||||
let payload = body.field("payload").unwrap();
|
||||
assert_eq!(payload.description.as_deref(), Some("Request payload"));
|
||||
assert_eq!(
|
||||
payload.field("enabled").unwrap().description.as_deref(),
|
||||
@@ -214,7 +218,7 @@ paths:
|
||||
let v2 = preview_document(document).unwrap();
|
||||
assert_eq!(
|
||||
v2.groups[0].operations[0].draft.output_schema.kind,
|
||||
SchemaKind::String
|
||||
SchemaKind::Oneof
|
||||
);
|
||||
|
||||
let legacy = preview_document_legacy_v1(document).unwrap();
|
||||
@@ -555,10 +559,10 @@ paths:
|
||||
.map(|rule| rule.target.as_str())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
assert!(input_fields.contains_key("email"));
|
||||
assert!(input_fields.contains_key("name"));
|
||||
assert!(targets.contains(&"$.request.body.email"));
|
||||
assert!(targets.contains(&"$.request.body.name"));
|
||||
assert!(input_fields.contains_key("body"));
|
||||
assert!(input_fields["body"].fields.contains_key("email"));
|
||||
assert!(input_fields["body"].fields.contains_key("name"));
|
||||
assert!(targets.contains(&"$.request.body"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -612,4 +616,344 @@ paths:
|
||||
assert!(codes.contains(&"weak_tool_name"));
|
||||
assert!(codes.contains(&"too_many_output_fields"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn previews_head_and_options_operations_and_rejects_trace_connect() {
|
||||
let document = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: Method coverage }
|
||||
paths:
|
||||
/health:
|
||||
head:
|
||||
operationId: healthHead
|
||||
responses: { '204': { description: Empty } }
|
||||
options:
|
||||
operationId: healthOptions
|
||||
responses: { '204': { description: Empty } }
|
||||
trace:
|
||||
responses: { '200': { description: Unsupported } }
|
||||
connect:
|
||||
responses: { '200': { description: Unsupported } }
|
||||
"#;
|
||||
|
||||
let preview = preview_document(document).unwrap();
|
||||
let operations = preview
|
||||
.groups
|
||||
.iter()
|
||||
.flat_map(|group| group.operations.iter())
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(operations.len(), 2);
|
||||
assert!(operations.iter().any(|operation| {
|
||||
operation.key == "HEAD /health" && operation.method == HttpMethod::Head
|
||||
}));
|
||||
assert!(operations.iter().any(|operation| {
|
||||
operation.key == "OPTIONS /health" && operation.method == HttpMethod::Options
|
||||
}));
|
||||
|
||||
let unsupported = preview
|
||||
.findings
|
||||
.iter()
|
||||
.filter(|finding| finding.code == "unsupported_http_method")
|
||||
.count();
|
||||
assert_eq!(unsupported, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_cookie_location_and_does_not_collapse_same_name_parameters() {
|
||||
let document = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: Cookie parameters }
|
||||
paths:
|
||||
/sessions:
|
||||
get:
|
||||
operationId: listSessions
|
||||
parameters:
|
||||
- name: token
|
||||
in: query
|
||||
schema: { type: string }
|
||||
- name: token
|
||||
in: cookie
|
||||
required: true
|
||||
schema: { type: string }
|
||||
responses:
|
||||
'200': { description: OK }
|
||||
"#;
|
||||
|
||||
let preview = preview_document(document).unwrap();
|
||||
let candidate = &preview.groups[0].operations[0];
|
||||
let cookie_and_query_rules = candidate
|
||||
.draft
|
||||
.input_mapping
|
||||
.rules
|
||||
.iter()
|
||||
.filter(|rule| {
|
||||
rule.target == "$.request.query.token" || rule.target == "$.request.cookies.token"
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(cookie_and_query_rules.len(), 2);
|
||||
assert_ne!(
|
||||
cookie_and_query_rules[0].source,
|
||||
cookie_and_query_rules[1].source
|
||||
);
|
||||
|
||||
let ir = normalize_verified_document(
|
||||
document,
|
||||
SourceDigest::parse("0".repeat(64)).unwrap(),
|
||||
&NormalizationConfig::default(),
|
||||
)
|
||||
.unwrap();
|
||||
let operation = &ir.operations[0];
|
||||
assert_eq!(operation.parameters.len(), 2);
|
||||
assert!(operation.parameters.iter().any(|parameter| {
|
||||
parameter.name == "token"
|
||||
&& parameter.location == crank_import::rest::RestParameterLocation::Query
|
||||
}));
|
||||
assert!(operation.parameters.iter().any(|parameter| {
|
||||
parameter.name == "token"
|
||||
&& parameter.location == crank_import::rest::RestParameterLocation::Cookie
|
||||
&& parameter.required
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_all_supported_request_media_variants() {
|
||||
let document = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: Request media variants }
|
||||
paths:
|
||||
/imports:
|
||||
post:
|
||||
operationId: importPayload
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [json_name]
|
||||
properties: { json_name: { type: string } }
|
||||
application/x-www-form-urlencoded:
|
||||
schema:
|
||||
type: object
|
||||
required: [form_name]
|
||||
properties: { form_name: { type: string } }
|
||||
multipart/form-data:
|
||||
schema:
|
||||
type: object
|
||||
required: [file]
|
||||
properties: { file: { type: string, format: binary } }
|
||||
encoding:
|
||||
file: { contentType: "image/png, image/jpeg" }
|
||||
responses:
|
||||
'202': { description: Accepted }
|
||||
"#;
|
||||
|
||||
let ir = normalize_verified_document(
|
||||
document,
|
||||
SourceDigest::parse("0".repeat(64)).unwrap(),
|
||||
&NormalizationConfig::default(),
|
||||
)
|
||||
.unwrap();
|
||||
let variants = &ir.operations[0].request_body_variants;
|
||||
let content_types = variants
|
||||
.iter()
|
||||
.map(|variant| variant.content_type.as_str())
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
content_types,
|
||||
vec![
|
||||
"application/json",
|
||||
"application/x-www-form-urlencoded",
|
||||
"multipart/form-data"
|
||||
]
|
||||
);
|
||||
assert!(variants.iter().all(|variant| variant.required));
|
||||
assert!(variants.iter().all(|variant| variant.schema.is_some()));
|
||||
assert_eq!(
|
||||
variants[2].multipart_field_mime_types.get("file"),
|
||||
Some(&vec!["image/png".to_owned(), "image/jpeg".to_owned()])
|
||||
);
|
||||
|
||||
let preview = preview_document(document).unwrap();
|
||||
let input_schema = &preview.groups[0].operations[0].draft.input_schema;
|
||||
assert_eq!(input_schema.kind, SchemaKind::Oneof);
|
||||
assert_eq!(input_schema.variants.len(), 3);
|
||||
assert!(
|
||||
input_schema
|
||||
.validate_shape(&json!({
|
||||
"content_type": "application/json",
|
||||
"body": {"json_name": "Ada"}
|
||||
}))
|
||||
.is_ok()
|
||||
);
|
||||
assert!(
|
||||
input_schema
|
||||
.validate_shape(&json!({
|
||||
"content_type": "application/json",
|
||||
"body": {"form_name": "Ada"}
|
||||
}))
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
input_schema
|
||||
.validate_shape(&json!({
|
||||
"content_type": "multipart/form-data",
|
||||
"body": {"file": {"data_base64": "SGVsbG8="}}
|
||||
}))
|
||||
.is_ok()
|
||||
);
|
||||
assert!(
|
||||
input_schema
|
||||
.validate_shape(&json!({
|
||||
"content_type": "multipart/form-data",
|
||||
"body": {"file": {"data_base64": "SGVsbG8=", "extra": true}}
|
||||
}))
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
input_schema
|
||||
.validate_shape(&json!({
|
||||
"content_type": "multipart/form-data",
|
||||
"body": {}
|
||||
}))
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(
|
||||
preview.groups[0].operations[0]
|
||||
.draft
|
||||
.target
|
||||
.multipart_field_mime_types
|
||||
.get("multipart/form-data")
|
||||
.and_then(|fields| fields.get("file")),
|
||||
Some(&vec!["image/png".to_owned(), "image/jpeg".to_owned()])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn optional_variant_pair_preserves_omission_and_nullable_null_in_mapping() {
|
||||
let document = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: Optional variants }
|
||||
paths:
|
||||
/events:
|
||||
post:
|
||||
operationId: createEvent
|
||||
requestBody:
|
||||
required: false
|
||||
content:
|
||||
application/json:
|
||||
schema: { type: object, nullable: true, properties: { name: { type: string } } }
|
||||
application/x-www-form-urlencoded:
|
||||
schema: { type: object, properties: { title: { type: string } } }
|
||||
responses: { '204': { description: Empty } }
|
||||
"#;
|
||||
let preview = preview_document(document).unwrap();
|
||||
let draft = &preview.groups[0].operations[0].draft;
|
||||
assert!(draft.input_schema.validate_shape(&json!({})).is_ok());
|
||||
assert!(
|
||||
draft
|
||||
.input_schema
|
||||
.validate_shape(&json!({"content_type": "application/json", "body": null}))
|
||||
.is_ok()
|
||||
);
|
||||
assert!(
|
||||
draft
|
||||
.input_schema
|
||||
.validate_shape(
|
||||
&json!({"content_type": "application/x-www-form-urlencoded", "body": null})
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
// Omission is represented by its own closed branch, so an incomplete
|
||||
// selector/body pair is rejected by the portable schema as well.
|
||||
assert!(
|
||||
draft
|
||||
.input_schema
|
||||
.validate_shape(&json!({"content_type": "application/json"}))
|
||||
.is_err()
|
||||
);
|
||||
let mapped = draft
|
||||
.input_mapping
|
||||
.apply(&json!({"mcp": {"content_type": "application/json", "body": null}}))
|
||||
.unwrap();
|
||||
assert_eq!(mapped["request"]["body"], json!(null));
|
||||
assert_eq!(mapped["request"]["body_content_type"], "application/json");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_multiple_success_responses_and_explicit_no_content() {
|
||||
let document = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: Response variants }
|
||||
paths:
|
||||
/items:
|
||||
get:
|
||||
operationId: getItems
|
||||
responses:
|
||||
'200':
|
||||
description: JSON result
|
||||
content:
|
||||
application/json:
|
||||
schema: { type: object, properties: { id: { type: string } } }
|
||||
'204': { description: No content }
|
||||
'206':
|
||||
description: Partial result
|
||||
content:
|
||||
application/json:
|
||||
schema: { type: array, items: { type: string } }
|
||||
"#;
|
||||
|
||||
let preview = preview_document(document).unwrap();
|
||||
let output_schema = &preview.groups[0].operations[0].draft.output_schema;
|
||||
let response_contract = &preview.groups[0].operations[0]
|
||||
.draft
|
||||
.target
|
||||
.response_contract;
|
||||
assert_eq!(output_schema.kind, SchemaKind::Oneof);
|
||||
assert_eq!(output_schema.variants.len(), 3);
|
||||
assert_eq!(
|
||||
response_contract
|
||||
.iter()
|
||||
.map(|descriptor| (descriptor.status_code, descriptor.media_type.as_deref()))
|
||||
.collect::<Vec<_>>(),
|
||||
vec![
|
||||
(200, Some("application/json")),
|
||||
(204, None),
|
||||
(206, Some("application/json")),
|
||||
]
|
||||
);
|
||||
assert!(
|
||||
output_schema
|
||||
.variants
|
||||
.iter()
|
||||
.any(|variant| variant.kind == SchemaKind::Null)
|
||||
);
|
||||
|
||||
let ir = normalize_verified_document(
|
||||
document,
|
||||
SourceDigest::parse("0".repeat(64)).unwrap(),
|
||||
&NormalizationConfig::default(),
|
||||
)
|
||||
.unwrap();
|
||||
let variants = &ir.operations[0].response_variants;
|
||||
assert_eq!(
|
||||
variants
|
||||
.iter()
|
||||
.map(|variant| variant.status_code)
|
||||
.collect::<Vec<_>>(),
|
||||
vec![200, 204, 206]
|
||||
);
|
||||
assert_eq!(
|
||||
variants[0].content_type.as_deref(),
|
||||
Some("application/json")
|
||||
);
|
||||
assert!(variants[0].schema.is_some());
|
||||
assert_eq!(variants[1].content_type, None);
|
||||
assert!(variants[1].schema.is_none());
|
||||
assert_eq!(
|
||||
variants[2].content_type.as_deref(),
|
||||
Some("application/json")
|
||||
);
|
||||
assert!(variants[2].schema.is_some());
|
||||
}
|
||||
}
|
||||
@@ -243,7 +243,11 @@ paths:
|
||||
let ir = normalize(compatible);
|
||||
assert!(matches!(
|
||||
ir.operations[0].request_body_schema.as_ref().map(|schema| &schema.kind),
|
||||
Some(NormalizedSchemaKind::Object { properties, required })
|
||||
Some(NormalizedSchemaKind::Object {
|
||||
properties,
|
||||
required,
|
||||
..
|
||||
})
|
||||
if properties.len() == 2 && required == &vec!["id".to_owned(), "name".to_owned()]
|
||||
));
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
use crank_import::rest::preview_document;
|
||||
use crank_schema::SchemaKind;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn preserves_format_default_nullable_and_string_enum_in_rest_projection() {
|
||||
let document = r#"
|
||||
openapi: 3.1.0
|
||||
info: { title: Schema metadata }
|
||||
paths:
|
||||
/metadata:
|
||||
get:
|
||||
operationId: metadata
|
||||
responses:
|
||||
'200':
|
||||
description: OK
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
identifier:
|
||||
type: string
|
||||
format: uuid
|
||||
default: 00000000-0000-0000-0000-000000000000
|
||||
nullable: true
|
||||
state:
|
||||
type: string
|
||||
enum: [draft, published]
|
||||
default: draft
|
||||
ratio:
|
||||
type: number
|
||||
format: double
|
||||
default: 1.5
|
||||
nullable: true
|
||||
"#;
|
||||
|
||||
let preview = preview_document(document).unwrap();
|
||||
let schema = &preview.groups[0].operations[0].draft.output_schema;
|
||||
|
||||
let identifier = schema.field("identifier").unwrap();
|
||||
assert_eq!(identifier.kind, SchemaKind::String);
|
||||
assert_eq!(identifier.format.as_deref(), Some("uuid"));
|
||||
assert_eq!(
|
||||
identifier.default_value,
|
||||
Some(json!("00000000-0000-0000-0000-000000000000"))
|
||||
);
|
||||
assert!(identifier.nullable);
|
||||
|
||||
let state = schema.field("state").unwrap();
|
||||
assert_eq!(state.kind, SchemaKind::Enum);
|
||||
assert_eq!(state.enum_values, vec!["draft", "published"]);
|
||||
assert_eq!(state.default_value, Some(json!("draft")));
|
||||
|
||||
let ratio = schema.field("ratio").unwrap();
|
||||
assert_eq!(ratio.kind, SchemaKind::Number);
|
||||
assert_eq!(ratio.format.as_deref(), Some("double"));
|
||||
assert_eq!(ratio.default_value, Some(json!(1.5)));
|
||||
assert!(ratio.nullable);
|
||||
|
||||
let serialized = serde_json::to_value(schema).unwrap();
|
||||
assert_eq!(serialized["fields"]["identifier"]["format"], "uuid");
|
||||
assert_eq!(serialized["fields"]["identifier"]["nullable"], true);
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
use crank_core::HttpMethod;
|
||||
use crank_import::rest::{
|
||||
NormalizationConfig, NormalizedSchemaKind, SourceDigest, normalize_verified_document,
|
||||
};
|
||||
|
||||
fn normalize(document: &str) -> crank_import::rest::NormalizedIr {
|
||||
normalize_verified_document(
|
||||
document,
|
||||
SourceDigest::parse("0".repeat(64)).unwrap(),
|
||||
&NormalizationConfig::default(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn swagger2_form_data_uses_effective_consumes_and_builds_body_schema() {
|
||||
let document = r#"
|
||||
swagger: '2.0'
|
||||
info: { title: Forms }
|
||||
host: example.test
|
||||
schemes: [https]
|
||||
consumes: [application/json, multipart/form-data]
|
||||
produces: [application/json]
|
||||
paths:
|
||||
/upload:
|
||||
post:
|
||||
operationId: upload
|
||||
consumes: [application/x-www-form-urlencoded, multipart/form-data]
|
||||
parameters:
|
||||
- { name: title, in: formData, type: string, required: true }
|
||||
- { name: count, in: formData, type: integer }
|
||||
- { name: file, in: formData, type: file }
|
||||
responses:
|
||||
'200': { description: OK, schema: { type: object, properties: { id: { type: string } } } }
|
||||
'204': { description: Empty }
|
||||
"#;
|
||||
|
||||
let ir = normalize(document);
|
||||
let operation = &ir.operations[0];
|
||||
assert_eq!(operation.method, HttpMethod::Post);
|
||||
assert_eq!(
|
||||
operation
|
||||
.request_body_variants
|
||||
.iter()
|
||||
.map(|variant| variant.content_type.as_str())
|
||||
.collect::<Vec<_>>(),
|
||||
vec!["application/x-www-form-urlencoded", "multipart/form-data"]
|
||||
);
|
||||
assert!(
|
||||
operation
|
||||
.request_body_variants
|
||||
.iter()
|
||||
.all(|variant| variant.required)
|
||||
);
|
||||
let Some(NormalizedSchemaKind::Object { properties, .. }) = operation
|
||||
.request_body_variants
|
||||
.first()
|
||||
.and_then(|variant| variant.schema.as_ref())
|
||||
.map(|schema| &schema.kind)
|
||||
else {
|
||||
panic!("formData must become an object request schema");
|
||||
};
|
||||
assert!(properties.contains_key("title"));
|
||||
assert!(properties.contains_key("count"));
|
||||
assert!(properties.contains_key("file"));
|
||||
assert!(
|
||||
operation
|
||||
.findings
|
||||
.iter()
|
||||
.all(|finding| finding.code != "unsupported_consumes")
|
||||
);
|
||||
assert_eq!(
|
||||
operation
|
||||
.response_variants
|
||||
.iter()
|
||||
.map(|variant| (variant.status_code, variant.content_type.as_deref()))
|
||||
.collect::<Vec<_>>(),
|
||||
vec![(200, Some("application/json")), (204, None)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn swagger2_operation_produces_overrides_global_and_preserves_json_variants() {
|
||||
let document = r#"
|
||||
swagger: '2.0'
|
||||
info: { title: Responses }
|
||||
host: example.test
|
||||
schemes: [https]
|
||||
produces: [application/json]
|
||||
paths:
|
||||
/items:
|
||||
get:
|
||||
operationId: listItems
|
||||
produces: [application/problem+json, application/xml]
|
||||
responses:
|
||||
'200': { description: OK, schema: { type: array, items: { type: string } } }
|
||||
"#;
|
||||
|
||||
let ir = normalize(document);
|
||||
let operation = &ir.operations[0];
|
||||
assert_eq!(
|
||||
operation
|
||||
.response_variants
|
||||
.iter()
|
||||
.map(|variant| variant.content_type.as_deref())
|
||||
.collect::<Vec<_>>(),
|
||||
vec![Some("application/problem+json")]
|
||||
);
|
||||
assert!(
|
||||
operation
|
||||
.findings
|
||||
.iter()
|
||||
.any(|finding| finding.code == "unsupported_produces")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn swagger2_imports_head_options_and_reports_path_blockers() {
|
||||
let document = r#"
|
||||
swagger: '2.0'
|
||||
info: { title: Methods }
|
||||
host: example.test
|
||||
schemes: [https]
|
||||
paths:
|
||||
/health:
|
||||
head:
|
||||
operationId: healthHead
|
||||
responses: { '204': { description: Empty } }
|
||||
options:
|
||||
operationId: healthOptions
|
||||
responses: { '204': { description: Empty } }
|
||||
/broken/{id}:
|
||||
get:
|
||||
parameters:
|
||||
- { name: other, in: path, required: true, type: string }
|
||||
responses: { '200': { description: OK } }
|
||||
/malformed: invalid
|
||||
"#;
|
||||
|
||||
let ir = normalize(document);
|
||||
assert!(
|
||||
ir.operations
|
||||
.iter()
|
||||
.any(|operation| operation.method == HttpMethod::Head)
|
||||
);
|
||||
assert!(
|
||||
ir.operations
|
||||
.iter()
|
||||
.any(|operation| operation.method == HttpMethod::Options)
|
||||
);
|
||||
assert!(ir.operations.iter().any(|operation| {
|
||||
operation.findings.iter().any(|finding| {
|
||||
finding.code == "path_parameter_placeholder_missing"
|
||||
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error
|
||||
})
|
||||
}));
|
||||
assert!(ir.findings.iter().any(|finding| {
|
||||
finding.code == "invalid_path_item"
|
||||
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error
|
||||
}));
|
||||
}
|
||||
Reference in new issue
Block a user