Skip to content

Canonical form without default value makes it impossible to automatically generate a schema that is compatible with the previous one #252

Closed

Description

@leo-vital

I have a struct like this:

#[derive(AvroSchema)]
struct Something {
   f: String
}

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?

Activity

  1. martin-g commented on Aug 8, 2025

    @martin-g
    Member

    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

    [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).
    
  2. leo-vital commented on Aug 12, 2025

    @leo-vital
    Author

    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

    [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).
    

    @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 AvroSchema derive macro? Or is writing the new schema version manually the only way to have that in this crate?

  3. martin-g commented on Aug 12, 2025

    @martin-g
    Member

    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!

  4. leo-vital commented on Aug 12, 2025

    @leo-vital
    Author

    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!

    @martin-g Let's say this is the struct:

    #[derive(AvroSchema)]
    struct Book {
        title: String,
    }

    And the schema generated from that ends up looking like this:

        {\"type\":\"record\",
        \"name\":\"Book\",
        \"fields\":[{\"name\":\"title\",\"type\":\"string\"}]}
    

    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:

    #[derive(AvroSchema)]
    struct Book {
        title: String,
        subtitle: Option<String>,
    }

    The generated schema will look like this:

        {\"type\":\"record\",
        \"name\":\"Book\",
        \"fields\":[
            {\"name\":\"title\",\"type\":\"string\"},
            {\"name\":\"subtitle\",\"type\":[\"null\",\"string\"]}]}
    

    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.

  5. martin-g commented on Aug 12, 2025

    @martin-g
    Member

    You could use #[avro(default = "null")] on the subtitle field.
    It will produce a RecordField like:

    RecordField {
                    name: "subtitle",
                    doc: None,
                    aliases: None,
                    default: Some(
                        String("null"),
                    ),
                    schema: Union(
                        UnionSchema {
                            schemas: [
                                Null,
                                String,
                            ],
                            variant_index: {
                                Null: 0,
                                String: 1,
                            },
                        },
                    ),
                    order: Ascending,
                    position: 2,
                    custom_attributes: {},
                },
    

    But as you already figured out the canonical form won't have the default field, so most probably you will still get the same error from the registry.

  6. added a commit that references this issue on Aug 12, 2025
  7. martin-g commented on Aug 12, 2025

    @martin-g
    Member

    I've just created #260 - it extends the test case with a Option<String> field

  8. leo-vital commented on Aug 12, 2025

    @leo-vital
    Author

    You could use #[avro(default = "null")] on the subtitle field. It will produce a RecordField like:

    RecordField {
                    name: "subtitle",
                    doc: None,
                    aliases: None,
                    default: Some(
                        String("null"),
                    ),
                    schema: Union(
                        UnionSchema {
                            schemas: [
                                Null,
                                String,
                            ],
                            variant_index: {
                                Null: 0,
                                String: 1,
                            },
                        },
                    ),
                    order: Ascending,
                    position: 2,
                    custom_attributes: {},
                },
    

    But as you already figured out the canonical form won't have the default field, so most probably you will still get the same error from the registry.

    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.

  9. Kriskras99 commented on Aug 12, 2025

    @Kriskras99
    Contributor

    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.

  10. martin-g commented on Aug 13, 2025

    @martin-g
    Member

    @rayokota You might be interested in this discussion too.

  11. rayokota commented on Aug 14, 2025

    @rayokota
    Contributor

    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

  12. martin-g commented on Aug 15, 2025

    @martin-g
    Member

    I am closing this issue.
    The improvement should be done at gklijs/schema_registry_converter#141

  13. added this to the 0.20.0 milestone on Aug 15, 2025
  14. added a commit that references this issue on Oct 28, 2025
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions