Skip to content

Find a way to use the provided Schema when using "the serde way" #70

Closed

Description

@martin-g

See

avro-rs/avro/src/schema.rs

Lines 6446 to 6482 in d6a1e4f

#[test]
fn avro_rs_53_uuid_with_fixed() -> TestResult {
#[derive(Debug, Serialize, Deserialize)]
struct Comment {
id: crate::Uuid,
}
impl AvroSchema for Comment {
fn get_schema() -> Schema {
Schema::parse_str(
r#"{
"type" : "record",
"name" : "Comment",
"fields" : [ {
"name" : "id",
"type" : {
"type" : "fixed",
"size" : 16,
"logicalType" : "uuid",
"name": "FixedUUID"
}
} ]
}"#,
)
.expect("Invalid Comment Avro schema")
}
}
let payload = Comment {
id: "de2df598-9948-4988-b00a-a41c0e287398".parse()?,
};
let mut buffer = Vec::new();
SpecificSingleObjectWriter::<Comment>::with_capacity(10)?
.write_ref(&payload, &mut buffer)?;
Ok(())
}

Here the test uses


 SpecificSingleObjectWriter::<Comment>::with_capacity(10)?
            .write_ref(&Comment {id: "de2df598-9948-4988-b00a-a41c0e287398".parse()?}, &mut buffer)?;

The Avro Schema is available via Comment::get_schema() but it is not used at

avro-rs/avro/src/writer.rs

Lines 551 to 555 in d6a1e4f

pub fn write_ref<W: Write>(&mut self, data: &T, writer: &mut W) -> AvroResult<usize> {
let mut serializer = Serializer::default();
let v = data.serialize(&mut serializer)?;
self.inner.write_value_ref(&v, writer)
}
because Serde does not work with schemas.

We need to find a way to make use of the schema because now the id struct field is serialized/encoded as a types::Value::String instead of Uuid or even Fixed(16), as defined in the schema.

Activity

  1. jdarais commented on Mar 2, 2025

    @jdarais
    Contributor

    Hi, thanks for your great work on this library. I started playing around with avro/rust, and ended up creating a PR (#139) that proposes some improvements, which would fix this issue as well. I think the crux of the matter is that the schema isn't being used by the serializer. You're right that Serde doesn't work with schemas, and no schema information embedded in the type being serialized would be exposed to the serializer, but you can implement a Serializer that contains a schema to reference when serializing any value. That's the approach that my PR takes, though you could likely do the same with a Serializer that serializes to a a Value.

  2. added a commit that references this issue on Mar 15, 2025
    f92cf3f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions