Files
crank/crates/crank-import/tests/reference_resolution/uri_transport.rs
T
bsodfather 1b6db09bd1
CI / Rust Checks (push) Successful in 25m54s
CI / UI Checks (push) Successful in 5s
CI / Community Image Smoke (push) Successful in 1m47s
CI / Frontend E2E (push) Successful in 10m12s
CI / Deploy (push) Successful in 1m22s
fix(import): единая идентичность внешних URI (#96)
Единая 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>
2026-10-11 06:19:16 +00:00

195 lines
7.2 KiB
Rust

//! F1 transport-parity cases for the pure resolver.
//!
//! Each case supplies a snapshot keyed exactly as the Admin URL transport
//! (`url::Url`) keys it, then drives `normalize_verified_bundle`. The cases are
//! registered from `reference_resolution.rs` so the parent file stays within the
//! repository file-size budget; the shared helpers (`digest`, `normalize_*`,
//! and the `rest` imports) come from `super` through `use super::*`.
use super::*;
// F1 (a): the external materializer derives its snapshot key from the URL
// transport (`url::Url`), which collapses a percent-encoded dot segment
// (`%2e%2e`) into the parent path. The pure resolver must derive the same
// canonical key, otherwise a supplied snapshot becomes a false
// `external_reference_unavailable` blocker. RED before the shared URI helper.
#[test]
fn encoded_dot_path_reference_matches_transport_canonical_snapshot() {
let document = r#"
openapi: 3.1.0
info: { title: Encoded dot transport parity }
servers: [{ url: https://api.example.test }]
paths:
/items:
get:
operationId: listItems
responses:
'200':
description: ok
content: { application/json: { schema: { $ref: 'https://schemas.example.test/a/%2e%2e/root.yaml#/Item' } } }
"#;
let snapshots = vec![ExternalDocumentSnapshot {
// Transport (`url::Url`) canonical form of the same reference.
canonical_uri: "https://schemas.example.test/root.yaml".to_owned(),
digest: digest('b'),
document: "Item: { type: object, required: [id], properties: { id: { type: string } } }"
.to_owned(),
}];
let config = NormalizationConfig {
external_references_enabled: true,
..NormalizationConfig::default()
};
let ir = normalize_verified_bundle(document, digest('a'), &snapshots, &config).unwrap();
assert!(
!ir.operations[0]
.findings
.iter()
.any(|finding| finding.code == "external_reference_unavailable"),
"encoded-dot $ref must match its transport-canonical snapshot, got: {:?}",
ir.operations[0].findings
);
assert!(
matches!(
ir.operations[0]
.response_schema
.as_ref()
.map(|schema| &schema.kind),
Some(NormalizedSchemaKind::Object { properties, .. }) if properties.contains_key("id")
),
"resolved schema must preserve its `id` property"
);
assert_eq!(ir.reference_graph.edges.len(), 1);
assert_eq!(
ir.reference_graph.edges[0].target.snapshot_digest,
digest('b')
);
assert!(ir.unresolved_references.is_empty());
}
// F1 (b): the URL transport percent-encodes an apostrophe inside a query string
// (`'` -> `%27`, the WHATWG special-query percent-encode set for http/https).
// The resolver must canonicalize the query the same way or the stored snapshot
// key never matches. RED before the shared URI helper.
//
// The `$ref` sits in a YAML double-quoted scalar: the reference itself contains
// an apostrophe, so a single-quoted scalar would terminate early and turn this
// into a parser syntax failure instead of the intended resolution RED.
#[test]
fn quoted_query_reference_matches_transport_canonical_snapshot() {
let document = r##"
openapi: 3.1.0
info: { title: Quoted query transport parity }
servers: [{ url: https://api.example.test }]
paths:
/items:
get:
operationId: listItems
responses:
'200':
description: ok
content: { application/json: { schema: { $ref: "https://schemas.example.test/schema.json?filter='x'#/Item" } } }
"##;
let snapshots = vec![ExternalDocumentSnapshot {
// Transport (`url::Url`) canonical form of the apostrophe query.
canonical_uri: "https://schemas.example.test/schema.json?filter=%27x%27".to_owned(),
digest: digest('b'),
document: "Item: { type: object, required: [id], properties: { id: { type: string } } }"
.to_owned(),
}];
let config = NormalizationConfig {
external_references_enabled: true,
..NormalizationConfig::default()
};
let ir = normalize_verified_bundle(document, digest('a'), &snapshots, &config).unwrap();
assert!(
!ir.operations[0]
.findings
.iter()
.any(|finding| finding.code == "external_reference_unavailable"),
"quoted-query $ref must match its transport-canonical snapshot, got: {:?}",
ir.operations[0].findings
);
assert!(
matches!(
ir.operations[0]
.response_schema
.as_ref()
.map(|schema| &schema.kind),
Some(NormalizedSchemaKind::Object { properties, .. }) if properties.contains_key("id")
),
"resolved schema must preserve its `id` property"
);
assert_eq!(ir.reference_graph.edges.len(), 1);
assert_eq!(
ir.reference_graph.edges[0].target.snapshot_digest,
digest('b')
);
assert!(ir.unresolved_references.is_empty());
}
// AC-3: after the encoded-dot alias is canonicalized, a relative child must be
// joined against the real canonical root, not the raw alias. RED while the
// resolver still keys the alias literally.
#[test]
fn encoded_dot_root_alias_resolves_relative_child_against_canonical_root() {
let document = r#"
openapi: 3.1.0
info: { title: Canonical root alias }
servers: [{ url: https://api.example.test }]
paths:
/items:
get:
operationId: listItems
responses:
'200':
description: ok
content: { application/json: { schema: { $ref: 'https://schemas.example.test/a/%2e%2e/root.yaml#/Item' } } }
"#;
let snapshots = vec![
ExternalDocumentSnapshot {
// Transport canonical of the encoded-dot root alias.
canonical_uri: "https://schemas.example.test/root.yaml".to_owned(),
digest: digest('b'),
document: "Item: { $ref: './child.yaml#/Item' }".to_owned(),
},
ExternalDocumentSnapshot {
canonical_uri: "https://schemas.example.test/child.yaml".to_owned(),
digest: digest('c'),
document:
"Item: { type: object, required: [id], properties: { id: { type: string } } }"
.to_owned(),
},
];
let config = NormalizationConfig {
external_references_enabled: true,
..NormalizationConfig::default()
};
let ir = normalize_verified_bundle(document, digest('a'), &snapshots, &config).unwrap();
assert!(
!ir.operations[0]
.findings
.iter()
.any(|finding| finding.code == "external_reference_unavailable"),
"encoded-dot root alias must resolve and join its child against the canonical root, got: {:?}",
ir.operations[0].findings
);
assert!(
matches!(
ir.operations[0]
.response_schema
.as_ref()
.map(|schema| &schema.kind),
Some(NormalizedSchemaKind::Object { properties, .. }) if properties.contains_key("id")
),
"child schema must resolve through the canonical root"
);
assert_eq!(
ir.reference_graph.dependency_digests,
vec![digest('b'), digest('c')]
);
assert!(ir.unresolved_references.is_empty());
}