Search code examples
avro

Does Avro support required fields?


i.e. is it possible to make field required similar to ProtoBuf:

message SearchRequest { required string query = 1; }


Solution

  • All fields are required in Avro by default. As is mentioned in the official documentation, if you want to make something optional, you have to make it nullable by unioning its type with null, like this

    { "namespace": "example.avro",
      "type": "record",
      "name": "User",
      "fields": [
        {"name": "name", "type": "string"},
        {"name": "favorite_number",  "type": ["int", "null"]},
        {"name": "favorite_color", "type": ["string", "null"]}
      ]
    }
    

    In this example, name is required, favorite_number and favorite_color are optional. I recommend spending some more time with the documentation.