avro-rs interpreting `uuid` gives SerdeError

Viewed 126

I'm trying to use avro_rs to deserialize data into a struct with a Uuid field.

use avro_rs::types::Value;
use serde::Deserialize;
use uuid::Uuid;

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Metadata {
    pub event_uuid: Uuid,
}

fn main() {
    let uuid = Value::Uuid(Uuid::parse_str("936DA01F9ABD4d9d80C702AF85C822A8").unwrap());
    let avro_uuid = avro_rs::from_value::<Uuid>(&uuid);
    eprintln!("avro_uuid = {:#?}", avro_uuid);

    let metadata = Value::Record(vec![("eventUuid".to_owned(), uuid)]);
    let avro_metadata = avro_rs::from_value::<Metadata>(&metadata);
}

Cargo.toml

[package]
name = "pp-avro"
version = "0.1.0"
edition = "2018"

[dependencies]

avro-rs = { version = ">= 0.6" }
serde = "1.0"
serde_json = "1.0"
uuid = { version = "0.8.1", features = ["serde", "v4"] }

This gives me the error:

avro_uuid = Err(
    Error {
        message: "not a string|bytes|fixed",
    },
)

It is impossible to create a Metadata object.

1 Answers

I'm not an avro_rs expert, but this is my intuition:

fn main() -> Result<(), Box<dyn Error>> {
    let uuid = Uuid::parse_str("936DA01F9ABD4d9d80C702AF85C822A8").unwrap();
    let uuid_val = Value::Uuid(uuid.clone());
    let metadata = Metadata { event_uuid: uuid };
    let ser = avro_rs::to_value(&metadata)?;
    println!("{:?}", ser);
    Ok(())
}

This code prints out:

Record([("eventUuid", String("936da01f-9abd-4d9d-80c7-02af85c822a8"))])

As you already suggested, it would makes sense to be able to run:

let uuid = Uuid::parse_str("936DA01F9ABD4d9d80C702AF85C822A8").unwrap();
let uuid_val = Value::Uuid(uuid);
let metadata = Value::Record(vec![("eventUuid".to_owned(), uuid_val)]);
println!("{:?}", metadata);
avro_rs::from_value::<Metadata>(&metadata)?;

This is a MRE, and it fails with:

Error: Error { message: "not a string|bytes|fixed" }

My guess is that this is either a bug, or something expected when you want to deserialize from a Value without providing a schema.


Old answer:

You've not added the content of your Cargo.toml, but if you're using this library:

I think you're missed the specification of the serde feature, which according to the README:

 * serde - adds the ability to serialize and deserialize a Uuid using the serde crate.

So inside your Cargo.toml, you should have something like this:

uuid = { version = "0.8", features = ["serde"] }
Related