Skip to content

Commit 0d4cd48

Browse files
committed
fix(breg): preserve ODRL profile coverage and interpretation
Signed-off-by: Jeremi Joslin <jeremi@joslin.fr>
1 parent 83447ed commit 0d4cd48

11 files changed

Lines changed: 360 additions & 12 deletions

File tree

‎crates/registry-breg/src/odrl_export.rs‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::GeneratedArtifact;
2020

2121
pub const COVERAGE_API_VERSION: &str = "id.registrystack.org/formats/breg/odrl-coverage/v1alpha1";
2222
pub const PROFILE_IRI: &str = "https://id.registrystack.org/profiles/breg-odrl/v1";
23-
pub const VOCAB: &str = "https://id.registrystack.org/vocab/breg-odrl#";
23+
pub const VOCAB: &str = "https://id.registrystack.org/vocab/breg-odrl/";
2424

2525
const ODRL_EQ: &str = "http://www.w3.org/ns/odrl/2/eq";
2626
const ODRL_HAS_PART: &str = "http://www.w3.org/ns/odrl/2/hasPart";
@@ -61,6 +61,10 @@ pub fn export_odrl(
6161
.access_profile_displays()
6262
.iter()
6363
.any(|profile| profile.id == selected)
64+
&& !registry
65+
.entities()
66+
.values()
67+
.any(|entity| entity.access_profiles.contains_key(selected))
6468
{
6569
return Err(refusal(
6670
"breg.odrl.unknown-access-profile",
@@ -150,6 +154,19 @@ pub fn export_odrl(
150154
)
151155
})?;
152156
collection.insert(format!("{VOCAB}partial"), Value::Bool(true));
157+
let members = collection
158+
.get_mut("@graph")
159+
.and_then(Value::as_array_mut)
160+
.ok_or_else(|| {
161+
refusal(
162+
"breg.odrl.render-failed",
163+
"odrl",
164+
"the ODRL renderer returned an invalid policy graph",
165+
)
166+
})?;
167+
for policy in members {
168+
policy[format!("{VOCAB}partial")] = Value::Bool(true);
169+
}
153170
collection.insert(
154171
DCTERMS_DESCRIPTION.to_owned(),
155172
Value::String(

‎crates/registry-breg/tests/odrl_export.rs‎

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// SPDX-License-Identifier: Apache-2.0
22

3+
use std::collections::BTreeSet;
4+
35
use registry_breg::compiler::{compile_project, module_digest, CompileProfile};
4-
use registry_breg::contract::{parse_module_yaml, parse_project_json, parse_project_yaml};
6+
use registry_breg::contract::{
7+
parse_module_json, parse_module_yaml, parse_project_json, parse_project_yaml,
8+
};
59
use registry_breg::odrl_export::{export_odrl, PROFILE_IRI, VOCAB};
610
use registry_breg::{CompiledRegistry, GeneratedArtifact};
711
use serde_json::{json, Value};
@@ -146,6 +150,21 @@ fn registered_coverage_example_matches_minimal_project() {
146150
);
147151
}
148152

153+
#[test]
154+
fn registered_policy_example_matches_authoring_display_fixture() {
155+
let project = parse_project_yaml(include_bytes!(
156+
"../../../products/breg/fixtures/authoring-display/registry.yaml"
157+
))
158+
.unwrap();
159+
let registry = compile_project(&project, &[], CompileProfile::Authoring).unwrap();
160+
let export = export_odrl(&registry, None).unwrap();
161+
assert_eq!(
162+
artifact(&export.artifacts, "generated/odrl/policies.jsonld").bytes,
163+
include_bytes!("../../../products/breg/examples/formats/odrl-policies.jsonld")
164+
);
165+
assert!(export.coverage["totals"]["exported"].as_u64().unwrap() > 0);
166+
}
167+
149168
#[test]
150169
fn supported_reads_preserve_profile_conditions_and_projection_fields() {
151170
let registry = compile_source(&supported_source());
@@ -212,6 +231,7 @@ fn supported_reads_preserve_profile_conditions_and_projection_fields() {
212231
assert_eq!(policies[format!("{VOCAB}partial")], true);
213232
assert!(policies["http://purl.org/dc/terms/description"].is_string());
214233
let policy = &policies["@graph"][0];
234+
assert_eq!(policy[format!("{VOCAB}partial")], true);
215235
assert_eq!(policy["odrl:profile"], json!([{"@id":PROFILE_IRI}]));
216236
let permissions = policy["odrl:permission"]
217237
.as_array()
@@ -455,3 +475,134 @@ fn exact_selection_of_known_action_only_profile_is_empty_and_valid() {
455475
);
456476
}
457477
}
478+
479+
#[test]
480+
fn exact_selection_accepts_profiles_from_modules_and_extensions() {
481+
let project = parse_project_json(
482+
&serde_json::to_vec(&json!({
483+
"apiVersion": "id.registrystack.org/formats/breg/project/v1alpha1",
484+
"kind": "BRegProject",
485+
"project": {"id":"module-profile", "version":"1", "defaultLanguage":"en",
486+
"canonicalBaseIri":"https://authoring.example.test"},
487+
"datasets": [{"id":"records"}],
488+
"recordTypes": [{
489+
"id":"case-file", "primaryDataset":"records", "route":"case-files",
490+
"mutationMode":"mutable",
491+
"fields":[{"id":"code", "type":"string", "maximumLength":32,
492+
"classification":"internal"}]
493+
}],
494+
"modules": [{"id":"core", "version":"1"}]
495+
}))
496+
.unwrap(),
497+
)
498+
.unwrap();
499+
let profile = |id| {
500+
json!({
501+
"id":id, "principalClaim":"sub", "operations":["get"],
502+
"readableFields":["code"], "requiredScopes":"unrestricted",
503+
"rowBoundaries":"unrestricted"
504+
})
505+
};
506+
let module = parse_module_json(
507+
&serde_json::to_vec(&json!({
508+
"apiVersion":"id.registrystack.org/formats/breg/module/v1alpha1",
509+
"kind":"BRegModule", "id":"core", "version":"1",
510+
"recordTypes":[{
511+
"id":"module-record", "primaryDataset":"records", "route":"module-records",
512+
"mutationMode":"mutable",
513+
"fields":[{"id":"code", "type":"string", "maximumLength":32,
514+
"classification":"internal"}],
515+
"accessProfiles":[profile("module-reader")]
516+
}],
517+
"extendRecordTypes":[{
518+
"recordType":"case-file", "accessProfiles":[profile("extension-reader")]
519+
}]
520+
}))
521+
.unwrap(),
522+
)
523+
.unwrap();
524+
let registry = compile_project(&project, &[module], CompileProfile::Authoring).unwrap();
525+
assert!(registry.access_profile_displays().is_empty());
526+
for (id, record_type) in [
527+
("module-reader", "module-record"),
528+
("extension-reader", "case-file"),
529+
] {
530+
let export = export_odrl(&registry, Some(id)).expect("compiled profile is selectable");
531+
assert_eq!(export.coverage["selectedAccessProfile"], id);
532+
assert_eq!(
533+
export.coverage["totals"],
534+
json!({"considered":1,"exported":1,"omitted":0})
535+
);
536+
assert_eq!(export.coverage["grants"][0]["recordType"], record_type);
537+
assert_eq!(export.coverage["grants"][0]["accessProfile"], id);
538+
}
539+
assert!(export_odrl(&registry, Some("module")).is_err());
540+
}
541+
542+
#[test]
543+
fn profile_declares_every_emitted_extension_and_adopts_read() {
544+
fn emitted_terms(value: &Value, terms: &mut BTreeSet<String>) {
545+
match value {
546+
Value::Object(object) => {
547+
for (key, value) in object {
548+
if key.starts_with(VOCAB) {
549+
terms.insert(key.clone());
550+
}
551+
emitted_terms(value, terms);
552+
}
553+
}
554+
Value::Array(values) => {
555+
for value in values {
556+
emitted_terms(value, terms);
557+
}
558+
}
559+
Value::String(value) if value.starts_with(VOCAB) || value == ODRL_READ => {
560+
terms.insert(value.clone());
561+
}
562+
_ => {}
563+
}
564+
}
565+
566+
let registry = compile_source(&supported_source());
567+
let export = export_odrl(&registry, None).unwrap();
568+
let policies = document(&export, "generated/odrl/policies.jsonld");
569+
let profile = document(&export, "generated/odrl/profile.jsonld");
570+
let graph = profile["@graph"].as_array().unwrap();
571+
let collection = graph
572+
.iter()
573+
.find(|node| node["@type"] == "skos:Collection")
574+
.expect("profile vocabulary is a machine-readable collection");
575+
let expand = |value: &Value| {
576+
let id = value.as_str().unwrap();
577+
id.strip_prefix("breg:").map_or_else(
578+
|| id.replace("odrl:", "http://www.w3.org/ns/odrl/2/"),
579+
|term| format!("{VOCAB}{term}"),
580+
)
581+
};
582+
let members = collection["skos:member"]
583+
.as_array()
584+
.unwrap()
585+
.iter()
586+
.map(|member| expand(&member["@id"]))
587+
.collect::<BTreeSet<_>>();
588+
let mut emitted = BTreeSet::new();
589+
emitted_terms(&policies, &mut emitted);
590+
assert_eq!(
591+
emitted.len(),
592+
14,
593+
"thirteen BReg terms and odrl:read are exercised"
594+
);
595+
assert_eq!(members, emitted);
596+
for term in graph
597+
.iter()
598+
.filter(|node| node["@type"] != "skos:Collection")
599+
{
600+
assert_eq!(term["rdfs:isDefinedBy"]["@id"], PROFILE_IRI);
601+
assert!(members.contains(&expand(&term["@id"])));
602+
}
603+
assert_eq!(
604+
graph.len(),
605+
14,
606+
"one collection and thirteen term definitions"
607+
);
608+
}

‎crates/registry-bregctl/src/lib.rs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8288,12 +8288,18 @@ fn generate_odrl(args: &GenerateArgs) -> Result<SuccessReport, FailureReport> {
82888288
let compiled = compile(&args.project, profile, "generate")?;
82898289
let export = registry_breg::odrl_export::export_odrl(&compiled, args.access_profile.as_deref())
82908290
.map_err(|diagnostic| {
8291-
source_failure(
8292-
"generate",
8293-
diagnostic,
8294-
DiagnosticArtifact::RegistryProject,
8295-
SuggestedAction::CorrectAuthoringSource,
8296-
)
8291+
let (artifact, action) = if diagnostic.code == "breg.odrl.unknown-access-profile" {
8292+
(
8293+
DiagnosticArtifact::CommandArguments,
8294+
SuggestedAction::CorrectCommandUsage,
8295+
)
8296+
} else {
8297+
(
8298+
DiagnosticArtifact::RegistryProject,
8299+
SuggestedAction::CorrectAuthoringSource,
8300+
)
8301+
};
8302+
source_failure("generate", diagnostic, artifact, action)
82978303
})?;
82988304
write_artifacts(&args.output, &export.artifacts).map_err(|diagnostic| {
82998305
source_failure(

‎crates/registry-bregctl/tests/odrl_cli.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ fn odrl_cli_unknown_profile_and_irrelevant_flags_refuse_before_writing() {
110110
assert_eq!(report["ok"], false);
111111
assert_eq!(report["command"], "generate");
112112
assert_eq!(report["diagnostics"][0]["code"], expected_code);
113+
assert_eq!(report["diagnostics"][0]["artifact"], "command-arguments");
114+
assert_eq!(
115+
report["diagnostics"][0]["suggestedAction"],
116+
"correct-command-usage"
117+
);
113118
assert!(result.stderr.is_empty(), "{result:?}");
114119
assert!(!output.exists());
115120
}

‎crates/registry-manifest-core/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ metadata model.
1919
- Pure renderers for catalog JSON, DCAT JSON-LD, BRegDCAT-AP JSON-LD, CPSV-AP JSON-LD, SHACL,
2020
JSON Schema Draft 2020-12, OGC API Records items, policies, and evidence
2121
offerings.
22+
- A pure ODRL 2.2 policy-collection renderer with explicit profiles, projected
23+
targets and constraints. Consumers own translation and profile semantics;
24+
rendering a policy does not enforce it or establish interoperability.
2225

2326
## Typical Use
2427

0 commit comments

Comments
 (0)