Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-2648

Incorrect validation of numeric default values

    XMLWordPrintableJSON

Details

    Description

      Validation of numeric default values is incorrect and results in API inconsistencies. Below are a few examples.

      Double value as int default value:

       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 as int 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)
      }

      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.

      Attachments

        Issue Links

          Activity

            People

              jmullins Jeffrey Mullins
              jmullins Jeffrey Mullins
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m