Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

fix(mapping): encode malformed percent signs in mapped IRIs

Rudy Celekli04dc6d643691
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6646495d..84cd2853 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,8 @@ All notable changes to Open Ontologies are documented here.
### Fixed
+- Encode literal percent characters in mapped IRI components while preserving valid percent escapes.
+
- Keep the last active ontology intact when parsing a replacement source, cache, or refresh fails; publish successful replacements in one graph transaction.
- Bound wildcard repository filtering to pattern/filename pairs so repeated stars cannot stall `onto_repo_list`.
- Sanitize quotes and ASCII control bytes in mapped IRI components so valid structured data does not fail RDF ingestion.
diff --git a/src/mapping.rs b/src/mapping.rs
index 8bd97ce4..82f7fba8 100644
--- a/src/mapping.rs
+++ b/src/mapping.rs
@@ -127,15 +127,29 @@ impl MappingConfig {
}
}
-/// Replace spaces and special characters that are invalid in IRIs with underscores.
+/// Replace invalid IRI characters and encode literal percent signs.
+/// Existing percent escapes remain unchanged.
fn sanitize_iri(s: &str) -> String {
- s.chars()
- .map(|c| match c {
- ' ' | '"' | '<' | '>' | '{' | '}' | '|' | '\\' | '^' | '`' | '?' => '_',
- c if c.is_ascii_control() => '_',
- _ => c,
- })
- .collect()
+ let mut out = String::with_capacity(s.len());
+ let mut chars = s.chars();
+ while let Some(c) = chars.next() {
+ match c {
+ '%' => {
+ let mut following = chars.clone();
+ let encoded = matches!(following.next(), Some(c) if c.is_ascii_hexdigit())
+ && matches!(following.next(), Some(c) if c.is_ascii_hexdigit());
+ if encoded {
+ out.push('%');
+ } else {
+ out.push_str("%25");
+ }
+ }
+ ' ' | '"' | '<' | '>' | '{' | '}' | '|' | '\\' | '^' | '`' | '?' => out.push('_'),
+ c if c.is_ascii_control() => out.push('_'),
+ _ => out.push(c),
+ }
+ }
+ out
}
/// Escape characters that are special in N-Triples string literals.
diff --git a/tests/mapping_percent_iri_test.rs b/tests/mapping_percent_iri_test.rs
new file mode 100644
index 00000000..b1196258
--- /dev/null
+++ b/tests/mapping_percent_iri_test.rs
@@ -0,0 +1,55 @@
+//! Invalid literal percent characters must not produce invalid mapped IRIs.
+use open_ontologies::graph::GraphStore;
+use open_ontologies::mapping::{FieldMapping, MappingConfig};
+use std::collections::HashMap;
+
+const BASE: &str = "https://example.org/";
+
+#[test]
+fn percent_components_parse_in_subjects_predicates_and_lookup_objects() {
+ for (value, expected) in [
+ ("50%", "50%25"),
+ ("%", "%25"),
+ ("%A", "%25A"),
+ ("%G1", "%25G1"),
+ ("%é", "%25é"),
+ ("%%20", "%25%20"),
+ ("name%20space", "name%20space"),
+ ("path%2fpart", "path%2fpart"),
+ ("literal%25", "literal%25"),
+ ("日本語", "日本語"),
+ ] {
+ let mut mapping = MappingConfig::from_headers(
+ &["id".into(), value.into()],
+ BASE,
+ &format!("{BASE}Record"),
+ );
+ mapping.mappings.push(FieldMapping {
+ field: "lookup".into(),
+ predicate: format!("{BASE}lookup"),
+ datatype: None,
+ class: None,
+ lookup: true,
+ });
+ let row = HashMap::from([
+ ("id".into(), value.into()),
+ (value.into(), "plain literal % stays %".into()),
+ ("lookup".into(), value.into()),
+ ]);
+ let triples = mapping.rows_to_ntriples(&[row]);
+ assert!(
+ triples.starts_with(&format!("<{BASE}{expected}>")),
+ "{triples}"
+ );
+ assert!(
+ triples.contains(&format!("<{BASE}ont#{expected}>")),
+ "{triples}"
+ );
+ assert!(
+ triples.contains(&format!("<{BASE}lookup> <{BASE}{expected}>")),
+ "{triples}"
+ );
+ assert!(triples.contains("plain literal % stays %"), "{triples}");
+ GraphStore::new().load_ntriples(&triples).unwrap();
+ }
+}