title: Avro field default values
body: <p>I am running into some issues setting up default values for Avro fields. I have a simple schema as given below:</p>
  
  <p><strong>data.avsc:</strong></p>
  
  <pre><code>{
   "namespace":"test",
   "type":"record",
   "name":"Data",
   "fields":[
      { "name": "id", "type": [ "long", "null" ] },
      { "name": "value", "type": [ "string", "null" ] },
      { "name": "raw", "type": [ "bytes", "null" ] }
   ]
  }
  </code></pre>
  
  <p>I am using the <em>avro-maven-plugin v1.7.6</em> to generate the Java model.</p>
  
  <p>When I create an instance of the model using:
  <code>Data data = Data.newBuilder().build();</code>, it fails with an exception: </p>
  
  <blockquote>
    <p>org.apache.avro.AvroRuntimeException:
    org.apache.avro.AvroRuntimeException: Field id type:UNION pos:0 not
    set and has no default value.</p>
  </blockquote>
  
  <p>But if I specify the "default" property,</p>
  
  <pre><code>{ "name": "id", "type": [ "long", "null" ], "default": "null" },
  </code></pre>
  
  <p>I do not get this error. I read in the documentation that first schema in the union becomes the default schema. So my question is, why do I still need to specify the "default" property? How else do I make a field optional?</p>
  
  <p>And if I do need to specify the default values, how does that work for a union; do I need to specify default values for each schema in the union and how does that work in terms of order/syntax?</p>
  
  <p>Thanks.</p>