Repository navigation
Description
Hi all!
While working on issue #353, I have also bumped into some possible issues with how we currently perform writer to reader schema resolution.
There are two issues here:
- Resolution coercions in this implementation that are not in the specification.
- Not using type names in resolution.
Currently, in my reading of the code, writer to reader schema resolution is performed by first deserializing some buffer into a Value using the "writer" schema and then by calling resolve (or resolve_schemata) on this value with the reader schema. I think the fact that we are performing schema resolution on the value directly without reference to the original writer schema is central to both of these issues.
Resolution coercions not in the specification
Currently, we are allowed to resolve Value::Map against a Schema::Record and to resolve Value::Long against a Schema::Int. This last resolution rule causes buggy behavior, as seen by this test.
#[test]
fn test_long_to_int() -> TestResult {
init();
let writer_schema = r#"{
"type": "record",
"name": "ASchema",
"fields": [
{
"name": "aNumber",
"type": "long"
}
]
}"#;
let reader_schema = r#"{
"type": "record",
"name": "ASchema",
"fields": [
{
"name": "aNumber",
"type": "int"
}
]
}"#;
// value that was written with writer_schema
let value = Value::Record(vec![(
"aNumber".into(),
Value::Long(300000000000)
)
]);
let buffer = TestBuffer(Rc::new(RefCell::new(Vec::new())), 0);
let writer_schema = Schema::parse_str(writer_schema)?;
let mut writer = Writer::new(&writer_schema, buffer.clone())?;
let _ = writer.append(value);
let _ = writer.flush();
let reader_schema = Schema::parse_str(reader_schema)?;
let mut reader = Reader::with_schema(&reader_schema,buffer)?;
let read_value = reader.next();
match read_value {
Some(v) => {
panic!("Reader and writer schema are incompatible, should not be able to deserialize. Deser value: {:?}", v)
}
None => {}
}
Ok(())
}Currently, this test fails and the value of the "aNumber" field in the reader schema is a silently overflowed integer.
Not using type names in resolution
The specification's rules for schema resolution indicate that we first compare the names of named types, and if that succeeds, perform resolution recursively on that value. Since Value does not include names for named types (fixed, record, and enum), this check is not performed.
What should we do
The fix for the long->int conversion is just to remove this arm from the resolution logic we currently have. However, I think this issue is actually a little deeper.
Schema resolution as defined by the specification requires three things, a writer schema, a reader schema and a value written with the writer schema (the value is needed for resolving which union branch of the reader schema to take, if possible).
As of now, we are performing schema resolution on just the value itself. Since the value doesn't actually have schema level information like the name of named types, there are schema resolution checks (matching via unqualified name) that are impossible with the current structure of Value. Therefore, our resolution feels completely structural. However, my reading of the resolution rules from the specification indicate that the names of named types are important and are used, hence resolution is at least quasi-nominal.
Proposed solution
Reorient Value as being a flexible user-facing way of creating Avro values. Value should include the minimum amount of information to be verified against a provided schema but nothing more. That means:
- Get rid of Value::Union. In my opinion, a union is a schema-level construct describing possible types; a concrete value inhabits a specific branch, not the union itself
- Rename Value::Enum as Value::EnumVariant. Enum is the type, variant is the value.
- Values of named types (Record, Fixed, Enum) should allow an optional name. If no name is provided, resolution is essentially the same structural resolution we have now. If a name is provided, we first perform resolution by name, and if that succeeds, perform structural resolution.
To fulfill the semantic role of a value of a given schema, we should create a new type: TypedValue that wraps both a Value and a schema and verifies that the provided Value is indeed a valid value that could have been written with the schema, performing coercion steps if needed (these coercion steps are free to be a superset of the resolution steps listed in the specification). In doing so, Value should also be transformed into a new type CompleteValue that includes information from the schema its wrapped with, like named type names and union branch numbers.
Schema resolution is then a transformation from TypedValue of one schema to a TypedValue of another by performing exactly the resolution steps listed in the specification.
To discuss
Since this has overlap with the work from #353, I was thinking of rolling these two up as one eventual PR. Let me know if the above proposal makes sense.
Go Avro!
Activity
martin-g commentedon Dec 30, 2025
Please cite the part of the spec that you refer to. It would make it easier for others to confirm/reject your understanding of the spec.
KitFieldhouse commentedon Dec 30, 2025
Yes of course, my apologies. Here is the excerpt from this section of the specification I had in mind:
The section then goes on to describe additional checks to perform if both schemas are records, unions, etc.
change 'resolution' to be Value->TypedValue coercion (issue apache#383)
Kriskras99 commentedon Jan 7, 2026
I think the issue you found with resolving
LongtoInt, is a valid issue. The code uses anascast instead of atry_fromand therefore silently truncates values.The fact that resolving is more lenient than allowed by the specification is also a valid concern, but is not something I think needs to be fixed.
I think your suggested improvement are not usable. They are all severely breaking changes and I think they are not an improvement.
If you want resolving that matches the specification, use
SchemaCompatiblityto check compatiblity before resolving.CompleteValue/TypedValueare also something you can create yourself while reading values from the reader.Kriskras99 commentedon Jan 9, 2026
@martin-g unless you disagree, I suggest we close this issue. The current resolving logic is quite lenient, but I think making it more strict will cause more harm than good. The bug with
Long -> Inthas been fixed, so there's nothing actionable left in this issue.martin-g commentedon Jan 12, 2026
avro-rs/avro/src/types.rs
Lines 75 to 88 in 6fc1f96
It is kind of the case already - we just have a
u32index of the variant as an extra information.About
Enumbe renamed toEnumVariant- I agree it would have been a more correct name, but I also agree with @Kriskras99 that it is not worth the troubles renaming it now. The crate is still v0.x but we try to keep the API breaks as less as possible!@KitFieldhouse Would that make something possible that is currently impossible/broken ?
Kriskras99 commentedon Jan 12, 2026
I realised this morning that using the name in resolving would allow the following two schemas to resolve correctly:
[{"type": "record", "name": "A", "fields": []},{"type": "record", "name": "B", "fields": []}][{"type": "record", "name": "B", "fields": []},{"type": "record", "name": "A", "fields": []}]If you do this with the current resolve, than
Awould incorrectly resolve toB.I think this is best solved by changing
Value::resolve(self, schema: &Schema) -> ValuetoValue::resolve(self, reader_schema: &Schema, writer_schema: &Schema) -> Value. We can also store the name in the value, but that would cause a lot of extra string clones.KitFieldhouse commentedon Jan 13, 2026
Thanks for the discussion. I apologize for not responding earlier, I've been out on vacation.
What I had in mind is exactly what Kriskras demonstrates in their above post.
As for storing name in the value, as Kriskras mentions:
I agree that we want to avoid making a bunch of string clones. For the fork I have been working on, copying names is not too much of an issue, as names are (usually) passed via
Arcreferences, so for values created by reading a binary stream with a schema, we can just store this reference. For user created values, the user supplied string literal only needs to live long enough for us to verify the value against a schema, at which point we can again use the reference.I do think optionally accepting names in value makes sense. For instance, for the schema
With the current state of
Value, to specify our record as being record "B", we would need to construct our value like sowhich, in my opinion is a roundabout way of saying what we really want to say which is that we are creating a record with name "B". I think this can lead to a footgun where if a user changes the order of the union in the schema, the "name" of the record may change or we may fail to validate all together.
A few thoughts,
Would we check
Valueagainstwriter_schemafirst and then, if this passes, perform our resolution againstreader_schema?What do we do when the specific type written in
Valuedisagrees with what is specified inwriter_schema?Should we reject this case, or coerce according to what makes sense? For instance, lets say we have a
writer_schemaof{"type": "long"}, areader_schemaof{"type": "double"}and a value ofValue::Int(123)? Should we reject this sinceValuedoes not agree withwriter_schemaeven though there is a clear coercion here? The only situation I can think of where this disagreement between writer schema and value would occur is if the provided value was constructed by the user and not read from a binary stream.Having
TypedValuerepresent a type-level promise that its wrapped value and schema have been checked against each other and any necessary coercions have been performed coupled with appropriately limiting the ways in which a user can create aTypedValueprovides a way of eliminating the situation where we provide aValueand awriter_schemathat are not mutually compatible. Then, if we write functions that takeTypedValueinstead ofValuewe can assume the fast path that no checking betweenwriter_schemaandValueis needed.In so far as this lenience doesn't cause incorrect behavior (like the example in your post) I agree with this argument to a degree. To play devil's advocate here, I see an argument that since resolution is unambiguously defined by the specification, it should be expected that implementations follow those rules and only those rules to ensure consistent behavior between implementations. I could see someone being confused on why this crate can resolve between a reader and writer schema where the Java implementation can not. Though, maybe this is fixable via a note in the documentation.
PookieBuns commentedon Mar 28, 2026
Hi guys. I noticed another issue, specifically wrt Unions when performing schema resolution
In this, if you use a reader's schema, it ends up converting Bar record into Foo record because of how resolution is currently implemented.
avro-rs/avro/src/schema/union.rs
Line 87 in 48dfe5b