You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Canonical form without default value makes it impossible to automatically generate a schema that is compatible with the previous one #252
Recently I have had a problem with the schema evolution using the BACKWARD compatibility configuration in the Schema Registry. I first create an initial schema version just fine, but then when I add a field whose type is Option<String> to the struct above and try to encode it using schema-registry-converter's encode_struct function, I see this error in the Schema Registry:
io.confluent.kafka.schemaregistry.exceptions.IncompatibleSchemaException: [{errorType:'READER_FIELD_MISSING_DEFAULT_VALUE', description:'The field 'field_name' at path '/fields/6' in the new schema has no default value and is missing in the old schema', additionalInfo:'field_name'},...
Looking at it deeper, I noticed that the main problem seems to be the fact that the canonical form does not include a default value. Even when I mark the field with #[avro(default="null")] the same behavior can be observed. Even though I can see that T::get_schema() returns a schema in which the field contains a default value, when I get the schema's canonical form there is no default value.
My understanding is that this is by design due to the STRIP rule here. But if that is the case, then how exactly can I get a retro-compatible schema when adding an optional field? Is that simply not supported and I need to manually create a schema version in the Schema Registry?
[STRIP] Keep only attributes that are relevant to parsing data, which are: type, name, fields, symbols, items, values, size. Strip all others (e.g., doc and aliases).
[STRIP] Keep only attributes that are relevant to parsing data, which are: type, name, fields, symbols, items, values, size. Strip all others (e.g., doc and aliases).
Less about Apache Avro's internals and more about this Rust crate specifically, however: if we want to add an optional field to a struct whose schema has a previous version already being used, is it possible to do so in a backwards-compatible way using the AvroSchema derive macro? Or is writing the new schema version manually the only way to have that in this crate?
It would be easier to discuss if you provide sample writer (before) schema and reader (after) schema.
If I understand you correctly you ask whether you can derive the reader schema with AvroSchema. Correct ? Let's see how it looks like and then we can answer!
It would be easier to discuss if you provide sample writer (before) schema and reader (after) schema. If I understand you correctly you ask whether you can derive the reader schema with AvroSchema. Correct ? Let's see how it looks like and then we can answer!
So let's say we register that schema as the initial version of the schema, in the Schema Registry. Then we want to add an optional new field to the struct:
And when trying to register that as a new version of the schema in the Registry, the aforementioned READER_FIELD_MISSING_DEFAULT_VALUE error will happen in the Registry.
As discussed in #260 (comment) the PFC might be the wrong thing to use for uploading your schema to the registry. You should probably do serde_json::to_string{_pretty}(&schema), which does include the default field.
We should probably add a to_string or to_schema(pretty: bool) -> String function to Schema to make it easier for the user to do this (instead of having to use serde_json::to_string.
Right, you won't want to use the canonical form when registering the schema with Schema Registry. The schema that is registered is what will be used by the reader. I'm assuming that if you've specified defaults, you'll want those defaults to take effect on read, for schema evolution purposes.
Activity
martin-g commentedon Aug 8, 2025
You should ask this question at [email protected] mailing list.
The strip rule is described in the Avro specification - https://avro.apache.org/docs/1.12.0/specification/#parsing-canonical-form-for-schemas
leo-vital commentedon Aug 12, 2025
@martin-g Thank you, I'll do that.
Less about Apache Avro's internals and more about this Rust crate specifically, however: if we want to add an optional field to a struct whose schema has a previous version already being used, is it possible to do so in a backwards-compatible way using the
AvroSchemaderive macro? Or is writing the new schema version manually the only way to have that in this crate?martin-g commentedon Aug 12, 2025
It would be easier to discuss if you provide sample writer (before) schema and reader (after) schema.
If I understand you correctly you ask whether you can derive the reader schema with
AvroSchema. Correct ? Let's see how it looks like and then we can answer!leo-vital commentedon Aug 12, 2025
@martin-g Let's say this is the struct:
And the schema generated from that ends up looking like this:
So let's say we register that schema as the initial version of the schema, in the Schema Registry. Then we want to add an optional new field to the struct:
The generated schema will look like this:
And when trying to register that as a new version of the schema in the Registry, the aforementioned
READER_FIELD_MISSING_DEFAULT_VALUEerror will happen in the Registry.martin-g commentedon Aug 12, 2025
You could use
#[avro(default = "null")]on thesubtitlefield.It will produce a RecordField like:
But as you already figured out the canonical form won't have the
defaultfield, so most probably you will still get the same error from the registry.Issue #252: Add an `Option<String>` field with a `#[avro(default = "n…
martin-g commentedon Aug 12, 2025
I've just created #260 - it extends the test case with a
Option<String>fieldleo-vital commentedon Aug 12, 2025
Yes, I did not mention it but indeed, even using
#[avro(default = "null")]the generated schema does not change in its canonical form.I ended up sending an email to the mailing list, hopefully I will get an answer eventually.
Kriskras99 commentedon Aug 12, 2025
As discussed in #260 (comment) the PFC might be the wrong thing to use for uploading your schema to the registry. You should probably do
serde_json::to_string{_pretty}(&schema), which does include thedefaultfield.We should probably add a
to_stringorto_schema(pretty: bool) -> Stringfunction toSchemato make it easier for the user to do this (instead of having to useserde_json::to_string.martin-g commentedon Aug 13, 2025
@rayokota You might be interested in this discussion too.
rayokota commentedon Aug 14, 2025
Right, you won't want to use the canonical form when registering the schema with Schema Registry. The schema that is registered is what will be used by the reader. I'm assuming that if you've specified defaults, you'll want those defaults to take effect on read, for schema evolution purposes.
When registering a schema, you can specify that the schema be normalized, which is similar to canonicalization in that in converts the schema to a standard format, but keeps elements like defaults and doc that are stripped during canonicalization. For more on schema normalization see https://docs.confluent.io/platform/current/schema-registry/fundamentals/serdes-develop/index.html
martin-g commentedon Aug 15, 2025
I am closing this issue.
The improvement should be done at gklijs/schema_registry_converter#141
Issue #252: Add an `Option<String>` field with a `#[avro(default = "n…