jsonavrogoogle-cloud-pubsub

How to format JSON PubSub message against Avro schema with "map" type field


I'm testing an Avro schema definition on Google Cloud Platform PubSub. I've simplified the schema down to:

{
  "type": "record",
  "name": "CloudEvent",
  "fields": [
    {
      "name": "attribute",
      "type": {
        "type": "map",
        "values": ["null", "boolean", "int", "string", "bytes"]
      }
    }
  ]
}

However, the JSON encoded message I'm testing with errors with "Message is invalid against schema".

This is the simplest message I'm sending that unexpectedly gives the error:

{
    "attribute": {
        "test": "hello"
    }
}

Interestingly, the following message is considered valid:

{
    "attribute": {}
}

That is expected but makes the previous message erroring even more confusing.


Solution

  • See the Avro specification on JSON encoding. Specifically, the fact that your map has nullable values means that when they do have values, you need to specify them as an object where the key is the name of the type, e.g.,:

    {
        "attribute": {
            "test": {
                "string": "hello"
            }
        }
    }