[ 
https://issues.apache.org/jira/browse/AVRO-1927?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15568879#comment-15568879
 ] 

Andreas Maier commented on AVRO-1927:
-------------------------------------

So I had a look at the generated Java code. And strangely enough, this code 
only throws an exception, if no default value is set:
{code}
   protected void validate(Field field, Object value) {
        if(!isValidValue(field, value)) {
            if(field.defaultValue() == null) {   // why this check?
                throw new AvroRuntimeException("Field " + field + " does not 
accept null values");
            }
          
        }
    }
{code}
I don't understand why Avro checks, if {{if(field.defaultValue() == null)}} 
before throwing an exception. In my opinion it should always throw an exception 
if the field value is invalid. 

> If a default value is set, Avro allows null values in non-nullable fields.
> --------------------------------------------------------------------------
>
>                 Key: AVRO-1927
>                 URL: https://issues.apache.org/jira/browse/AVRO-1927
>             Project: Avro
>          Issue Type: Bug
>          Components: java
>    Affects Versions: 1.8.1
>            Reporter: Andreas Maier
>              Labels: newbie
>
> With an avro schema like
> {code}
> {
>   "name": "myfield",
>   "type": "string",
>   "default": ""
> }
> {code}
> the following code should throw an exception
> {code}
> MyObject myObject = MyObject.newBuilder().setMyfield(null).build();
> {code}
> But instead the value of myfield is set to null, which causes an exception 
> later when serializing myObject, because null is not a valid value for 
> myfield. 
> I believe in this case setMyfield(null) should throw an exception, 
> independent of the value of default. 
> See also
> https://stackoverflow.com/questions/38509279/generated-avro-builder-set-null-doesnt-overwrite-with-default



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to