Skip to content

Schema type for Uuid type is overridden by set_serde_human_readable() parameter on serialization #440

Closed

Description

@alambov-md

Hello team!

Apache Avro Specification states that:

A uuid logical type annotates an Avro string or fixed of length 16.

However, in the current mainline (#6218cd7) schema requirements for underlying type are ignored on uuids serialisation, and the type depends on apache_avro::util::set_serde_human_readable() parameter: when true uuids are serialised into strings, when false - into bytes.
This behaviour differs from latest released version rel/release-0.21.0, where the type depends on the schema.

It seems the behaviour from rel/release-0.21.0 is more correct. Could it be restored?

Attaching here an integration test for convenience.

use apache_avro::{Reader, Schema, Uuid, Writer};
use std::error::Error;

#[test]
fn test_uuid_serliazation_deserialization() -> Result<(), Box<dyn Error>> {
    // This line makes the test to pass.
    // Currently it's passigng only with `set_serde_human_readable(true)`
    // It worked correctly both with `true` an `false in `rel/release-0.21.0`.
    // apache_avro::util::set_serde_human_readable(true);

    let raw_schema = r#" 
    {
        "name": "uuid",
        "type": "string",
        "logicalType": "uuid"
    }
    "#;

    let schema = Schema::parse_str(raw_schema)?;
    let initial_id = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000")?;

    let mut writer = Writer::new(&schema, Vec::new())?;
    writer.append_ser(&initial_id)?;

    let input = writer.into_inner()?;
    let reader = Reader::with_schema(&schema, &input[..])?;
    
    for value in reader {
        println!("{value:?}");
        assert_eq!(initial_id, apache_avro::from_value::<Uuid>(&value?)?);
    }

    Ok(())
}

Activity

  1. Kriskras99 commented on Jan 26, 2026

    @Kriskras99
    Contributor

    It did not work correctly in 0.21.0 (and before).
    If serde_human_readable was set to true it would allways output a string (even if the schema was bytes) and if it was set to false it would always output a bytes (not a fixed!):

    human_readable Uuid
    true string
    false bytes

    In 0.22.0 it will do the following:

    human_readable Uuid (String) Uuid (Bytes) Uuid (Fixed)
    true string Err Err
    false bytes* bytes fixed

    * This is a bug and will be fixed, it should be Err

    What you want is not possible. We don't get a Uuid from Serde. We get a string if human_readable is true (like 550e8400-e29b-41d4-a716-446655440000) or we bytes when human_readable is false (like [171, 27, 146, 24, 38, 212, 254, 206, 21, 172, 18, 139, 191, 244, 27, 226]). So we can't choose the input to match what the schema expects.

  2. alambov-md commented on Jan 26, 2026

    @alambov-md
    Author

    Hi @Kriskras99, technically the serialization result is defined not by serde, but by uuid crate here, but I see what you mean.

    So just to understand, the expected behaviour is to produce errors when schema and serialisation results don't match?

    Additionally, do you see a custom Serialize implementation (for an struct with Uuid field for example) as a good way to ensure the serialisation result is aligned with the schema for both serde_human_readable true and false?

  3. Kriskras99 commented on Jan 26, 2026

    @Kriskras99
    Contributor

    Yes, that is correct.

    Yes, that would be a good option.

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