0

actually, I'm using AVRO to validate a json payload. I'm trying to declare a field (defined as record) optional but it does not work.

Schema

...
    {
      "name" : "buildarea",
      "type" : "com.data.Area",
      "type" : ["null","com.data.Area"],
      "default": null
    }
...

with Area defined as

{
  "type": "record",
  "namespace": "com.data",
  "name": "Area",
  "fields": [
        {
          "name": "start",
          "type": "double"
        },
        {
          "name": "end",
          "type": "double"
        }
    ]
 }

Json Example

If I define a not null value I get an exception: Unknown union branch start

"buildarea": {
   "start": 10.20,
   "end": 15
}

If I set the field as null e.g. "buildarea":null, I get an exception message: Expected record-start. Got VALUE_NULL

"buildarea": null

If I remove the optional spec in the schema i.e.

...
    {
      "name" : "buildarea",
      "type" : "com.data.Area"
    }
...

I must always define a not null "buildarea" object, which is not a correct expectation all the time i.e. buildarea maybe null.

Can anybody give me a hint to solve this?

Rubén
  • 427
  • 1
  • 9
  • 23

2 Answers2

0

you have to define your type only once to make it nullable, not twice. Like this

 {
  "name" : "buildarea",
  "type" : ["null","com.data.Area"],
  "default": null
 }
hlagos
  • 7,690
  • 3
  • 23
  • 41
0

This answer is more for my own documentation and maybe help someone who might have the same issue. Let's say this is your schema for your two records Area and BuildArea.

{
  "type": "record",
  "namespace": "com.data",
  "name": "Area",
  "fields": [
        {
          "name": "start",
          "type": "double"
        },
        {
          "name": "end",
          "type": "double"
        }
    ]
 }

{
  "name" : "BuildArea",
  "namespace": "com.data",
  "type" : "Record",
  "fields": [
            {
              "name": "area",
              "type": ["null","Area"]
            },

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

And your JSON data is this

{"area":{"start": 45.0, "end": 45.9}, "name":"build_area_0"}

You might get the same error as the one this question asked about. So to fixed it prefix the object key with the namespace. see this answer

{"com.data.area":{"start": 45.0, "end": 45.9}, "name":"build_area_0"}

If that doesn't work, do this

{"area":"com.data.Area":{"start": 45.0, "end": 45.9}, "name":"build_area_0"}}
CleverChuk
  • 141
  • 1
  • 6