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

ASF subversion and git services commented on AVRO-2648:
-------------------------------------------------------

Commit 7347e25381f170a28d6ab081e08ddf4b3354b8fe in avro's branch 
refs/heads/master from Jeffrey Mullins
[ https://gitbox.apache.org/repos/asf?p=avro.git;h=7347e25 ]

AVRO-2648: Incorrect validation of numeric default values

Validation of numeric default values in Java is incorrect and results
in API inconsistencies. Consider the following examples:

Double values as int field default values:
public void testDoubleAsIntDefaultValue() {
  Schema.Field field = new Schema.Field("myField",
          Schema.create(Schema.Type.INT), "doc", 1.1);
  field.hasDefaultValue(); // true
  field.defaultValue(); // internal DoubleNode (1.1)
  field.defaultVal(); // null
  GenericData.get().getDefaultValue(field); // Integer (1)

  field = new Schema.Field("myField",
          Schema.create(Schema.Type.INT), "doc", 1.0);
  field.hasDefaultValue(); // true
  field.defaultValue(); // internal DoubleNode (1.0)
  field.defaultVal(); // null
  GenericData.get().getDefaultValue(field); // Integer (1)
}

Invalid long value as int field default value:
public void testInvalidLongAsIntDefault() {
  Schema.Field field = new Schema.Field("myField",
         Schema.create(Schema.Type.INT), "doc", Integer.MAX_VALUE + 1L);
  field.hasDefaultValue(); // true
  field.defaultValue(); // internal LongNode (2147483648)
  field.defaultVal(); // Long (2147483648)
  GenericData.get().getDefaultValue(field); // Integer (-2147483648)
}

This PR makes changes to invalidate incorrect default values for INT and
LONG schemas, including all floating point values, e.g. 1.0.
Additionally it contains changes to try and return the appropriate
Object type given the schema type.

This change is necessary for correctness and consitency but also
because users cannot disable default value validation and handle
these cases on their own since the underlying Field.defaultValue()
is no longer public. Users only have access to default values
mutated by Field.defaultVal() and GenericData.getDefaultValue().

Notes on JacksonUtils.toObject():
 - This method is used to convert the underlying JsonNode default value
  to an Object when Field.defaultVal() is called. This method is
  invoked regardless of whether default value validation is true or
  false.
 - For LongNode values we continue to return Long values for INT
   schemas in the case we cannot safely convert to an Integer.
   This behavior, while maintained, is inconsistent with that
   of FloatNode / DoubleNode where null is returned for INT
   and LONG schemas. Additional changes may be needed for
   further consistency.

Closes #739
(cherry picked from commit 8b5c11ade7f73eb74a0f017bd52b9b485d68d42f)


> Incorrect validation of numeric default values
> ----------------------------------------------
>
>                 Key: AVRO-2648
>                 URL: https://issues.apache.org/jira/browse/AVRO-2648
>             Project: Apache Avro
>          Issue Type: Bug
>          Components: java
>            Reporter: Jeffrey Mullins
>            Assignee: Jeffrey Mullins
>            Priority: Major
>
> Validation of numeric default values is incorrect and results in API 
> inconsistencies. Below are a few examples.
> Double value as int default value:
> {code:java}
>  public void testDoubleAsIntDefaultValue() {
>     Schema.Field field = new Schema.Field("myField", 
> Schema.create(Schema.Type.INT), "doc", 1.1);
>     field.hasDefaultValue(); // true
>     field.defaultValue(); // internal DoubleNode (1.1)
>     field.defaultVal(); // null
>     GenericData.get().getDefaultValue(field); // Integer (1)
>     
>     field = new Schema.Field("myField", Schema.create(Schema.Type.INT), 
> "doc", 1.0);
>     field.hasDefaultValue(); // true
>     field.defaultValue(); // internal DoubleNode (1.0)
>     field.defaultVal(); // null
>     GenericData.get().getDefaultValue(field); // Integer (1)
> }{code}
>  
> {color:#172b4d}Invalid long as int default value:{color}
> {code:java}
>  public void testInvalidLongAsIntDefault() {
>     Schema.Field field = new Schema.Field("myField", 
> Schema.create(Schema.Type.INT), "doc", Integer.MAX_VALUE + 1L);
>     field.hasDefaultValue(); // true
>     field.defaultValue(); // internal LongNode (2147483648)
>     field.defaultVal(); // Long (2147483648)
>     GenericData.get().getDefaultValue(field); // Integer (-2147483648)
> }{code}
> Additionally, since the underlying Schema.FIeld.defaultValue() is no longer 
> public it's not possible for users to disable default value validation and 
> reliably validate schemas independently.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to