Can we use "oneOf" in Confluent Platform Schema Registry?

1.7k views Asked by At

I have a use case of user balance changes. I want to put all user balance events in 1 topic. But user balance changes are happening due to multiple events like referral bonus, win bonus, withdrawal, deposit. This can be implemented via nested records like this :

{
"name": "userBalance",
"type": "record",
"fields": [
       {
            "name": "cashDeposit",
            "type": 
                   {
                        "type" : "record",
                        "name" : "userCashDeposit",
                        "fields" : [
                            {"name": "id", "type": "long"},
                            {"name": "amount", "type": "float"}
                        ]
                    }
        },
        {
            "name": "cashWithdraw",
            "type": {
                        "type" : "record",
                        "name" : "userCashWithdraw",
                        "fields" : [
                            {"name": "id", "type": "long"},
                            {"name": "amount", "type": "float"}
                        ]
                    }
        }
    ]
}

But this makes all the nested records as required, while I want to strictly implement that any one of these events be present in the event with strict compliance to that events' record. Avro schema supports "oneOf" but I could not find oneOf being used anywhere for Confluent Schema Registry usecase. Is there any way to use it?

1

There are 1 answers

0
aasthetic On BEST ANSWER

This can work :

{
"name": "userBalance",
"type": "record",
"fields": [
       {
            "name": "userBalance",
            "type": [
                   {
                        "type" : "record",
                        "name" : "userCashDeposit",
                        "fields" : [
                            {"name": "id", "type": "long"},
                            {"name": "amount", "type": "float"}
                        ]
                    },
                    {
                         "type" : "record",
                        "name" : "userCashWithdraw",
                        "fields" : [
                            {"name": "id", "type": "long"},
                            {"name": "amount", "type": "float"}
                        ]
                     }
             ]
        }
    ]
}