Skip to content

Avro does not respect default values defined in schema #416

@basimons

Description

@basimons

Hello,

I encountered something strange while doing some tests with the avro decoding.

Example here, was ran in version 2.16.0:

 String avroWithDefault = """
        {
        "type": "record",
        "name": "Employee",
        "fields": [
         {"name": "name", "type": ["string", "null"], "default" : "bram"},
         {"name": "age", "type": "int"},
         {"name": "emails", "type": {"type": "array", "items": "string"}},
         {"name": "boss", "type": ["Employee","null"]}
        ]}
        """;

// Notice no name field
String employeeJson = """
{
    "age" : 26,
    "emails" : ["test@test.com"],
    "boss" : {
         "name" : "test",
         "age" : 33,
         "emails" : ["test@test.com"]
    }
}
""";

SchemaFormat schema = new AvroMapper().schemaFrom(avroWithDefault);
JsonNode jsonObject = new ObjectMapper().reader().readTree(payload);
byte[] objectAsBytes = new AvroMapper().writer().with(formatSchema).writeValueAsBytes(jsonObject);

// Decode it again
JsonNode decodedObject = new AvroMapper().reader(schema).readTree(payload);

System.out.println(decodedObject.toString());

If you look at this object you see that the default value is not filled. It is just a null, all the other fields are filled just as expected. I tried this with different schemas and not having a union with a null, but just the default, but that would result in a JsonMappingException.

Am I doing something wrong here, or is this not supported? It doesn't say that it does not support default values like it says in the protobuffer one.

Thanks in advance

EDIT: This makes sense that it does not work, as you cannot write a AVRO file with a default without a value for it. I think it should've thrown an error on writing. But the main question is why it doesn't work with a reading schema that has a default, but a writing schema that does have one. See my other question.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions