From af1009c64a6dace6ba256dcaddfdefda91d52584 Mon Sep 17 00:00:00 2001 From: Joseph Shearer Date: Fri, 28 Jul 2023 15:27:05 -0400 Subject: [PATCH] fix: Limit field counts for objects inside arrays --- crates/doc/src/inference.rs | 69 +++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/crates/doc/src/inference.rs b/crates/doc/src/inference.rs index 28ba752c33..e6519ce3e4 100644 --- a/crates/doc/src/inference.rs +++ b/crates/doc/src/inference.rs @@ -1706,6 +1706,12 @@ impl Shape { pub fn enforce_field_count_limits(&mut self, loc: Location) { // TODO: If we implement inference/widening of array tuple shapes // then we'll need to also check that those aren't excessively large. + if self.type_.overlaps(types::ARRAY) { + if let Some(array_shape) = self.array.additional.as_mut() { + array_shape.enforce_field_count_limits(loc.push_item(0)); + } + } + if !self.type_.overlaps(types::OBJECT) { return; } @@ -1832,6 +1838,69 @@ mod test { ); } + #[test] + fn test_field_count_limits_inside_array() { + widening_snapshot_helper( + None, + r#" + type: array + minItems: 1 + maxItems: 1 + items: + type: object + additionalProperties: false + required: [key] + properties: + key: + type: string + minLength: 4 + maxLength: 4 + "#, + &[r#"[{"key": "test"}]"#], + true, + ); + let dynamic_array_objects = (0..800) + .map(|id| { + ser::to_string(&json!([{ + format!("key-{id}"): "test" + }])) + .unwrap() + }) + .collect_vec(); + + widening_snapshot_helper( + Some( + r#" + type: array + minItems: 1 + maxItems: 1 + items: + type: object + additionalProperties: false + required: [key] + properties: + key: + type: string + minLength: 4 + maxLength: 4 + "#, + ), + r#" + type: array + minItems: 1 + maxItems: 1 + items: + type: object + additionalProperties: + type: string + minLength: 4 + maxLength: 4 + "#, + &dynamic_array_objects, + true, + ); + } + #[test] fn test_field_count_limits_noop() { let dynamic_keys = (0..1)