Kriskras99 commented on code in PR #569:
URL: https://github.com/apache/avro-rs/pull/569#discussion_r3494508411


##########
avro_derive/tests/enum.rs:
##########
@@ -0,0 +1,721 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use apache_avro::reader::datum::GenericDatumReader;
+use apache_avro::writer::datum::GenericDatumWriter;
+use apache_avro::{AvroSchema, Schema};
+use pretty_assertions::assert_eq;
+use serde::{Deserialize, Serialize, de::DeserializeOwned};
+
+/// Takes in a type that implements the right combination of traits and runs 
it through a Serde Cycle and asserts the result is the same
+#[expect(
+    clippy::needless_pass_by_value,
+    reason = "Significantly complicates the trait bounds"
+)]
+#[track_caller]
+fn serde_assert<T>(obj: T)
+where
+    T: std::fmt::Debug + Serialize + DeserializeOwned + AvroSchema + PartialEq,
+{
+    assert_eq!(obj, serde(&obj));
+}
+
+#[track_caller]
+fn serde<T>(obj: &T) -> T
+where
+    T: Serialize + DeserializeOwned + AvroSchema,
+{
+    de(&ser(obj))
+}
+
+#[track_caller]
+fn ser<T>(obj: &T) -> Vec<u8>
+where
+    T: Serialize + AvroSchema,
+{
+    let schema = T::get_schema();
+    GenericDatumWriter::builder(&schema)
+        .build()
+        .unwrap()
+        .write_ser_to_vec(&obj)
+        .unwrap()
+}
+
+#[track_caller]
+fn de<T>(mut encoded: &[u8]) -> T
+where
+    T: DeserializeOwned + AvroSchema,
+{
+    assert!(!encoded.is_empty());
+    let schema = T::get_schema();
+    GenericDatumReader::builder(&schema)
+        .build()
+        .unwrap()
+        .read_deser(&mut encoded)
+        .unwrap()
+}
+
+#[test]
+fn avro_rs_561_bare_union_untagged_empty_tuple_variant() {
+    #[derive(Debug, Eq, PartialEq, AvroSchema, Serialize, Deserialize)]
+    #[avro(repr = "bare_union")]
+    #[serde(untagged)]
+    enum C {
+        A(),
+    }
+
+    serde_assert(C::A());
+}
+
+#[test]
+fn avro_rs_561_bare_union_empty_tuple_variant() {
+    #[derive(Debug, Eq, PartialEq, AvroSchema, Serialize, Deserialize)]
+    #[avro(repr = "bare_union")]
+    enum C {
+        A(),
+        B {},
+    }
+
+    serde_assert(C::A());
+    serde_assert(C::B {});
+}
+
+#[test]
+fn avro_rs_569_enum_repr_default() {
+    #[derive(AvroSchema, Debug, Serialize, Deserialize, Clone, PartialEq)]
+    enum Foo {
+        A,
+        B,
+        #[serde(rename = "D")]
+        C,
+    }
+
+    let schema = Schema::parse_str(
+        r#"{
+        "type": "enum",
+        "name": "Foo",
+        "symbols": ["A", "B", "D"]
+    }"#,
+    )
+    .unwrap();
+
+    assert_eq!(Foo::get_schema(), schema);
+    serde_assert(Foo::A);
+    serde_assert(Foo::B);
+    serde_assert(Foo::C);
+}
+
+#[test]
+fn avro_rs_569_enum_repr_enum() {
+    #[derive(AvroSchema, Debug, Serialize, Deserialize, Clone, PartialEq)]
+    #[avro(repr = "enum")]
+    enum Foo {
+        A,
+        B,
+        #[serde(rename = "D")]
+        C,
+    }
+
+    let schema = Schema::parse_str(
+        r#"{
+        "type": "enum",
+        "name": "Foo",
+        "symbols": ["A", "B", "D"]
+    }"#,
+    )
+    .unwrap();
+
+    assert_eq!(Foo::get_schema(), schema);
+    serde_assert(Foo::A);
+    serde_assert(Foo::B);
+    serde_assert(Foo::C);
+}
+
+#[test]
+fn avro_rs_569_enum_repr_record_tag_content_plain() {
+    #[derive(AvroSchema, Debug, Serialize, Deserialize, Clone, PartialEq)]
+    #[avro(repr = "record_tag_content")]
+    #[serde(tag = "type", content = "value")]
+    enum Foo {
+        A,
+        B,
+        #[serde(rename = "D")]
+        C,
+    }
+
+    let schema = Schema::parse_str(
+        r#"{
+        "type": "record",
+        "name": "Foo",
+        "fields": [
+            {
+                "name": "type",
+                "type": {
+                    "type": "enum",
+                    "name": "type",
+                    "symbols": ["A", "B", "D"]
+                }
+            },
+            {
+                "name": "value",
+                "type": [
+                    "null"
+                ]
+            }
+        ]
+    }"#,
+    )
+    .unwrap();
+
+    assert_eq!(Foo::get_schema(), schema);
+    serde_assert(Foo::A);
+    serde_assert(Foo::B);
+    serde_assert(Foo::C);
+}
+
+#[test]
+fn avro_rs_569_enum_repr_record_tag_content_tuple() {
+    #[derive(AvroSchema, Debug, Serialize, Deserialize, Clone, PartialEq)]
+    #[avro(repr = "record_tag_content")]
+    #[serde(tag = "type", content = "value")]
+    enum Foo {
+        A,
+        Alt(),
+        B(String),
+        #[serde(rename = "D")]
+        C(
+            String,
+            #[serde(rename = "is_it_true", alias = "is_it_false")] bool,
+        ),
+    }
+
+    let schema = Schema::parse_str(
+        r#"{
+        "type": "record",
+        "name": "Foo",
+        "fields": [
+            {
+                "name": "type",
+                "type": {
+                    "type": "enum",
+                    "name": "type",
+                    "symbols": ["A", "Alt", "B", "D"]
+                }
+            },
+            {
+                "name": "value",
+                "type": [
+                    "null",
+                    {
+                        "type": "record",
+                        "name": "Alt",
+                        "fields": [],
+                        "org.apache.avro.rust.tuple": true
+                    },
+                    "string",
+                    {
+                        "type": "record",
+                        "name": "D",
+                        "fields": [
+                            { "name": "field_0", "type": "string" },
+                            { "name": "is_it_true", "aliases": 
["is_it_false"], "type": "boolean" }
+                        ],
+                        "org.apache.avro.rust.tuple": true
+                    }
+                ]
+            }
+        ]
+    }"#,
+    )
+    .unwrap();
+
+    assert_eq!(Foo::get_schema(), schema);
+    serde_assert(Foo::A);

Review Comment:
   This actually surfaced something we can't support. I've now disallowed empty 
tuple variants on enums with `tag` and `content` set. `Foo::Alt{}` does work, 
so I've added that as a test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to