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/03/21 11:58:18 UTC

[avro] branch avro-3452-manual-deseriaze-for-Name updated (314e7d3 -> 33bdd64)

This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a change to branch avro-3452-manual-deseriaze-for-Name
in repository https://gitbox.apache.org/repos/asf/avro.git.


 discard 314e7d3  AVRO-3452: Implement custom deserialization for Name that employs the special parsing of name and namespace
     new 33bdd64  AVRO-3452: Implement custom deserialization for Name that employs the special parsing of name and namespace

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (314e7d3)
            \
             N -- N -- N   refs/heads/avro-3452-manual-deseriaze-for-Name (33bdd64)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 lang/rust/avro/tests/schema.rs | 45 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 38 insertions(+), 7 deletions(-)

[avro] 01/01: AVRO-3452: Implement custom deserialization for Name that employs the special parsing of name and namespace

Posted by mg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch avro-3452-manual-deseriaze-for-Name
in repository https://gitbox.apache.org/repos/asf/avro.git

commit 33bdd6444d1bfe5e2c3cc29489a757767800f4f0
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
AuthorDate: Mon Mar 21 12:41:37 2022 +0200

    AVRO-3452: Implement custom deserialization for Name that employs the special parsing of name and namespace
    
    Signed-off-by: Martin Tzvetanov Grigorov <mg...@apache.org>
---
 lang/rust/avro/src/schema.rs   | 23 +++++++++++++++++++--
 lang/rust/avro/tests/schema.rs | 47 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 61 insertions(+), 9 deletions(-)

