From 999d9b30682977175fc7361416f8d69f57982ba1 Mon Sep 17 00:00:00 2001 From: trinity-1686a Date: Tue, 13 Aug 2024 17:01:15 +0200 Subject: [PATCH 1/2] make find_field_with_default return json fields without path --- src/schema/schema.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/schema/schema.rs b/src/schema/schema.rs index a91fb67137..06038f89a9 100644 --- a/src/schema/schema.rs +++ b/src/schema/schema.rs @@ -363,7 +363,7 @@ impl Schema { .or(default_field_opt.map(|field| (field, full_path)))?; let field_entry = self.get_field_entry(field); let is_json = field_entry.field_type().value_type() == Type::Json; - if is_json == json_path.is_empty() { + if !is_json && !json_path.is_empty() { return None; } Some((field, json_path)) From 8dd06691d23df341e073086d3631da6166a8c975 Mon Sep 17 00:00:00 2001 From: trinity-1686a Date: Mon, 19 Aug 2024 15:18:57 +0200 Subject: [PATCH 2/2] add tests for find_field_with_default --- src/schema/schema.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/schema/schema.rs b/src/schema/schema.rs index 06038f89a9..c1d22c0baa 100644 --- a/src/schema/schema.rs +++ b/src/schema/schema.rs @@ -960,4 +960,64 @@ mod tests { assert_eq!(schema.find_field("thiswouldbeareallylongfieldname"), None); assert_eq!(schema.find_field("baz.bar.foo"), None); } + + #[test] + fn test_find_field_with_default() { + let mut schema_builder = Schema::builder(); + schema_builder.add_json_field("_default", JsonObjectOptions::default()); + let default = Field::from_field_id(0); + schema_builder.add_json_field("foo", STRING); + let foo = Field::from_field_id(1); + schema_builder.add_text_field("foo.bar", STRING); + let foo_bar = Field::from_field_id(2); + schema_builder.add_text_field("bar", STRING); + let bar = Field::from_field_id(3); + schema_builder.add_json_field("baz", JsonObjectOptions::default()); + let baz = Field::from_field_id(4); + let schema = schema_builder.build(); + + assert_eq!(schema.find_field_with_default("foo", None), Some((foo, ""))); + assert_eq!( + schema.find_field_with_default("foo.bar", None), + Some((foo_bar, "")) + ); + assert_eq!(schema.find_field_with_default("bar", None), Some((bar, ""))); + assert_eq!(schema.find_field_with_default("bar.baz", None), None); + assert_eq!(schema.find_field_with_default("baz", None), Some((baz, ""))); + assert_eq!( + schema.find_field_with_default("baz.foobar", None), + Some((baz, "foobar")) + ); + assert_eq!(schema.find_field_with_default("foobar", None), None); + + assert_eq!( + schema.find_field_with_default("foo", Some(default)), + Some((foo, "")) + ); + assert_eq!( + schema.find_field_with_default("foo.bar", Some(default)), + Some((foo_bar, "")) + ); + assert_eq!( + schema.find_field_with_default("bar", Some(default)), + Some((bar, "")) + ); + // still None, we are under an existing field + assert_eq!( + schema.find_field_with_default("bar.baz", Some(default)), + None + ); + assert_eq!( + schema.find_field_with_default("baz", Some(default)), + Some((baz, "")) + ); + assert_eq!( + schema.find_field_with_default("baz.foobar", Some(default)), + Some((baz, "foobar")) + ); + assert_eq!( + schema.find_field_with_default("foobar", Some(default)), + Some((default, "foobar")) + ); + } }