| 1 | use std::{ |
| 2 | hash::Hash, |
| 3 | sync::atomic::{AtomicPtr, Ordering}, |
| 4 | }; |
| 5 | |
| 6 | use serde_json::Value; |
| 7 | |
| 8 | mod keys; |
| 9 | |
| 10 | use crate::{resource::InnerResourcePtr, Draft, Error, Resolved, Resolver}; |
| 11 | pub(crate) use keys::{AnchorKey, AnchorKeyRef}; |
| 12 | |
| 13 | #[derive(Debug)] |
| 14 | pub(crate) struct AnchorName { |
| 15 | ptr: AtomicPtr<u8>, |
| 16 | len: usize, |
| 17 | } |
| 18 | |
| 19 | impl AnchorName { |
| 20 | fn new(s: &str) -> Self { |
| 21 | Self { |
| 22 | ptr: AtomicPtr::new(s.as_ptr().cast_mut()), |
| 23 | len: s.len(), |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | #[allow(unsafe_code)] |
| 28 | fn as_str(&self) -> &str { |
| 29 | // SAFETY: The pointer is valid as long as the registry exists |
| 30 | unsafe { |
| 31 | std::str::from_utf8_unchecked(std::slice::from_raw_parts( |
| 32 | self.ptr.load(Ordering::Relaxed), |
| 33 | self.len, |
| 34 | )) |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl Clone for AnchorName { |
| 40 | fn clone(&self) -> Self { |
| 41 | Self { |
| 42 | ptr: AtomicPtr::new(self.ptr.load(Ordering::Relaxed)), |
| 43 | len: self.len, |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl Hash for AnchorName { |
| 49 | fn hash<H: std::hash::Hasher>(&self, state: &mut H) { |
| 50 | self.as_str().hash(state); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl PartialEq for AnchorName { |
| 55 | fn eq(&self, other: &Self) -> bool { |
| 56 | self.as_str() == other.as_str() |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl Eq for AnchorName {} |
| 61 | |
| 62 | /// An anchor within a resource. |
| 63 | #[derive(Debug, Clone)] |
| 64 | pub(crate) enum Anchor { |
| 65 | Default { |
| 66 | name: AnchorName, |
| 67 | resource: InnerResourcePtr, |
| 68 | }, |
| 69 | Dynamic { |
| 70 | name: AnchorName, |
| 71 | resource: InnerResourcePtr, |
| 72 | }, |
| 73 | } |
| 74 | |
| 75 | impl Anchor { |
| 76 | /// Anchor's name. |
| 77 | pub(crate) fn name(&self) -> AnchorName { |
| 78 | match self { |
| 79 | Anchor::Default { name, .. } | Anchor::Dynamic { name, .. } => name.clone(), |
| 80 | } |
| 81 | } |
| 82 | /// Get the resource for this anchor. |
| 83 | pub(crate) fn resolve<'r>(&'r self, resolver: Resolver<'r>) -> Result<Resolved<'r>, Error> { |
| 84 | match self { |
| 85 | Anchor::Default { resource, .. } => Ok(Resolved::new( |
| 86 | resource.contents(), |
| 87 | resolver, |
| 88 | resource.draft(), |
| 89 | )), |
| 90 | Anchor::Dynamic { name, resource } => { |
| 91 | let mut last = resource; |
| 92 | for uri in &resolver.dynamic_scope() { |
| 93 | match resolver.registry.anchor(uri, name.as_str()) { |
| 94 | Ok(anchor) => { |
| 95 | if let Anchor::Dynamic { resource, .. } = anchor { |
| 96 | last = resource; |
| 97 | } |
| 98 | } |
| 99 | Err(Error::NoSuchAnchor { .. }) => {} |
| 100 | Err(err) => return Err(err), |
| 101 | } |
| 102 | } |
| 103 | Ok(Resolved::new( |
| 104 | last.contents(), |
| 105 | resolver.in_subresource_inner(last)?, |
| 106 | last.draft(), |
| 107 | )) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | pub(crate) enum AnchorIter { |
| 114 | Empty, |
| 115 | One(Anchor), |
| 116 | Two(Anchor, Anchor), |
| 117 | } |
| 118 | |
| 119 | impl Iterator for AnchorIter { |
| 120 | type Item = Anchor; |
| 121 | |
| 122 | fn next(&mut self) -> Option<Self::Item> { |
| 123 | match std::mem::replace(self, AnchorIter::Empty) { |
| 124 | AnchorIter::Empty => None, |
| 125 | AnchorIter::One(anchor) => Some(anchor), |
| 126 | AnchorIter::Two(first, second) => { |
| 127 | *self = AnchorIter::One(second); |
| 128 | Some(first) |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | pub(crate) fn anchor(draft: Draft, contents: &Value) -> AnchorIter { |
| 135 | let Some(schema) = contents.as_object() else { |
| 136 | return AnchorIter::Empty; |
| 137 | }; |
| 138 | |
| 139 | // First check for top-level anchors |
| 140 | let default_anchor = |
| 141 | schema |
| 142 | .get("$anchor") |
| 143 | .and_then(Value::as_str) |
| 144 | .map(|name| Anchor::Default { |
| 145 | name: AnchorName::new(name), |
| 146 | resource: InnerResourcePtr::new(contents, draft), |
| 147 | }); |
| 148 | |
| 149 | let dynamic_anchor = schema |
| 150 | .get("$dynamicAnchor") |
| 151 | .and_then(Value::as_str) |
| 152 | .map(|name| Anchor::Dynamic { |
| 153 | name: AnchorName::new(name), |
| 154 | resource: InnerResourcePtr::new(contents, draft), |
| 155 | }); |
| 156 | |
| 157 | match (default_anchor, dynamic_anchor) { |
| 158 | (Some(default), Some(dynamic)) => AnchorIter::Two(default, dynamic), |
| 159 | (Some(default), None) => AnchorIter::One(default), |
| 160 | (None, Some(dynamic)) => AnchorIter::One(dynamic), |
| 161 | (None, None) => AnchorIter::Empty, |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | pub(crate) fn anchor_2019(draft: Draft, contents: &Value) -> AnchorIter { |
| 166 | match contents |
| 167 | .as_object() |
| 168 | .and_then(|schema| schema.get("$anchor")) |
| 169 | .and_then(Value::as_str) |
| 170 | { |
| 171 | Some(name) => AnchorIter::One(Anchor::Default { |
| 172 | name: AnchorName::new(name), |
| 173 | resource: InnerResourcePtr::new(contents, draft), |
| 174 | }), |
| 175 | None => AnchorIter::Empty, |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | pub(crate) fn legacy_anchor_in_dollar_id(draft: Draft, contents: &Value) -> AnchorIter { |
| 180 | match contents |
| 181 | .as_object() |
| 182 | .and_then(|schema| schema.get("$id")) |
| 183 | .and_then(Value::as_str) |
| 184 | .and_then(|id| id.strip_prefix('#')) |
| 185 | { |
| 186 | Some(id) => AnchorIter::One(Anchor::Default { |
| 187 | name: AnchorName::new(id), |
| 188 | resource: InnerResourcePtr::new(contents, draft), |
| 189 | }), |
| 190 | None => AnchorIter::Empty, |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | pub(crate) fn legacy_anchor_in_id(draft: Draft, contents: &Value) -> AnchorIter { |
| 195 | match contents |
| 196 | .as_object() |
| 197 | .and_then(|schema| schema.get("id")) |
| 198 | .and_then(Value::as_str) |
| 199 | .and_then(|id| id.strip_prefix('#')) |
| 200 | { |
| 201 | Some(id) => AnchorIter::One(Anchor::Default { |
| 202 | name: AnchorName::new(id), |
| 203 | resource: InnerResourcePtr::new(contents, draft), |
| 204 | }), |
| 205 | None => AnchorIter::Empty, |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | #[cfg(test)] |
| 210 | mod tests { |
| 211 | use crate::{Draft, Registry}; |
| 212 | use serde_json::json; |
| 213 | |
| 214 | #[test] |
| 215 | fn test_lookup_trivial_dynamic_ref() { |
| 216 | let one = Draft::Draft202012.create_resource(json!({"$dynamicAnchor": "foo"})); |
| 217 | let registry = |
| 218 | Registry::try_new("http://example.com", one.clone()).expect("Invalid resources"); |
| 219 | let resolver = registry |
| 220 | .try_resolver("http://example.com") |
| 221 | .expect("Invalid base URI"); |
| 222 | let resolved = resolver.lookup("#foo").expect("Lookup failed"); |
| 223 | assert_eq!(resolved.contents(), one.contents()); |
| 224 | } |
| 225 | |
| 226 | #[test] |
| 227 | fn test_multiple_lookup_trivial_dynamic_ref() { |
| 228 | let true_resource = Draft::Draft202012.create_resource(json!(true)); |
| 229 | let root = Draft::Draft202012.create_resource(json!({ |
| 230 | "$id": "http://example.com", |
| 231 | "$dynamicAnchor": "fooAnchor", |
| 232 | "$defs": { |
| 233 | "foo": { |
| 234 | "$id": "foo", |
| 235 | "$dynamicAnchor": "fooAnchor", |
| 236 | "$defs": { |
| 237 | "bar": true, |
| 238 | "baz": { |
| 239 | "$dynamicAnchor": "fooAnchor", |
| 240 | }, |
| 241 | }, |
| 242 | }, |
| 243 | }, |
| 244 | })); |
| 245 | |
| 246 | let registry = Registry::try_from_resources([ |
| 247 | ("http://example.com".to_string(), root.clone()), |
| 248 | ("http://example.com/foo/".to_string(), true_resource), |
| 249 | ("http://example.com/foo/bar".to_string(), root.clone()), |
| 250 | ]) |
| 251 | .expect("Invalid resources"); |
| 252 | let resolver = registry |
| 253 | .try_resolver("http://example.com") |
| 254 | .expect("Invalid base URI"); |
| 255 | |
| 256 | let first = resolver.lookup("").expect("Lookup failed"); |
| 257 | let second = first.resolver().lookup("foo/").expect("Lookup failed"); |
| 258 | let third = second.resolver().lookup("bar").expect("Lookup failed"); |
| 259 | let fourth = third |
| 260 | .resolver() |
| 261 | .lookup("#fooAnchor") |
| 262 | .expect("Lookup failed"); |
| 263 | assert_eq!(fourth.contents(), root.contents()); |
| 264 | assert_eq!(format!("{:?}", fourth.resolver()), "Resolver { base_uri: \"http://example.com\", scopes: \"[http://example.com/foo/, http://example.com, http://example.com]\" }"); |
| 265 | } |
| 266 | |
| 267 | #[test] |
| 268 | fn test_multiple_lookup_dynamic_ref_to_nondynamic_ref() { |
| 269 | let one = Draft::Draft202012.create_resource(json!({"$anchor": "fooAnchor"})); |
| 270 | let two = Draft::Draft202012.create_resource(json!({ |
| 271 | "$id": "http://example.com", |
| 272 | "$dynamicAnchor": "fooAnchor", |
| 273 | "$defs": { |
| 274 | "foo": { |
| 275 | "$id": "foo", |
| 276 | "$dynamicAnchor": "fooAnchor", |
| 277 | "$defs": { |
| 278 | "bar": true, |
| 279 | "baz": { |
| 280 | "$dynamicAnchor": "fooAnchor", |
| 281 | }, |
| 282 | }, |
| 283 | }, |
| 284 | }, |
| 285 | })); |
| 286 | |
| 287 | let registry = Registry::try_from_resources([ |
| 288 | ("http://example.com".to_string(), two.clone()), |
| 289 | ("http://example.com/foo/".to_string(), one), |
| 290 | ("http://example.com/foo/bar".to_string(), two.clone()), |
| 291 | ]) |
| 292 | .expect("Invalid resources"); |
| 293 | let resolver = registry |
| 294 | .try_resolver("http://example.com") |
| 295 | .expect("Invalid base URI"); |
| 296 | |
| 297 | let first = resolver.lookup("").expect("Lookup failed"); |
| 298 | let second = first.resolver().lookup("foo/").expect("Lookup failed"); |
| 299 | let third = second.resolver().lookup("bar").expect("Lookup failed"); |
| 300 | let fourth = third |
| 301 | .resolver() |
| 302 | .lookup("#fooAnchor") |
| 303 | .expect("Lookup failed"); |
| 304 | assert_eq!(fourth.contents(), two.contents()); |
| 305 | } |
| 306 | |
| 307 | #[test] |
| 308 | fn test_unknown_anchor() { |
| 309 | let schema = Draft::Draft202012.create_resource(json!({ |
| 310 | "$defs": { |
| 311 | "foo": { "$anchor": "knownAnchor" } |
| 312 | } |
| 313 | })); |
| 314 | let registry = Registry::try_new("http://example.com", schema).expect("Invalid resources"); |
| 315 | let resolver = registry |
| 316 | .try_resolver("http://example.com") |
| 317 | .expect("Invalid base URI"); |
| 318 | |
| 319 | let result = resolver.lookup("#unknownAnchor"); |
| 320 | assert_eq!( |
| 321 | result.unwrap_err().to_string(), |
| 322 | "Anchor 'unknownAnchor' does not exist" |
| 323 | ); |
| 324 | } |
| 325 | |
| 326 | #[test] |
| 327 | fn test_invalid_anchor_with_slash() { |
| 328 | let schema = Draft::Draft202012.create_resource(json!({ |
| 329 | "$defs": { |
| 330 | "foo": { "$anchor": "knownAnchor" } |
| 331 | } |
| 332 | })); |
| 333 | let registry = Registry::try_new("http://example.com", schema).expect("Invalid resources"); |
| 334 | let resolver = registry |
| 335 | .try_resolver("http://example.com") |
| 336 | .expect("Invalid base URI"); |
| 337 | |
| 338 | let result = resolver.lookup("#invalid/anchor"); |
| 339 | assert_eq!( |
| 340 | result.unwrap_err().to_string(), |
| 341 | "Anchor 'invalid/anchor' is invalid" |
| 342 | ); |
| 343 | } |
| 344 | |
| 345 | #[test] |
| 346 | fn test_lookup_trivial_recursive_ref() { |
| 347 | let one = Draft::Draft201909.create_resource(json!({"$recursiveAnchor": true})); |
| 348 | let registry = |
| 349 | Registry::try_new("http://example.com", one.clone()).expect("Invalid resources"); |
| 350 | let resolver = registry |
| 351 | .try_resolver("http://example.com") |
| 352 | .expect("Invalid base URI"); |
| 353 | let first = resolver.lookup("").expect("Lookup failed"); |
| 354 | let resolved = first |
| 355 | .resolver() |
| 356 | .lookup_recursive_ref() |
| 357 | .expect("Lookup failed"); |
| 358 | assert_eq!(resolved.contents(), one.contents()); |
| 359 | } |
| 360 | |
| 361 | #[test] |
| 362 | fn test_lookup_recursive_ref_to_bool() { |
| 363 | let true_resource = Draft::Draft201909.create_resource(json!(true)); |
| 364 | let registry = Registry::try_new("http://example.com", true_resource.clone()) |
| 365 | .expect("Invalid resources"); |
| 366 | let resolver = registry |
| 367 | .try_resolver("http://example.com") |
| 368 | .expect("Invalid base URI"); |
| 369 | let resolved = resolver.lookup_recursive_ref().expect("Lookup failed"); |
| 370 | assert_eq!(resolved.contents(), true_resource.contents()); |
| 371 | } |
| 372 | |
| 373 | #[test] |
| 374 | fn test_multiple_lookup_recursive_ref_to_bool() { |
| 375 | let true_resource = Draft::Draft201909.create_resource(json!(true)); |
| 376 | let root = Draft::Draft201909.create_resource(json!({ |
| 377 | "$id": "http://example.com", |
| 378 | "$recursiveAnchor": true, |
| 379 | "$defs": { |
| 380 | "foo": { |
| 381 | "$id": "foo", |
| 382 | "$recursiveAnchor": true, |
| 383 | "$defs": { |
| 384 | "bar": true, |
| 385 | "baz": { |
| 386 | "$recursiveAnchor": true, |
| 387 | "$anchor": "fooAnchor", |
| 388 | }, |
| 389 | }, |
| 390 | }, |
| 391 | }, |
| 392 | })); |
| 393 | |
| 394 | let registry = Registry::try_from_resources(vec![ |
| 395 | ("http://example.com".to_string(), root.clone()), |
| 396 | ("http://example.com/foo/".to_string(), true_resource), |
| 397 | ("http://example.com/foo/bar".to_string(), root.clone()), |
| 398 | ]) |
| 399 | .expect("Invalid resources"); |
| 400 | |
| 401 | let resolver = registry |
| 402 | .try_resolver("http://example.com") |
| 403 | .expect("Invalid base URI"); |
| 404 | let first = resolver.lookup("").expect("Lookup failed"); |
| 405 | let second = first.resolver().lookup("foo/").expect("Lookup failed"); |
| 406 | let third = second.resolver().lookup("bar").expect("Lookup failed"); |
| 407 | let fourth = third |
| 408 | .resolver() |
| 409 | .lookup_recursive_ref() |
| 410 | .expect("Lookup failed"); |
| 411 | assert_eq!(fourth.contents(), root.contents()); |
| 412 | } |
| 413 | |
| 414 | #[test] |
| 415 | fn test_multiple_lookup_recursive_ref_with_nonrecursive_ref() { |
| 416 | let one = Draft::Draft201909.create_resource(json!({"$recursiveAnchor": true})); |
| 417 | let two = Draft::Draft201909.create_resource(json!({ |
| 418 | "$id": "http://example.com", |
| 419 | "$recursiveAnchor": true, |
| 420 | "$defs": { |
| 421 | "foo": { |
| 422 | "$id": "foo", |
| 423 | "$recursiveAnchor": true, |
| 424 | "$defs": { |
| 425 | "bar": true, |
| 426 | "baz": { |
| 427 | "$recursiveAnchor": true, |
| 428 | "$anchor": "fooAnchor", |
| 429 | }, |
| 430 | }, |
| 431 | }, |
| 432 | }, |
| 433 | })); |
| 434 | let three = Draft::Draft201909.create_resource(json!({"$recursiveAnchor": false})); |
| 435 | |
| 436 | let registry = Registry::try_from_resources(vec![ |
| 437 | ("http://example.com".to_string(), three), |
| 438 | ("http://example.com/foo/".to_string(), two.clone()), |
| 439 | ("http://example.com/foo/bar".to_string(), one), |
| 440 | ]) |
| 441 | .expect("Invalid resources"); |
| 442 | |
| 443 | let resolver = registry |
| 444 | .try_resolver("http://example.com") |
| 445 | .expect("Invalid base URI"); |
| 446 | let first = resolver.lookup("").expect("Lookup failed"); |
| 447 | let second = first.resolver().lookup("foo/").expect("Lookup failed"); |
| 448 | let third = second.resolver().lookup("bar").expect("Lookup failed"); |
| 449 | let fourth = third |
| 450 | .resolver() |
| 451 | .lookup_recursive_ref() |
| 452 | .expect("Lookup failed"); |
| 453 | assert_eq!(fourth.contents(), two.contents()); |
| 454 | } |
| 455 | } |