title: Avro nested type java.lang.ClassCastException: org.apache.avro.generic.GenericData$Record cannot be cast to
body: <p>When I try do decode Avro records with the following schema I get the following error:</p>
  
  <pre><code>{
    "type":"record",
    "name":"Example",
    "namespace":"com.example.test",
    "fields":[
      {
          "name":"nestedExample",
          "type":{
              "type":"record",
              "name":"NestedExample",
              "namespace":"com.example.test.Example$",
              "fields":[]
          }
      }
    ]
  }
  </code></pre>
  
  <blockquote>
    <p>java.lang.ClassCastException:
    org.apache.avro.generic.GenericData$Record cannot be cast to
    com.example.test.Example$.NestedExample   at
    com.example.test.Example.put(Example.java:40)     at
    org.apache.avro.generic.GenericData.setField(GenericData.java:573)    at
    org.apache.avro.generic.GenericData.setField(GenericData.java:590)    at
    org.apache.avro.generic.GenericDatumReader.readField(GenericDatumReader.java:193)
      at
    org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:183)
      at
    org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:151)
      at
    org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:142)</p>
  </blockquote>
  
  <p>This appears to be related to the namespace of the nested type. Note: this is how the Avro reflection specifies the namespace if reflection is used on a Java class with a nested class. If I change the namespace to be the same as Example then I can decode the records fine. <strong>How can I decode records that have nested record types when the nested record types are in their own namespace?</strong></p>
  
  <p>I am generating the Java classes using the Avro maven plugin. My Avro version is 1.7.7. Here is the code I am using to decode an Example record:</p>
  
  <pre><code>public class Avro_NestedRecord {
  
      List&lt;Example&gt; examples;
  
      @Before
      public void setUp() throws Exception {
          examples = Arrays.asList(new Example[] {
                  Example.newBuilder()
                  .setNestedExample(new com.example.test.Example$.NestedExample())
                  .build()
          });
      }
  
      @Test
      public void thisIsHowWeCodeSpecificRecordsDirectly() throws IOException {
          Schema schema = Example.getClassSchema();
          // write
          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
          BinaryEncoder encoderInstance = EncoderFactory.get().directBinaryEncoder(outputStream, null);
          SpecificDatumWriter&lt;Example&gt; specificDatumWriter = new SpecificDatumWriter&lt;Example&gt;(schema);
          specificDatumWriter.write(examples.get(0), encoderInstance);
          // read
          ByteArrayInputStream inStream = new ByteArrayInputStream(outputStream.toByteArray());
          BinaryDecoder decoderInstance = DecoderFactory.get().directBinaryDecoder(inStream, null);
          SpecificDatumReader&lt;Example&gt; specificDatumReader = new SpecificDatumReader&lt;Example&gt;(schema);
          specificDatumReader.read(null, decoderInstance);
      }
  }
  </code></pre>