martin-g commented on code in PR #441:
URL: https://github.com/apache/avro-rs/pull/441#discussion_r2729101715


##########
avro/tests/serde_human_readable_true.rs:
##########
@@ -59,3 +60,77 @@ fn avro_rs_53_uuid_with_string_true() -> TestResult {
 
     Ok(())
 }
+
+#[test]
+fn avro_rs_440_uuid_string() -> TestResult {
+    #[derive(apache_avro_derive::AvroSchema, Serialize, Deserialize)]
+    #[serde(transparent)]
+    struct CustomUuid {
+        #[avro(with = || Schema::Uuid(UuidSchema::String))]
+        inner: Uuid,
+    }
+    let uuid = CustomUuid {
+        inner: Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000")?,
+    };
+    let mut buffer = Vec::new();
+
+    assert!(apache_avro::util::set_serde_human_readable(true));
+    let mut writer = SpecificSingleObjectWriter::with_capacity(64)?;
+    writer.write(uuid, &mut buffer)?;
+
+    assert_eq!(
+        String::from_utf8_lossy(&buffer),
+        "�\u{1}'G�8�[\u{4}�H550e8400-e29b-41d4-a716-446655440000"
+    );
+
+    Ok(())
+}
+
+#[test]
+fn avro_rs_440_uuid_bytes() -> TestResult {
+    #[derive(apache_avro_derive::AvroSchema, Serialize, Deserialize)]
+    #[serde(transparent)]
+    struct CustomUuid {
+        #[avro(with = || Schema::Uuid(UuidSchema::Bytes))]
+        inner: Uuid,
+    }
+    let uuid = CustomUuid {
+        inner: Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000")?,
+    };
+    let mut buffer = Vec::new();
+
+    assert!(apache_avro::util::set_serde_human_readable(true));
+    let mut writer = SpecificSingleObjectWriter::with_capacity(64)?;
+    assert_eq!(
+        writer.write(uuid, &mut buffer).unwrap_err().to_string(),
+        "Failed to serialize value of type string using schema Uuid(Bytes): 
550e8400-e29b-41d4-a716-446655440000. Cause: Expected: Uuid. Got: String"

Review Comment:
   ```suggestion
           "Failed to serialize value of type string using schema Uuid(Bytes): 
550e8400-e29b-41d4-a716-446655440000. Cause: Expected: Uuid(Bytes). Got: String"
   ```
   Can we make the error message more helpful ?!



##########
avro/src/serde/ser_schema.rs:
##########
@@ -1218,10 +1218,17 @@ impl<'s, W: Write> SchemaAwareWriteSerializer<'s, W> {
         };
 
         match schema {
-            Schema::String
-            | Schema::Bytes
-            | Schema::Uuid(UuidSchema::Bytes | UuidSchema::String)
-            | Schema::BigDecimal => self.write_bytes(value),
+            Schema::String | Schema::Bytes | Schema::BigDecimal => 
self.write_bytes(value),
+            Schema::Uuid(UuidSchema::Bytes) => {
+                if value.len() == 16 {
+                    self.write_bytes(value)
+                } else {
+                    Err(create_error(format!(
+                        "Expected 16 bytes for `Schema::Uuid(Bytes) but got {} 
bytes",

Review Comment:
   ```suggestion
                           "Expected 16 bytes for `Schema::Uuid(Bytes)` but got 
{} bytes",
   ```



##########
avro/tests/serde_human_readable_false.rs:
##########
@@ -60,3 +61,82 @@ fn avro_rs_53_uuid_with_fixed() -> TestResult {
 
     Ok(())
 }
+
+#[test]
+fn avro_rs_440_uuid_string() -> TestResult {
+    #[derive(apache_avro_derive::AvroSchema, Serialize, Deserialize)]
+    #[serde(transparent)]
+    struct CustomUuid {
+        #[avro(with = || Schema::Uuid(UuidSchema::String))]
+        inner: Uuid,
+    }
+    let uuid = CustomUuid {
+        inner: Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000")?,
+    };
+    let mut buffer = Vec::new();
+
+    assert!(!apache_avro::util::set_serde_human_readable(false));
+    let mut writer = SpecificSingleObjectWriter::with_capacity(64)?;
+    assert!(writer.write(uuid, &mut 
buffer).unwrap_err().to_string().contains("Failed to serialize value of type 
bytes using schema Uuid(String): 55e840e29b41d4a7164466554400. Cause: Expected 
String, Bytes, Uuid, BigDecimal, Fixed, Duration, Decimal, Ref or Union schema. 
Got: Uuid"));

Review Comment:
   `Expected ..., Uuid, ... schema. Got: Uuid"`
   This error message needs improvement.



##########
avro/tests/serde_human_readable_true.rs:
##########
@@ -59,3 +60,77 @@ fn avro_rs_53_uuid_with_string_true() -> TestResult {
 
     Ok(())
 }
+
+#[test]
+fn avro_rs_440_uuid_string() -> TestResult {
+    #[derive(apache_avro_derive::AvroSchema, Serialize, Deserialize)]
+    #[serde(transparent)]
+    struct CustomUuid {
+        #[avro(with = || Schema::Uuid(UuidSchema::String))]
+        inner: Uuid,
+    }
+    let uuid = CustomUuid {
+        inner: Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000")?,
+    };
+    let mut buffer = Vec::new();
+
+    assert!(apache_avro::util::set_serde_human_readable(true));
+    let mut writer = SpecificSingleObjectWriter::with_capacity(64)?;
+    writer.write(uuid, &mut buffer)?;
+
+    assert_eq!(
+        String::from_utf8_lossy(&buffer),
+        "�\u{1}'G�8�[\u{4}�H550e8400-e29b-41d4-a716-446655440000"

Review Comment:
   nit: maybe assert `.contains(uuid_str)` ?!



-- 
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