| 1 | use core::slice; |
| 2 | use std::iter::FlatMap; |
| 3 | |
| 4 | use serde_json::Value; |
| 5 | |
| 6 | use crate::{resource::InnerResourcePtr, segments::Segment, Error, Resolver, Segments}; |
| 7 | |
| 8 | type ObjectIter<'a> = FlatMap< |
| 9 | serde_json::map::Iter<'a>, |
| 10 | SubresourceIteratorInner<'a>, |
| 11 | fn((&'a std::string::String, &'a Value)) -> SubresourceIteratorInner<'a>, |
| 12 | >; |
| 13 | |
| 14 | /// A simple iterator that either wraps an iterator producing &Value or is empty. |
| 15 | /// NOTE: It is noticeably slower if `Object` is boxed. |
| 16 | #[allow(clippy::large_enum_variant)] |
| 17 | pub(crate) enum SubresourceIterator<'a> { |
| 18 | Object(ObjectIter<'a>), |
| 19 | Empty, |
| 20 | } |
| 21 | |
| 22 | impl<'a> Iterator for SubresourceIterator<'a> { |
| 23 | type Item = &'a Value; |
| 24 | fn next(&mut self) -> Option<Self::Item> { |
| 25 | match self { |
| 26 | SubresourceIterator::Object(iter) => iter.next(), |
| 27 | SubresourceIterator::Empty => None, |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | pub(crate) enum SubresourceIteratorInner<'a> { |
| 33 | Once(&'a Value), |
| 34 | Array(slice::Iter<'a, Value>), |
| 35 | Object(serde_json::map::Values<'a>), |
| 36 | FilteredObject(serde_json::map::Values<'a>), |
| 37 | Empty, |
| 38 | } |
| 39 | |
| 40 | impl<'a> Iterator for SubresourceIteratorInner<'a> { |
| 41 | type Item = &'a Value; |
| 42 | fn next(&mut self) -> Option<Self::Item> { |
| 43 | match self { |
| 44 | SubresourceIteratorInner::Once(_) => { |
| 45 | let SubresourceIteratorInner::Once(value) = |
| 46 | std::mem::replace(self, SubresourceIteratorInner::Empty) |
| 47 | else { |
| 48 | unreachable!() |
| 49 | }; |
| 50 | Some(value) |
| 51 | } |
| 52 | SubresourceIteratorInner::Array(iter) => iter.next(), |
| 53 | SubresourceIteratorInner::Object(iter) => iter.next(), |
| 54 | SubresourceIteratorInner::FilteredObject(iter) => { |
| 55 | for next in iter.by_ref() { |
| 56 | if !next.is_object() { |
| 57 | continue; |
| 58 | } |
| 59 | return Some(next); |
| 60 | } |
| 61 | None |
| 62 | } |
| 63 | SubresourceIteratorInner::Empty => None, |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | pub(crate) fn object_iter<'a>( |
| 69 | (key, value): (&'a String, &'a Value), |
| 70 | ) -> SubresourceIteratorInner<'a> { |
| 71 | match key.as_str() { |
| 72 | "additionalProperties" |
| 73 | | "contains" |
| 74 | | "contentSchema" |
| 75 | | "else" |
| 76 | | "if" |
| 77 | | "items" |
| 78 | | "not" |
| 79 | | "propertyNames" |
| 80 | | "then" |
| 81 | | "unevaluatedItems" |
| 82 | | "unevaluatedProperties" => SubresourceIteratorInner::Once(value), |
| 83 | "allOf" | "anyOf" | "oneOf" | "prefixItems" => { |
| 84 | if let Some(arr) = value.as_array() { |
| 85 | SubresourceIteratorInner::Array(arr.iter()) |
| 86 | } else { |
| 87 | SubresourceIteratorInner::Empty |
| 88 | } |
| 89 | } |
| 90 | "$defs" | "definitions" | "dependentSchemas" | "patternProperties" | "properties" => { |
| 91 | if let Some(obj) = value.as_object() { |
| 92 | SubresourceIteratorInner::Object(obj.values()) |
| 93 | } else { |
| 94 | SubresourceIteratorInner::Empty |
| 95 | } |
| 96 | } |
| 97 | _ => SubresourceIteratorInner::Empty, |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | pub(crate) fn maybe_in_subresource<'r>( |
| 102 | segments: &Segments, |
| 103 | resolver: &Resolver<'r>, |
| 104 | subresource: &InnerResourcePtr, |
| 105 | ) -> Result<Resolver<'r>, Error> { |
| 106 | const IN_VALUE: &[&str] = &[ |
| 107 | "additionalProperties", |
| 108 | "contains", |
| 109 | "contentSchema", |
| 110 | "else", |
| 111 | "if", |
| 112 | "items", |
| 113 | "not", |
| 114 | "propertyNames", |
| 115 | "then", |
| 116 | "unevaluatedItems", |
| 117 | "unevaluatedProperties", |
| 118 | ]; |
| 119 | const IN_CHILD: &[&str] = &[ |
| 120 | "allOf", |
| 121 | "anyOf", |
| 122 | "oneOf", |
| 123 | "prefixItems", |
| 124 | "$defs", |
| 125 | "definitions", |
| 126 | "dependentSchemas", |
| 127 | "patternProperties", |
| 128 | "properties", |
| 129 | ]; |
| 130 | |
| 131 | let mut iter = segments.iter(); |
| 132 | while let Some(segment) = iter.next() { |
| 133 | if let Segment::Key(key) = segment { |
| 134 | if !IN_VALUE.contains(&key.as_ref()) |
| 135 | && (!IN_CHILD.contains(&key.as_ref()) || iter.next().is_none()) |
| 136 | { |
| 137 | return Ok(resolver.clone()); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | resolver.in_subresource_inner(subresource) |
| 142 | } |
| 143 | |
| 144 | #[inline] |
| 145 | pub(crate) fn maybe_in_subresource_with_items_and_dependencies<'r>( |
| 146 | segments: &Segments, |
| 147 | resolver: &Resolver<'r>, |
| 148 | subresource: &InnerResourcePtr, |
| 149 | in_value: &[&str], |
| 150 | in_child: &[&str], |
| 151 | ) -> Result<Resolver<'r>, Error> { |
| 152 | let mut iter = segments.iter(); |
| 153 | while let Some(segment) = iter.next() { |
| 154 | if let Segment::Key(key) = segment { |
| 155 | if (*key == "items" || *key == "dependencies") && subresource.contents().is_object() { |
| 156 | return resolver.in_subresource_inner(subresource); |
| 157 | } |
| 158 | if !in_value.contains(&key.as_ref()) |
| 159 | && (!in_child.contains(&key.as_ref()) || iter.next().is_none()) |
| 160 | { |
| 161 | return Ok(resolver.clone()); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | resolver.in_subresource_inner(subresource) |
| 166 | } |
| 167 | |
| 168 | #[cfg(test)] |
| 169 | mod tests { |
| 170 | use crate::Draft; |
| 171 | |
| 172 | use super::{object_iter, SubresourceIterator}; |
| 173 | use ahash::HashSet; |
| 174 | use serde_json::{json, Value}; |
| 175 | use test_case::test_case; |
| 176 | |
| 177 | pub(crate) fn subresources_of(contents: &Value) -> SubresourceIterator<'_> { |
| 178 | match contents.as_object() { |
| 179 | Some(schema) => SubresourceIterator::Object(schema.iter().flat_map(object_iter)), |
| 180 | None => SubresourceIterator::Empty, |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | #[test_case(&json!(true), &[] ; "boolean schema")] |
| 185 | #[test_case(&json!(false), &[] ; "boolean schema false")] |
| 186 | #[test_case(&json!({}), &[] ; "empty object")] |
| 187 | #[test_case(&json!({"type": "string"}), &[] ; "no subresources")] |
| 188 | #[test_case( |
| 189 | &json!({"additionalProperties": {"type": "string"}}), |
| 190 | &[json!({"type": "string"})] ; |
| 191 | "in_value single" |
| 192 | )] |
| 193 | #[test_case( |
| 194 | &json!({"if": {"type": "string"}, "then": {"minimum": 0}}), |
| 195 | &[json!({"type": "string"}), json!({"minimum": 0})] ; |
| 196 | "in_value multiple" |
| 197 | )] |
| 198 | #[test_case( |
| 199 | &json!({"properties": {"foo": {"type": "string"}, "bar": {"type": "number"}}}), |
| 200 | &[json!({"type": "string"}), json!({"type": "number"})] ; |
| 201 | "in_subvalues" |
| 202 | )] |
| 203 | #[test_case( |
| 204 | &json!({"allOf": [{"type": "string"}, {"minLength": 1}]}), |
| 205 | &[json!({"type": "string"}), json!({"minLength": 1})] ; |
| 206 | "in_subarray" |
| 207 | )] |
| 208 | #[test_case( |
| 209 | &json!({ |
| 210 | "type": "object", |
| 211 | "properties": { |
| 212 | "foo": {"type": "string"}, |
| 213 | "bar": {"type": "number"} |
| 214 | }, |
| 215 | "additionalProperties": {"type": "boolean"}, |
| 216 | "allOf": [ |
| 217 | {"required": ["foo"]}, |
| 218 | {"required": ["bar"]} |
| 219 | ] |
| 220 | }), |
| 221 | &[ |
| 222 | json!({"type": "string"}), |
| 223 | json!({"type": "number"}), |
| 224 | json!({"type": "boolean"}), |
| 225 | json!({"required": ["foo"]}), |
| 226 | json!({"required": ["bar"]}) |
| 227 | ] ; |
| 228 | "complex schema" |
| 229 | )] |
| 230 | #[test_case( |
| 231 | &json!({ |
| 232 | "$defs": { |
| 233 | "positiveInteger": { |
| 234 | "type": "integer", |
| 235 | "exclusiveMinimum": 0 |
| 236 | } |
| 237 | }, |
| 238 | "properties": { |
| 239 | "count": { "$ref": "#/$defs/positiveInteger" } |
| 240 | } |
| 241 | }), |
| 242 | &[ |
| 243 | json!({"type": "integer", "exclusiveMinimum": 0}), |
| 244 | json!({"$ref": "#/$defs/positiveInteger"}) |
| 245 | ] ; |
| 246 | "with $defs" |
| 247 | )] |
| 248 | fn test_subresources_of(schema: &serde_json::Value, expected: &[serde_json::Value]) { |
| 249 | let subresources: HashSet<&serde_json::Value> = subresources_of(schema).collect(); |
| 250 | let expected_set: HashSet<&serde_json::Value> = expected.iter().collect(); |
| 251 | |
| 252 | assert_eq!( |
| 253 | subresources.len(), |
| 254 | expected.len(), |
| 255 | "Number of subresources doesn't match" |
| 256 | ); |
| 257 | assert_eq!( |
| 258 | subresources, expected_set, |
| 259 | "Subresources don't match expected values" |
| 260 | ); |
| 261 | } |
| 262 | |
| 263 | #[test] |
| 264 | fn test_all_keywords() { |
| 265 | let schema = json!({ |
| 266 | "additionalProperties": {"type": "string"}, |
| 267 | "contains": {"minimum": 0}, |
| 268 | "contentSchema": {"format": "email"}, |
| 269 | "else": {"maximum": 100}, |
| 270 | "if": {"type": "number"}, |
| 271 | "items": {"type": "array"}, |
| 272 | "not": {"type": "null"}, |
| 273 | "propertyNames": {"minLength": 1}, |
| 274 | "then": {"multipleOf": 2}, |
| 275 | "unevaluatedItems": {"type": "boolean"}, |
| 276 | "unevaluatedProperties": {"type": "integer"}, |
| 277 | "allOf": [{"type": "object"}, {"required": ["foo"]}], |
| 278 | "anyOf": [{"minimum": 0}, {"maximum": 100}], |
| 279 | "oneOf": [{"type": "string"}, {"type": "number"}], |
| 280 | "prefixItems": [{"type": "string"}, {"type": "number"}], |
| 281 | "$defs": { |
| 282 | "positiveInteger": {"type": "integer", "minimum": 1} |
| 283 | }, |
| 284 | "definitions": { |
| 285 | "negativeInteger": {"type": "integer", "maximum": -1} |
| 286 | }, |
| 287 | "dependentSchemas": { |
| 288 | "foo": {"required": ["bar"]} |
| 289 | }, |
| 290 | "patternProperties": { |
| 291 | "^S_": {"type": "string"}, |
| 292 | "^I_": {"type": "integer"} |
| 293 | }, |
| 294 | "properties": { |
| 295 | "prop1": {"type": "string"}, |
| 296 | "prop2": {"type": "number"} |
| 297 | } |
| 298 | }); |
| 299 | |
| 300 | let subresources: Vec<&serde_json::Value> = subresources_of(&schema).collect(); |
| 301 | assert_eq!(subresources.len(), 26); |
| 302 | |
| 303 | assert!(subresources.contains(&&json!({"type": "string"}))); |
| 304 | assert!(subresources.contains(&&json!({"minimum": 0}))); |
| 305 | assert!(subresources.contains(&&json!({"format": "email"}))); |
| 306 | assert!(subresources.contains(&&json!({"maximum": 100}))); |
| 307 | assert!(subresources.contains(&&json!({"type": "number"}))); |
| 308 | assert!(subresources.contains(&&json!({"type": "array"}))); |
| 309 | assert!(subresources.contains(&&json!({"type": "null"}))); |
| 310 | assert!(subresources.contains(&&json!({"minLength": 1}))); |
| 311 | assert!(subresources.contains(&&json!({"multipleOf": 2}))); |
| 312 | assert!(subresources.contains(&&json!({"type": "boolean"}))); |
| 313 | assert!(subresources.contains(&&json!({"type": "integer"}))); |
| 314 | assert!(subresources.contains(&&json!({"type": "object"}))); |
| 315 | assert!(subresources.contains(&&json!({"required": ["foo"]}))); |
| 316 | assert!(subresources.contains(&&json!({"minimum": 0}))); |
| 317 | assert!(subresources.contains(&&json!({"maximum": 100}))); |
| 318 | assert!(subresources.contains(&&json!({"type": "string"}))); |
| 319 | assert!(subresources.contains(&&json!({"type": "number"}))); |
| 320 | assert!(subresources.contains(&&json!({"type": "integer", "minimum": 1}))); |
| 321 | assert!(subresources.contains(&&json!({"type": "integer", "maximum": -1}))); |
| 322 | assert!(subresources.contains(&&json!({"required": ["bar"]}))); |
| 323 | assert!(subresources.contains(&&json!({"type": "string"}))); |
| 324 | assert!(subresources.contains(&&json!({"type": "integer"}))); |
| 325 | } |
| 326 | |
| 327 | #[test_case(Draft::Draft4)] |
| 328 | #[test_case(Draft::Draft6)] |
| 329 | #[test_case(Draft::Draft7)] |
| 330 | #[test_case(Draft::Draft201909)] |
| 331 | #[test_case(Draft::Draft202012)] |
| 332 | fn test_subresources_of_bool_schema(draft: Draft) { |
| 333 | let bool_schema = json!(true); |
| 334 | |
| 335 | assert!( |
| 336 | draft |
| 337 | .subresources_of(&bool_schema) |
| 338 | .collect::<Vec<_>>() |
| 339 | .is_empty(), |
| 340 | "Draft {draft:?} should return empty subresources for boolean schema", |
| 341 | ); |
| 342 | } |
| 343 | } |