feat(import): resolve references and schema composition
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
use admin_api::service::{OpenApiImportCreatePayload, OpenApiUpload, OpenApiUploadLocale};
|
||||
use admin_api::service::{
|
||||
AdminServiceBuilder, OpenApiImportCreatePayload, OpenApiUpload, OpenApiUploadLocale,
|
||||
};
|
||||
use crank_core::WorkspaceId;
|
||||
use crank_registry::ImportJobStatus;
|
||||
use serial_test::serial;
|
||||
@@ -7,6 +9,8 @@ use super::common::{
|
||||
test_auth_settings, test_registry, test_secret_crypto, test_service, test_storage_root,
|
||||
};
|
||||
|
||||
mod external_references;
|
||||
|
||||
const OPENAPI3: &str = r#"
|
||||
openapi: 3.0.3
|
||||
info:
|
||||
@@ -73,11 +77,11 @@ async fn previews_openapi_and_creates_draft_operations() {
|
||||
assert_eq!(preview_job.status, ImportJobStatus::Pending);
|
||||
assert_eq!(
|
||||
preview_job.preview_payload["normalization"]["normalizer_version"],
|
||||
"normalized-ir-v2"
|
||||
"normalized-ir-v3"
|
||||
);
|
||||
assert_eq!(
|
||||
preview_job.preview_payload["normalization"]["projection_version"],
|
||||
"preview-v2"
|
||||
"preview-v3"
|
||||
);
|
||||
assert!(preview_job.preview_payload["normalization"]["ir_fingerprint"].is_string());
|
||||
|
||||
@@ -236,7 +240,7 @@ async fn unknown_selected_operations_are_persisted_in_the_canonical_replay() {
|
||||
async fn concurrent_openapi_import_replays_the_same_atomic_result() {
|
||||
let registry = test_registry().await;
|
||||
let service = test_service(
|
||||
registry,
|
||||
registry.clone(),
|
||||
test_storage_root("openapi_import_replay"),
|
||||
test_auth_settings(),
|
||||
test_secret_crypto(),
|
||||
@@ -271,6 +275,31 @@ async fn concurrent_openapi_import_replays_the_same_atomic_result() {
|
||||
service.list_operations(&workspace_id).await.unwrap().len(),
|
||||
1
|
||||
);
|
||||
let completed_before = registry
|
||||
.get_import_job(&workspace_id, &job_id)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let empty = serde_json::json!([]);
|
||||
let failed_at = time::OffsetDateTime::now_utc();
|
||||
registry
|
||||
.finish_import_job(crank_registry::FinishImportJobRequest {
|
||||
id: &job_id,
|
||||
status: ImportJobStatus::Failed,
|
||||
created_operation_ids: &empty,
|
||||
error_text: Some("late_verification_failure"),
|
||||
finished_at: &failed_at,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let completed_after = registry
|
||||
.get_import_job(&workspace_id, &job_id)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(completed_after.status, ImportJobStatus::Completed);
|
||||
assert_eq!(completed_after.error_text, completed_before.error_text);
|
||||
assert_eq!(completed_after.finished_at, completed_before.finished_at);
|
||||
|
||||
let conflicting_replay = service
|
||||
.create_openapi_import(
|
||||
@@ -326,6 +355,13 @@ async fn apply_fails_closed_when_the_preview_parser_contract_drifts() {
|
||||
.await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert_failed_job_and_detached_sources(
|
||||
®istry,
|
||||
&workspace_id,
|
||||
&job_id,
|
||||
"import_replay_verification_failed",
|
||||
)
|
||||
.await;
|
||||
assert!(
|
||||
service
|
||||
.list_operations(&workspace_id)
|
||||
@@ -395,6 +431,13 @@ async fn apply_fails_closed_for_each_normalization_contract_field_and_digest_sha
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert_failed_job_and_detached_sources(
|
||||
®istry,
|
||||
&workspace_id,
|
||||
&job_id,
|
||||
"import_replay_verification_failed",
|
||||
)
|
||||
.await;
|
||||
assert!(
|
||||
service
|
||||
.list_operations(&workspace_id)
|
||||
@@ -449,7 +492,7 @@ async fn legacy_job_without_normalization_contract_replays_until_expiry() {
|
||||
async fn blocker_finding_keeps_valid_preview_but_prevents_draft_mutation() {
|
||||
let registry = test_registry().await;
|
||||
let service = test_service(
|
||||
registry,
|
||||
registry.clone(),
|
||||
test_storage_root("openapi_import_blocker"),
|
||||
test_auth_settings(),
|
||||
test_secret_crypto(),
|
||||
@@ -503,11 +546,124 @@ paths:
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
);
|
||||
assert_failed_job_and_detached_sources(
|
||||
®istry,
|
||||
&workspace_id,
|
||||
&preview.job_id.as_str().into(),
|
||||
"reference_resolution_blocked",
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn operation_level_reference_blocker_prevents_draft_mutation() {
|
||||
async fn operation_blockers_apply_only_to_selected_keys_and_full_selection_fails_atomically() {
|
||||
let registry = test_registry().await;
|
||||
let service = test_service(
|
||||
registry.clone(),
|
||||
test_storage_root("openapi_import_selected_blockers"),
|
||||
test_auth_settings(),
|
||||
test_secret_crypto(),
|
||||
);
|
||||
let workspace_id = WorkspaceId::new("ws_default");
|
||||
let upload = OpenApiUpload {
|
||||
bytes: br#"
|
||||
openapi: 3.1.0
|
||||
info: { title: Selected blockers }
|
||||
servers: [{ url: https://api.example.test }]
|
||||
paths:
|
||||
/valid:
|
||||
get:
|
||||
operationId: validOperation
|
||||
responses: { '200': { description: ok } }
|
||||
/broken:
|
||||
get:
|
||||
operationId: brokenOperation
|
||||
responses:
|
||||
'200':
|
||||
description: unresolved external schema
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: 'https://schemas.example.test/missing.yaml#/Result' }
|
||||
"#
|
||||
.to_vec(),
|
||||
mime_type: "application/yaml".to_owned(),
|
||||
locale: OpenApiUploadLocale::En,
|
||||
};
|
||||
|
||||
let valid_preview = service
|
||||
.preview_openapi_import(&workspace_id, upload.clone())
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(
|
||||
valid_preview.preview.findings.iter().all(|finding| {
|
||||
finding.severity != crank_import::rest::ImportFindingSeverity::Error
|
||||
})
|
||||
);
|
||||
let broken = valid_preview
|
||||
.preview
|
||||
.groups
|
||||
.iter()
|
||||
.flat_map(|group| &group.operations)
|
||||
.find(|operation| operation.key == "GET /broken")
|
||||
.unwrap();
|
||||
assert!(
|
||||
broken.findings.iter().any(|finding| {
|
||||
finding.severity == crank_import::rest::ImportFindingSeverity::Error
|
||||
})
|
||||
);
|
||||
|
||||
let valid = service
|
||||
.create_openapi_import(
|
||||
&workspace_id,
|
||||
&valid_preview.job_id.as_str().into(),
|
||||
OpenApiImportCreatePayload {
|
||||
selected_operation_keys: vec!["GET /valid".to_owned()],
|
||||
server_url: None,
|
||||
conflict_mode: "skip".to_owned(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(valid.created.len(), 1);
|
||||
|
||||
let full_preview = service
|
||||
.preview_openapi_import(&workspace_id, upload)
|
||||
.await
|
||||
.unwrap();
|
||||
let result = service
|
||||
.create_openapi_import(
|
||||
&workspace_id,
|
||||
&full_preview.job_id.as_str().into(),
|
||||
OpenApiImportCreatePayload {
|
||||
selected_operation_keys: vec!["GET /valid".to_owned(), "GET /broken".to_owned()],
|
||||
server_url: None,
|
||||
conflict_mode: "rename".to_owned(),
|
||||
},
|
||||
)
|
||||
.await;
|
||||
assert!(result.is_err());
|
||||
assert_eq!(
|
||||
service.list_operations(&workspace_id).await.unwrap().len(),
|
||||
1,
|
||||
"the full selection must not create either selected Draft"
|
||||
);
|
||||
let failed = registry
|
||||
.get_import_job(&workspace_id, &full_preview.job_id.as_str().into())
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(failed.status, ImportJobStatus::Failed);
|
||||
assert_eq!(
|
||||
failed.error_text.as_deref(),
|
||||
Some("reference_resolution_blocked")
|
||||
);
|
||||
assert!(failed.finished_at.is_some());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn local_operation_reference_resolves_and_creates_draft() {
|
||||
let registry = test_registry().await;
|
||||
let service = test_service(
|
||||
registry,
|
||||
@@ -520,6 +676,7 @@ async fn operation_level_reference_blocker_prevents_draft_mutation() {
|
||||
bytes: br#"
|
||||
openapi: 3.0.3
|
||||
info: { title: References }
|
||||
servers: [{ url: https://api.example.test }]
|
||||
paths:
|
||||
/referenced:
|
||||
get:
|
||||
@@ -551,32 +708,26 @@ components:
|
||||
preview.preview.groups[0].operations[0]
|
||||
.findings
|
||||
.iter()
|
||||
.any(|finding| {
|
||||
finding.code == "unresolved_reference"
|
||||
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error
|
||||
})
|
||||
.all(|finding| finding.code != "unresolved_reference")
|
||||
);
|
||||
assert_eq!(preview.preview.groups[0].operations[0].output_fields, 1);
|
||||
|
||||
assert!(
|
||||
service
|
||||
.create_openapi_import(
|
||||
&workspace_id,
|
||||
&preview.job_id.as_str().into(),
|
||||
OpenApiImportCreatePayload {
|
||||
selected_operation_keys: vec!["GET /referenced".to_owned()],
|
||||
server_url: None,
|
||||
conflict_mode: "skip".to_owned(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
service
|
||||
.list_operations(&workspace_id)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
let created = service
|
||||
.create_openapi_import(
|
||||
&workspace_id,
|
||||
&preview.job_id.as_str().into(),
|
||||
OpenApiImportCreatePayload {
|
||||
selected_operation_keys: vec!["GET /referenced".to_owned()],
|
||||
server_url: None,
|
||||
conflict_mode: "skip".to_owned(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(created.created.len(), 1);
|
||||
assert_eq!(
|
||||
service.list_operations(&workspace_id).await.unwrap().len(),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
@@ -587,3 +738,44 @@ fn openapi_upload() -> OpenApiUpload {
|
||||
locale: OpenApiUploadLocale::En,
|
||||
}
|
||||
}
|
||||
|
||||
async fn assert_failed_job_and_detached_sources(
|
||||
registry: &crank_registry::PostgresRegistry,
|
||||
workspace_id: &WorkspaceId,
|
||||
job_id: &crank_registry::ImportJobId,
|
||||
expected_error: &str,
|
||||
) {
|
||||
let job = registry
|
||||
.get_import_job(workspace_id, job_id)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(job.status, ImportJobStatus::Failed);
|
||||
assert!(job.finished_at.is_some());
|
||||
assert_eq!(job.error_text.as_deref(), Some(expected_error));
|
||||
|
||||
let mut source_ids = vec![
|
||||
job.preview_payload["source"]["source_id"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.to_owned(),
|
||||
];
|
||||
source_ids.extend(
|
||||
job.preview_payload["dependencies"]
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.map(|dependency| dependency["source_id"].as_str().unwrap().to_owned()),
|
||||
);
|
||||
for source_id in source_ids {
|
||||
let lifecycle: String = sqlx::query_scalar(
|
||||
"select lifecycle from artifact_sources where workspace_id = $1 and source_id = $2",
|
||||
)
|
||||
.bind(workspace_id.as_str())
|
||||
.bind(source_id)
|
||||
.fetch_one(registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(lifecycle, "detached");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user