Search code examples
avro

Schema with records referring to each other


I have an schema where I have two records with field that contain elements of each other. How do you forward declare a record in Avro so that you can use its declaration before it is defined.

  {
"namespace": "mytest",
"name": "Foo",
"type": "record",
"fields": [
  {"name" : "bar", "type": ["null", "Bar"]}
],

"name": "Bar",
"type": "record",
"fields": [
  {"name" : "foo", "type": ["null", "Foo"]}
]
}

Solution

  • As far as I know you can't use record statements in schema as you did.

    I think you need a schema like this:

    {
      "type": "record",
      "name": "Foo",
      "namespace": "q44820145",
      "fields": [
        {
          "name": "bar",
          "type": {
            "type": "record",
            "name": "Bar",
            "fields": [
              {
                "name": "foo",
                "type": ["null", "Foo"]
              }
            ]
          }
        }
      ]
    }