FasterXML / FasterXML/jackson-dataformats-binary

Avro does not respect default values defined in schema

Open
#416 8 comments 0 reactions 0 assignees View on GitHub
avro
Dominant language
Java
Stars
347
Forks
156
Avg merge
3d 3h
Merged PRs (30d)
22

Description

Hello,

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

Example here, was ran in version 2.16.0:

```java

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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.