Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

GCP Pub/Sub message validation against nested record AVRO schema fails

I have a fairly complex AVRO schema which passes the validation, but the message that’s sent to it somehow isn’t compatible with the AVRO schema I defined.

Since then, I tried narrowing down the schema to understand why this could be happening, and it seems that nested record AVRO schema doesn’t work too well in Pub/Sub.

For example, consider the following AVRO schema:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
 "type" : "record",
 "name" : "Avro",
 "fields" : [
   {
     "name" : "foo",
     "type" : {
        "name": "foo_1",
        "type": "int"
     }
   }
 ]
}

and the sample message:

{
  "foo": {
    "foo_1": 42
  }
}

The sample schema above passes the schema validation in Pub/Sub, but when I test the message above, it fails.

I found similar post here, but it doesn’t really help much.

Perhaps, Pub/Sub has issue with nested record?

Thanks a bunch.

UPDATE

Thanks to Kamal, the original message works. How would I handle UNION[NULL, nestedRecord] case? i.e.

{
    "type": "record",
    "name": "Avro",
    "fields": [
        {
            "name": "foo",
            "type": [
              "null",
                {
                  "type": "record",
                  "name": "NestedRecord",
                  "fields": [
                      {
                          "name": "foo_1",
                          "type": "int"
                      }
                  ]
              }
            ]
        }
    ]
}

Is this not a valid schema? My expectation is that this schema should handle both

{"foo": null}

and

{
  "foo": {
    "foo_1": 42
  }
}

but it only works for the null input, not with the nested input. Seems like a very basic question, but it’s been very frustrating for me since Pub/Sub validation error isn’t really useful..

>Solution :

The schema you have provided does not specify a nested record. Note that you have no additional "record" type inside the fields. The schema you want to match the message you have specified is:

{
    "type": "record",
    "name": "Avro",
    "fields": [
        {
            "name": "foo",
            "type": {
                "type": "record",
                "name": "NestedRecord",
                "fields": [
                    {
                        "name": "foo_1",
                        "type": "int"
                    }
                ]
            }
        }
    ]
}

The schema you specified has a record with a single int field with the name foo. The nested "name" specification is essentially ignored. This is a message that is valid against the schema you provided:

{
  "foo": 42
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading