Is there a way to make Google Cloud Pub/Sub Schema fields optional?

Viewed 519

The title says it all, really. I'm struggling to figure out how to make a Google Cloud Pub/Sub schema that has optional fields. Or would having optional fields in an AVRO schema basically directly contradict the whole point of having a schema?

The structure I tried is this, with no success:

{
  "type": "record",
  "name": "Avro",
  "fields": [
    {
      "name": "TestStringField",
      "type": ["null", "string"],
      "default": ""
    },
    {
      "name": "TestIntField",
      "type": ["null", "int"],
      "default": 0
    }
  ]
}
1 Answers

The schema as presented in the question needs to have the null values be the default since null is the first type in the union:

{
  "type": "record",
  "name": "Avro",
  "fields": [
    {
      "name": "TestStringField",
      "type": ["null", "string"],
      "default": null
    },
    {
      "name": "TestIntField",
      "type": ["null", "int"],
      "default": null
    }
  ]
}

A message that wants to be have non-null fields would have to put them inside an object, e.g.:

{
  "TestStringField": {
    "string": "Hi"
  },
  "TestIntField": {
    "int": 7
  }
}
Related