Alfonso Nishikawa created AVRO-1600:
---------------------------------------

             Summary: GenericData#getDefaultValue(Field) behaviour inconsistent 
with javadoc comment when default values is not defined
                 Key: AVRO-1600
                 URL: https://issues.apache.org/jira/browse/AVRO-1600
             Project: Avro
          Issue Type: Bug
          Components: java
    Affects Versions: 1.7.7, 1.7.4
            Reporter: Alfonso Nishikawa
            Priority: Trivial


The behavior of {{GenericData#getDefaultValue(Field)}} when the default value 
is not defined in the schema is to throw an AvroRuntimeException.
The javadoc comment states that should return {{null}} in this case. [This can 
be seen 
here|http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java?view=markup#l872]:
{code}
  /**
   * Gets the default value of the given field, if any.
   * @param field the field whose default value should be retrieved.
   * @return the default value associated with the given field, 
   * or null if none is specified in the schema.
   */
  @SuppressWarnings({ "rawtypes", "unchecked" })
  public Object getDefaultValue(Field field) {    
    JsonNode json = field.defaultValue();
    if (json == null)
      throw new AvroRuntimeException("Field " + field
                                     + " not set and has no default value");
{code}

This behavior affects {{RecordBuilderBase#defaultValue(Field)}} which expects a 
null too, [as stated in it's javadoc 
comment|http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/data/RecordBuilderBase.java?view=markup#l125]:

{code}
  /**
   * Gets the default value of the given field, if any.
   * @param field the field whose default value should be retrieved.
   * @return the default value associated with the given field, 
   * or null if none is specified in the schema.
   * @throws IOException 
   */
  @SuppressWarnings({ "rawtypes", "unchecked" })
  protected Object defaultValue(Field field) throws IOException {    
    return data.deepCopy(field.schema(), data.getDefaultValue(field));
  }
{code}

Fix options:

* Modify the javadoc to comment it throws a runtime exception (and people 
should take this into account).
* Modify the method to return a null as stated in the javadoc comment.





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

Reply via email to