This is an automated email from the ASF dual-hosted git repository. mgrigorov pushed a commit to branch avro-3974-compatibility-check-for-schema-refs in repository https://gitbox.apache.org/repos/asf/avro.git
commit c1167fd3ac673cb40ebd9ed717a5587926e9db43 Author: Martin Tzvetanov Grigorov <[email protected]> AuthorDate: Thu Apr 11 11:33:52 2024 +0300 AVRO-3974: [Rust] Support schema compatibility for Schema::Ref Signed-off-by: Martin Tzvetanov Grigorov <[email protected]> --- lang/rust/avro/src/schema_compatibility.rs | 59 ++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/lang/rust/avro/src/schema_compatibility.rs b/lang/rust/avro/src/schema_compatibility.rs index 669062461..1c7fa8194 100644 --- a/lang/rust/avro/src/schema_compatibility.rs +++ b/lang/rust/avro/src/schema_compatibility.rs @@ -26,6 +26,28 @@ use std::{ ptr, }; +fn match_ref_schemas( + writers_schema: &Schema, + readers_schema: &Schema, +) -> Result<(), CompatibilityError> { + match (readers_schema, writers_schema) { + (Schema::Ref { name: r_name }, Schema::Ref { name: w_name }) => { + if r_name == w_name { + Ok(()) + } else { + Err(CompatibilityError::NameMismatch { + writer_name: w_name.fullname(None), + reader_name: r_name.fullname(None), + }) + } + } + _ => Err(CompatibilityError::WrongType { + writer_schema_type: format!("{:#?}", writers_schema), + reader_schema_type: format!("{:#?}", readers_schema), + }), + } +} + pub struct SchemaCompatibility; struct Checker { @@ -80,6 +102,7 @@ impl Checker { } match r_type { + SchemaKind::Ref => match_ref_schemas(writers_schema, readers_schema), SchemaKind::Record => self.match_record_schemas(writers_schema, readers_schema), SchemaKind::Map => { if let Schema::Map(w_m) = writers_schema { @@ -431,6 +454,7 @@ impl SchemaCompatibility { SchemaKind::Duration => { return Ok(()); } + SchemaKind::Ref => return match_ref_schemas(writers_schema, readers_schema), _ => { return Err(CompatibilityError::Inconclusive(String::from( "readers_schema", @@ -1710,4 +1734,39 @@ mod tests { Ok(()) } + + #[test] + fn avro_3974_can_read_schema_references() -> TestResult { + let schema_strs = vec![ + r#"{ + "type": "record", + "name": "Child", + "namespace": "avro", + "fields": [ + { + "name": "val", + "type": "int" + } + ] + } + "#, + r#"{ + "type": "record", + "name": "Parent", + "namespace": "avro", + "fields": [ + { + "name": "child", + "type": "avro.Child" + } + ] + } + "#, + ]; + + let schemas = Schema::parse_list(&schema_strs).unwrap(); + SchemaCompatibility::can_read(&schemas[1], &schemas[1])?; + + Ok(()) + } }
