This is an automated email from the ASF dual-hosted git repository.
mgrigorov pushed a commit to branch branch-1.11
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/branch-1.11 by this push:
new 005125ece AVRO-3974: [Rust] Support schema compatibility for
Schema::Ref (#2847)
005125ece is described below
commit 005125ece729c1262053dcb8fdbf28df10e1a272
Author: Martin Grigorov <[email protected]>
AuthorDate: Thu Apr 11 20:55:23 2024 +0300
AVRO-3974: [Rust] Support schema compatibility for Schema::Ref (#2847)
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
(cherry picked from commit cd3e2dfa2cbb7648abe5d7222ab7f6424da83c43)
---
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 d75de6b2b..5681dde10 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(())
+ }
}