diff --git a/lang/rust/avro/src/schema.rs b/lang/rust/avro/src/schema.rs
index 9c0750b..6f3e38b 100644
--- a/lang/rust/avro/src/schema.rs
+++ b/lang/rust/avro/src/schema.rs
@@ -228,7 +228,7 @@ impl From<&types::Value> for SchemaKind {
 ///
 /// More information about schema names can be found in the
 /// [Avro specification](https://avro.apache.org/docs/current/spec.html#names)
-#[derive(Clone, Debug, Deserialize, Hash, PartialEq, Eq)]
+#[derive(Clone, Debug, Hash, PartialEq, Eq)]
 pub struct Name {
     pub name: String,
     pub namespace: Namespace,
@@ -265,7 +265,7 @@ impl Name {
     }
 
     /// Parse a `serde_json::Value` into a `Name`.
-    fn parse(complex: &Map<String, Value>) -> AvroResult<Self> {
+    pub(crate) fn parse(complex: &Map<String, Value>) -> AvroResult<Self> {
         let (name, namespace_from_name) = complex
             .name()
             .map(|name| Name::get_name_and_namespace(name.as_str()).unwrap())
@@ -335,6 +335,25 @@ impl fmt::Display for Name {
     }
 }
 
+impl<'de> Deserialize<'de> for Name {
+    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+    where
+        D: serde::de::Deserializer<'de>,
+    {
+        serde_json::Value::deserialize(deserializer).and_then(|value| {
+            if let Value::Object(json) = value {
+                Name::parse(&json).map_err(serde::de::Error::custom)
+            } else {
+                use serde::de::Error;
+                Err(D::Error::custom(format!(
+                    "Expected a json object: {:?}",
+                    value
+                )))
+            }
+        })
+    }
+}
+
 pub(crate) struct ResolvedSchema<'s> {
     names_ref: NamesRef<'s>,
     root_schema: &'s Schema,
diff --git a/lang/rust/avro/tests/schema.rs b/lang/rust/avro/tests/schema.rs
index c404452..292d0aa 100644
--- a/lang/rust/avro/tests/schema.rs
+++ b/lang/rust/avro/tests/schema.rs
@@ -1077,9 +1077,9 @@ fn test_fullname_name_and_namespace_specified() {
 #[test]
 fn test_fullname_fullname_and_namespace_specified() {
     init();
-    let name: Name =
-        serde_json::from_str(r#"{"name": "a.b.c.d", "namespace": "o.a.h", "aliases": null}"#)
-            .unwrap();
+    let name: Name = serde_json::from_str(r#"{"name": "a.b.c.d", "namespace": "o.a.h"}"#).unwrap();
+    assert_eq!(&name.name, "d");
+    assert_eq!(name.namespace, Some("a.b.c".to_owned()));
     let fullname = name.fullname(None);
     assert_eq!("a.b.c.d", fullname);
 }
@@ -1087,8 +1087,9 @@ fn test_fullname_fullname_and_namespace_specified() {
 #[test]
 fn test_fullname_name_and_default_namespace_specified() {
     init();
-    let name: Name =
-        serde_json::from_str(r#"{"name": "a", "namespace": null, "aliases": null}"#).unwrap();
+    let name: Name = serde_json::from_str(r#"{"name": "a", "namespace": null}"#).unwrap();
+    assert_eq!(&name.name, "a");
+    assert_eq!(name.namespace, None);
     let fullname = name.fullname(Some("b.c.d".into()));
     assert_eq!("b.c.d.a", fullname);
 }
@@ -1096,18 +1097,48 @@ fn test_fullname_name_and_default_namespace_specified() {
 #[test]
 fn test_fullname_fullname_and_default_namespace_specified() {
     init();
-    let name: Name =
-        serde_json::from_str(r#"{"name": "a.b.c.d", "namespace": null, "aliases": null}"#).unwrap();
+    let name: Name = serde_json::from_str(r#"{"name": "a.b.c.d", "namespace": null}"#).unwrap();
+    assert_eq!(&name.name, "d");
+    assert_eq!(name.namespace, Some("a.b.c".to_owned()));
     let fullname = name.fullname(Some("o.a.h".into()));
     assert_eq!("a.b.c.d", fullname);
 }
 
 #[test]
+fn test_avro_3452_parsing_name_without_namespace() {
+    init();
+    let name: Name = serde_json::from_str(r#"{"name": "a.b.c.d"}"#).unwrap();
+    assert_eq!(&name.name, "d");
+    assert_eq!(name.namespace, Some("a.b.c".to_owned()));
+    let fullname = name.fullname(None);
+    assert_eq!("a.b.c.d", fullname);
+}
+
+#[test]
+fn test_avro_3452_parsing_name_with_leading_dot_without_namespace() {
+    init();
+    let name: Name = serde_json::from_str(r#"{"name": ".a"}"#).unwrap();
+    assert_eq!(&name.name, "a");
+    assert_eq!(name.namespace, None);
+    assert_eq!("a", name.fullname(None));
+}
+
+#[test]
+fn test_avro_3452_parse_json_without_name_field() {
+    init();
+    let result: serde_json::error::Result<Name> = serde_json::from_str(r#"{"unknown": "a"}"#);
+    assert!(&result.is_err());
+    assert_eq!(result.unwrap_err().to_string(), "No `name` field");
+}
+
+#[test]
 fn test_fullname_fullname_namespace_and_default_namespace_specified() {
     init();
     let name: Name =
         serde_json::from_str(r#"{"name": "a.b.c.d", "namespace": "o.a.a", "aliases": null}"#)
             .unwrap();
+    assert_eq!(&name.name, "d");
+    assert_eq!(name.namespace, Some("a.b.c".to_owned()));
     let fullname = name.fullname(Some("o.a.h".into()));
     assert_eq!("a.b.c.d", fullname);
 }
@@ -1117,6 +1148,8 @@ fn test_fullname_name_namespace_and_default_namespace_specified() {
     init();
     let name: Name =
         serde_json::from_str(r#"{"name": "a", "namespace": "o.a.a", "aliases": null}"#).unwrap();
+    assert_eq!(&name.name, "a");
+    assert_eq!(name.namespace, Some("o.a.a".to_owned()));
     let fullname = name.fullname(Some("o.a.h".into()));
     assert_eq!("o.a.a.a", fullname);
 }