You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by KV 59 <kv...@gmail.com> on 2021/03/23 22:16:57 UTC

Setting a null value to field with default value

Hi,
I have a schema defined as below


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> *{  "type" : "record",  "name" : "DefRecord",  "namespace" :
> "com.test.ns1",  "doc" : "Message",  "fields" : [ {    "name" :
> "providerId",    "type" : {      "type" : "string",      "avro.java.string"
> : "String"    }  }, {    "name" : "defId",    "type" : {      "type" :
> "string",      "avro.java.string" : "String"    }    "default" : ""  }, {
>   "name" : "text",    "type" : [ "null", {      "type" : "string",
> "avro.java.string" : "String"    } ],    "doc" : "the text",    "default" :
> null  }, {    "name" : "count",    "type" : "int",    "doc" : "Number of
> segments in message",    "default" : 0  }, {    "name" : "defBytes",
> "type" : "bytes",    "default" : "ÿ"  } ]}*


I generated the classes using Java and using the builder tries to create an
object

DefRecord defRecord = DefRecord.newBuilder()
    .setProviderId("providerId")
    .setCount(1)
    .setDefId(null)
    .build();

The build runs successfully. (I expect this to fail as the field defId is
not nullable)

When I serialize this object it throws a NullPointerException

What I want to know is why the build is successful in the first place.
Shouldn't the build fail as we are trying to set a null value to a non
nullable field. Is there a reason for this behavior.

Thanks

Re: Setting a null value to field with default value

Posted by Ryan Skraba <ry...@skraba.com>.
Hello!  I can reproduce it in Avro 1.10.2, and I think this is a bug.
I raised https://issues.apache.org/jira/browse/AVRO-3091 to track it.
Thanks so much for the full example!

It looks like the workaround is to validate the defId in your own code
before building.  Until this is fixed, the newBuilder() won't catch
these errors (and I think it should).

Best regards, Ryan

On Tue, Mar 23, 2021 at 11:17 PM KV 59 <kv...@gmail.com> wrote:
>
> Hi,
> I have a schema defined as below
>
>> {
>>   "type" : "record",
>>   "name" : "DefRecord",
>>   "namespace" : "com.test.ns1",
>>   "doc" : "Message",
>>   "fields" : [ {
>>     "name" : "providerId",
>>     "type" : {
>>       "type" : "string",
>>       "avro.java.string" : "String"
>>     }
>>   }, {
>>     "name" : "defId",
>>     "type" : {
>>       "type" : "string",
>>       "avro.java.string" : "String"
>>     }
>>     "default" : ""
>>   }, {
>>     "name" : "text",
>>     "type" : [ "null", {
>>       "type" : "string",
>>       "avro.java.string" : "String"
>>     } ],
>>     "doc" : "the text",
>>     "default" : null
>>   }, {
>>     "name" : "count",
>>     "type" : "int",
>>     "doc" : "Number of segments in message",
>>     "default" : 0
>>   }, {
>>     "name" : "defBytes",
>>     "type" : "bytes",
>>     "default" : "ÿ"
>>   } ]
>> }
>
>
> I generated the classes using Java and using the builder tries to create an object
>
> DefRecord defRecord = DefRecord.newBuilder()
>     .setProviderId("providerId")
>     .setCount(1)
>     .setDefId(null)
>     .build();
>
> The build runs successfully. (I expect this to fail as the field defId is not nullable)
>
> When I serialize this object it throws a NullPointerException
>
> What I want to know is why the build is successful in the first place. Shouldn't the build fail as we are trying to set a null value to a non nullable field. Is there a reason for this behavior.
>
> Thanks
>