AttitudeL
AttitudeL

Reputation: 309

AVRO schema with default array of objects

I have an AVRO schema with an array field. This array field has items being an object of another AVRO schema. Below is the code I have right now.

{
  "name": "bars",
  "type": ["null", {
    "type": "array",
    "items": "com.example.Bar"
  }],
  "default": null
}

This code is the field definition for "bars" which is an array containing "com.example.Bar" objects.

I have the default set as "null" for now, however I would like to explicitly set the default value to be an array of 3 Bar objects.

Imagine my Bar has the following fields definition

{
  "name": "name",
  "type": [
    "null",
    "string"
  ],
  "default": null
},
{
  "name": "number",
  "type": [
    "null",
    "int"
  ],
  "default": null
}

I would like to set the "bars" default to be

{
  "name": "bars",
  "type": ["null", {
    "type": "array",
    "items": "com.example.Bar"
  }],
  "default": [{"name": "bar1", "number":1}, {"name": "bar2", "number":2}, {"name": "bar3", "number":3}]
}

But this code doesn't work. How am I supposed to set the default value of objects?

Upvotes: 0

Views: 2947

Answers (2)

AttitudeL
AttitudeL

Reputation: 309

The problem is I was allowing "null" to be the type for the array field. I guess if I want to pre-populate the default fields with values I cannot allow null.

So the following code fixes the problem.

{
  "name": "bars",
  "type": {
    "type": "array",
    "items": "com.example.Bar"
  },
  "default": [{"name": "bar1", "number":1}, {"name": "bar2", "number":2}, {"name": "bar3", "number":3}]
}

Upvotes: 0

Chin Huang
Chin Huang

Reputation: 13790

Default values for union fields correspond to the first schema in the union. If the default value is an array, then the array schema should be the first schema in the union.

"type": [
  {
    "type": "array",
    "items": "com.example.Bar"
  },
  "null"
],

Upvotes: 2

Related Questions