Search code examples
jsonvalidationavro

AvroTypeException: Expected start-union. Got VALUE_NUMBER_INT


[PS : I have gone through all the possible issues under same error and tried to fix my Json Schema] I have the following Avro Schema (Converted to JSON using http://json-schema-validator.herokuapp.com/avro.jsp) :

{
  "definitions" : {
    "record:parentTest.test.MyPlatformData" : {
      "description" : "representsthetestevent",
      "type" : "object",
      "required" : [ "event", "subjects" ],
      "additionalProperties" : false,
      "properties" : {
        "event" : {
          "$ref" : "#/definitions/record:parentTest.test.event_record"
        },
        "subjects" : {
          "type" : "array",
          "items" : {
            "$ref" : "#/definitions/record:parentTest.test.TESTEVENT_record"
          }
        }
      }
    },
    "record:parentTest.test.event_record" : {
      "type" : "object",
      "required" : [ "created_at", "id", "event_at", "subject", "action", "up_event_id", "up_source" ],
      "additionalProperties" : false,
      "properties" : {
        "created_at" : {
          "type" : "string"
        },
        "id" : {
          "type" : "integer",
          "minimum" : -2147483648,
          "maximum" : 2147483647
        },
        "event_at" : {
          "type" : "string"
        },
        "subject" : {
          "type" : "string"
        },
        "action" : {
          "type" : "string"
        },
        "up_event_id" : {
          "default" : "null",
          "oneOf" : [ {
            "type" : "string"
          }, {
            "type" : "null"
          } ]
        },
        "up_source" : {
          "default" : "null",
          "oneOf" : [ {
            "type" : "string"
          }, {
            "type" : "null"
          } ]
        }
      }
    },
    "record:parentTest.test.TESTEVENT_record" : {
      "type" : "object",
      "required" : [ "mydata", "id", "user_id", "data_id", "place_id", "my_object" ],
      "additionalProperties" : false,
      "properties" : {
        "mydata" : {
          "$ref" : "#/definitions/record:parentTest.test.mydata_record"
        },
        "id" : {
          "type" : "integer",
          "minimum" : -9223372036854775808,
          "maximum" : 9223372036854775807
        },
        "user_id" : {
          "type" : "integer",
          "minimum" : -9223372036854775808,
          "maximum" : 9223372036854775807
        },
        "data_id" : {
          "type" : "integer",
          "minimum" : -9223372036854775808,
          "maximum" : 9223372036854775807
        },
        "place_id" : {
          "type" : "integer",
          "minimum" : -2147483648,
          "maximum" : 2147483647
        },
        "my_object" : {
          "$ref" : "#/definitions/record:parentTest.test.my_object_record"
        }
      }
    },
    "record:parentTest.test.mydata_record" : {
      "type" : "object",
      "required" : [ "id", "del" ],
      "additionalProperties" : false,
      "properties" : {
        "id" : {
          "type" : "integer",
          "minimum" : -9223372036854775808,
          "maximum" : 9223372036854775807
        },
        "del" : {
          "$ref" : "#/definitions/record:parentTest.test.del_record"
        }
      }
    },
    "record:parentTest.test.del_record" : {
      "type" : "object",
      "required" : [ "status", "track_details", "cr", "service_level" ],
      "additionalProperties" : false,
      "properties" : {
        "status" : {
          "$ref" : "#/definitions/record:parentTest.test.status_record"
        },
        "tracking_details" : {
          "type" : "array",
          "items" : {
            "$ref" : "#/definitions/record:parentTest.test.tracking_number_detail_record"
          }
        },
        "cr" : {
          "$ref" : "#/definitions/record:parentTest.test.cr_record"
        },
        "service_level" : {
          "$ref" : "#/definitions/record:parentTest.test.service_level_record"
        }
      }
    },
    "record:parentTest.test.status_record" : {
      "type" : "object",
      "required" : [ "name", "status_at" ],
      "additionalProperties" : false,
      "properties" : {
        "name" : {
          "type" : "string"
        },
        "status_at" : {
          "type" : "string"
        }
      }
    },
    "record:parentTest.test.tracking_number_detail_record" : {
      "type" : "object",
      "required" : [ "id", "cr", "status" ],
      "additionalProperties" : false,
      "properties" : {
        "id" : {
          "type" : "string"
        },
        "cr" : {
          "$ref" : "#/definitions/record:parentTest.test.tracking_cr_record"
        },
        "status" : {
          "$ref" : "#/definitions/record:parentTest.test.trac_status_record"
        }
      }
    },
    "record:parentTest.test.tracking_cr_record" : {
      "type" : "object",
      "required" : [ "id" ],
      "additionalProperties" : false,
      "properties" : {
        "id" : {
          "type" : "integer",
          "minimum" : -2147483648,
          "maximum" : 2147483647
        }
      }
    },
    "record:parentTest.test.tracking_status_record" : {
      "type" : "object",
      "required" : [ "name", "status_at" ],
      "additionalProperties" : false,
      "properties" : {
        "name" : {
          "oneOf" : [ {
            "type" : "string"
          }, {
            "type" : "null"
          } ]
        },
        "status_at" : {
          "oneOf" : [ {
            "type" : "string"
          }, {
            "type" : "null"
          } ]
        }
      }
    },
    "record:parentTest.test.cr_record" : {
      "type" : "object",
      "required" : [ "id" ],
      "additionalProperties" : false,
      "properties" : {
        "id" : {
          "type" : "integer",
          "minimum" : -2147483648,
          "maximum" : 2147483647
        }
      }
    },
    "record:parentTest.test.service_level_record" : {
      "type" : "object",
      "required" : [ "sc_id", "ss_id", "ss_class" ],
      "additionalProperties" : false,
      "properties" : {
        "sc_id" : {
          "type" : "integer",
          "minimum" : -2147483648,
          "maximum" : 2147483647
        },
        "ss_id" : {
          "type" : "integer",
          "minimum" : -2147483648,
          "maximum" : 2147483647
        },
        "ss_class" : {
          "type" : "string"
        }
      }
    },
    "record:parentTest.test.mydata_record" : {
      "type" : "object",
      "required" : [ "s_id" ],
      "additionalProperties" : false,
      "properties" : {
        "s_id" : {
          "oneOf" : [ {
            "type" : "integer",
            "minimum" : -2147483648,
            "maximum" : 2147483647
          }, {
            "type" : "null"
          } ]
        }
      }
    }
  },
  "$ref" : "#/definitions/record:parentTest.test.MyPlatformData"
}

However on inserting the following data, I get following error

Input Data

{
  "event": {
    "action": "TESTEVENT",
    "created_at": "2021-01-21T14:16:23+00:00",
    "event_at": "2021-01-21T14:16:23.747",
    "id": 28001755,
    "subject": "TEST_ITEM",
    "up_source": {
      "string": "TEST_W"
    },
    "up_event_id": {
      "string": "59c7eec8-9bf6-4907-9df9-628d92abd278"
    }
  },
  "subjects": [
    {
      "id": 12345678,
      "user_id": 5197509841,
      "data_id": 3497218791,
      "place_id": 49,
      "mydata": {
        "s_id": {
          "int": 29489
        }
      },
      "my_object": {
        "id": 5417499011,
        "del": {
          "status": {
            "name": "TESTEVENT",
            "status_at": "2021-01-21T09:09:00"
          },
          "tracking_details": [
            {
              "id": "CS303755237",
              "cr": {
                "id": 961
              },
              "status": {
                "name": {
                  "string": "TESTEVENT"
                },
                "status_at": {
                  "string": "2021-01-21T09:09:00"
                }
              }
            }
          ],
          "cr": {
            "id": 961
          },
          "service_level": {
            "sc_id": 2,
            "ss_id": 5,
            "ss_class": "MY_TEST_SS"
          }
        }
      }
    }
  ]
}

Error

Caused by: org.apache.avro.AvroTypeException: Expected start-union. Got VALUE_NUMBER_INT

Solution

  • I was able to finally find the issue. Being a beginner, I wasn't able to comprehend that time.

    For anyone facing similar issue :

    Rule : Whenever a field is marked "datatype" and "null" both, it is important to mention the datatype, if the value is not null.

    For example :

    For Avro schema, field definition : 
    
    "up_source" : {
              "default" : "null",
              "oneOf" : [ {
                "type" : "string"
              }, {
                "type" : "null"
              } ]
            }
    
    
    Possible declarations : 
    
    "up_source": {
          "string": "TEST_123"
        },
    
    OR 
    
    "up_source": null,
    

    One of my fields was missing this.