You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2022/08/26 11:40:58 UTC

[avro] branch branch-1.11 updated: AVRO-3621: [Rust] do not log each error while validating against union variants (#1839)

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 25a83f547 AVRO-3621: [Rust] do not log each error while validating against union variants (#1839)
25a83f547 is described below

commit 25a83f547af675edf79aad2620281fdb8bbd12bc
Author: Shaeq Ahmed <sh...@gmail.com>
AuthorDate: Fri Aug 26 15:40:04 2022 +0400

    AVRO-3621: [Rust] do not log each error while validating against union variants (#1839)
    
    AVRO-3621: do not log each error while validating against union variants
    
    AVRO-3621: Improve the panic message for the new assert_not_logged() helper method
    
    Signed-off-by: Martin Tzvetanov Grigorov <mg...@apache.org>
    Co-authored-by: Martin Tzvetanov Grigorov <mg...@apache.org>
    (cherry picked from commit ae31eb12a06b5387f56261283344fdbaaeca2f02)
---
 lang/rust/avro/src/schema.rs             | 9 +++++----
 lang/rust/avro/src/types.rs              | 5 ++++-
 lang/rust/avro_test_helper/src/logger.rs | 9 +++++++++
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/lang/rust/avro/src/schema.rs b/lang/rust/avro/src/schema.rs
index ec26a8bf9..26868524a 100644
--- a/lang/rust/avro/src/schema.rs
+++ b/lang/rust/avro/src/schema.rs
@@ -692,10 +692,11 @@ impl UnionSchema {
             Some((i, &self.schemas[i]))
         } else {
             // slow path (required for matching logical or named types)
-            self.schemas
-                .iter()
-                .enumerate()
-                .find(|(_, schema)| value.validate(schema))
+            self.schemas.iter().enumerate().find(|(_, schema)| {
+                let rs =
+                    ResolvedSchema::try_from(*schema).expect("Schema didn't successfully parse");
+                value.validate_internal(schema, rs.get_names()).is_none()
+            })
         }
     }
 }
diff --git a/lang/rust/avro/src/types.rs b/lang/rust/avro/src/types.rs
index 7e700b4ea..72a420889 100644
--- a/lang/rust/avro/src/types.rs
+++ b/lang/rust/avro/src/types.rs
@@ -954,7 +954,7 @@ mod tests {
         schema::{Name, RecordField, RecordFieldOrder, Schema, UnionSchema},
         types::Value,
     };
-    use apache_avro_test_helper::logger::assert_logged;
+    use apache_avro_test_helper::logger::{assert_logged, assert_not_logged};
     use pretty_assertions::assert_eq;
     use uuid::Uuid;
 
@@ -1294,6 +1294,9 @@ mod tests {
         assert_logged(
             r#"Invalid value: Record([("a", Long(42)), ("c", String("foo"))]) for schema: Record { name: Name { name: "some_record", namespace: None }, aliases: None, doc: None, fields: [RecordField { name: "a", doc: None, default: None, schema: Long, order: Ascending, position: 0, custom_attributes: {} }, RecordField { name: "b", doc: None, default: None, schema: String, order: Ascending, position: 1, custom_attributes: {} }, RecordField { name: "c", doc: None, default: Some(Null), sche [...]
         );
+        assert_not_logged(
+            r#"Invalid value: String("foo") for schema: Int. Reason: Unsupported value-schema combination"#,
+        );
 
         let value = Value::Record(vec![
             ("a".to_string(), Value::Long(42i64)),
diff --git a/lang/rust/avro_test_helper/src/logger.rs b/lang/rust/avro_test_helper/src/logger.rs
index 621405219..97b7e7cb9 100644
--- a/lang/rust/avro_test_helper/src/logger.rs
+++ b/lang/rust/avro_test_helper/src/logger.rs
@@ -55,6 +55,15 @@ pub fn clear_log_messages() {
     LOG_MESSAGES.borrow_mut().clear();
 }
 
+pub fn assert_not_logged(unexpected_message: &str) {
+    if let Some(_last_log) = LOG_MESSAGES.borrow().last() {
+        panic!(
+            "The following log message should not have been logged: '{}'",
+            unexpected_message
+        );
+    }
+}
+
 pub fn assert_logged(expected_message: &str) {
     assert_eq!(LOG_MESSAGES.borrow_mut().pop().unwrap(), expected_message);
 }