Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializing Some(date) gives validation error #167

Open
ttencate opened this issue Dec 9, 2020 · 2 comments
Open

Serializing Some(date) gives validation error #167

ttencate opened this issue Dec 9, 2020 · 2 comments

Comments

@ttencate
Copy link

ttencate commented Dec 9, 2020

Using avro-rs 0.11.0 because 0.12.0 won't compile for me.

use avro_rs::{Schema, Writer};
use serde::Serialize;

#[derive(Serialize)]
struct Foo {
    date: Option<i32>,
}

fn main() {
    let schema = Schema::parse_str(
        r#"{
            "type": "record",
            "name": "Foo",
            "fields": [
                {"name": "date", "type": ["null", {"type": "int", "logicalType": "date"}]}
            ]
        }"#)
        .unwrap();
    let mut writer = Writer::new(&schema, Vec::new());

    writer.append_ser(Foo { date: None }).unwrap(); // This works fine.

    writer.append_ser(Foo { date: Some(42) }).unwrap(); // This returns avro_rs::Error::Validation.
}

I think this happens because UnionSchema::find_schema only looks for a variant that matches the input type exactly:

avro-rs/src/schema.rs

Lines 343 to 349 in de6153e

pub fn find_schema(&self, value: &types::Value) -> Option<(usize, &Schema)> {
let type_index = &SchemaKind::from(value);
self.variant_index
.get(type_index)
.copied()
.map(|i| (i, &self.schemas[i]))
}

That logic fails here, because the schema contains a Schema::Date variant but the incoming value is a Value::Int. Note that Value::validate handles this case correctly.

ttencate added a commit to ttencate/avro-rs that referenced this issue Dec 9, 2020
@ttencate
Copy link
Author

ttencate commented Dec 9, 2020

I hacked around this in my fork, but my approach is probably not the best way so I doubt it's worth a PR: ttencate@1c89f55

@poros
Copy link
Collaborator

poros commented Dec 16, 2020

Thanks for reporting the bug. As for the compilation, the error should disappear if you can update your rust version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants