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

Java Schema Generated sources, non nullable, default defined fails on setField(null)

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.7.7
    • None
    • java

    Description

      I define a schema descriptor like:

      {
          "namespace": "some.namespace",
          "type": "record",
          "name": "SomeBean",
          "fields": [
              {"name": "someField", "type": "string", "default": ""},
          ]
      }
      

      Then I generate some java classes, using org.apache.avro:avro-maven-plugin. In the builder methods I will get the following:

          public some.namespace.SomeBean.Builder setSomeField(java.lang.CharSequence value) {
            validate(fields()[0], value);
            this.someField = value;
            fieldSetFlags()[0] = true;
            return this; 
          }
      

      The Validate methode is like:

          protected void validate(Field field, Object value) {
              if(!isValidValue(field, value)) {
                  if(field.defaultValue() == null) {
                      throw new AvroRuntimeException("Field " + field + " does not accept null values");
                  }
              }
          }
      

      So if I perform SomeBean.setSomeField(null), as the defaultValue is defined, no exception will raise. However, someField will get a null value. That lead to the following type of error:

      java.lang.NullPointerException: null of string in field someField of some.namespace.SomeBean	at org.apache.avro.generic.GenericDatumWriter.npe(GenericDatumWriter.java:93)
      	at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:87)
      	at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:58)
      ...
      Caused by: java.lang.NullPointerException
      	at org.apache.avro.io.Encoder.writeString(Encoder.java:121)
      	at org.apache.avro.generic.GenericDatumWriter.writeString(GenericDatumWriter.java:213)
      	at org.apache.avro.generic.GenericDatumWriter.writeString(GenericDatumWriter.java:208)
      	at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:76)
      	at org.apache.avro.generic.GenericDatumWriter.writeField(GenericDatumWriter.java:114)
      	at org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:104)
      	at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:66)
      	... 7 more
      

      I believe that in such case we should protect the field from being set to null, and remain at its default value.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              secherre Francois Secherre
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated: