Avro
  1. Avro
  2. AVRO-1064

Encoder.writeString throws NullPointerException when the charSequence is null

    Details

    • Type: Bug Bug
    • Status: Patch Available
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.6.3
    • Fix Version/s: None
    • Component/s: java
    • Labels:
      None

      Description

      org.apache.avro.io.Encoder.writeString(CharSequence charSequence) throws NullPointerException when dealing with a javabean which has a null value in one of its fields, like the following one:

      Person person = new Person();
      person.setName("Kevin");
      person.setId(null);

        Activity

        Hide
        Kevin Zhao added a comment -

        request for code reviewing~

        Show
        Kevin Zhao added a comment - request for code reviewing~
        Hide
        Thiruvalluvan M. G. added a comment -

        The patch makes Encoder treat null for Avro String as empty. There is no such requirement in Encoder's specification. This would make the the spec somewhat non-uniform. For example, if we assign the interpretation for null for Avro String, we should also add interpretation for null for Avro bytes, fixed and so on.

        I think ensuring the the bean doesn't have null member or annotating the property @Nullable is a better way to handle this.

        Show
        Thiruvalluvan M. G. added a comment - The patch makes Encoder treat null for Avro String as empty. There is no such requirement in Encoder's specification. This would make the the spec somewhat non-uniform. For example, if we assign the interpretation for null for Avro String , we should also add interpretation for null for Avro bytes , fixed and so on. I think ensuring the the bean doesn't have null member or annotating the property @Nullable is a better way to handle this.
        Hide
        Ken Diep added a comment -

        I am new to AVRO and this issue is affecting me as well. As opposed to using an empty string when null is encountered, why not use writeString((String)null)?

            public void writeString(CharSequence charSequence) throws IOException {
                if(charSequence == null) writeString((String)null);
                else super.writeString(charSequence);
            }
        

        Also, if the bean is being generated by AVRO, how would one annotate with @Nullable if there is no way to specify it (well I have yet to find a way)? Manually annotating it after generating the java sources seems to defeat the purpose of the code generation.

        Show
        Ken Diep added a comment - I am new to AVRO and this issue is affecting me as well. As opposed to using an empty string when null is encountered, why not use writeString((String)null)? public void writeString(CharSequence charSequence) throws IOException { if (charSequence == null ) writeString(( String ) null ); else super .writeString(charSequence); } Also, if the bean is being generated by AVRO, how would one annotate with @Nullable if there is no way to specify it (well I have yet to find a way)? Manually annotating it after generating the java sources seems to defeat the purpose of the code generation.
        Hide
        Doug Cutting added a comment -

        If you intend a string to be nullable and are generating Java code from an Avro schema, then the appropriate type is ["null", "string"], a union of null and string.

        Show
        Doug Cutting added a comment - If you intend a string to be nullable and are generating Java code from an Avro schema, then the appropriate type is ["null", "string"] , a union of null and string.

          People

          • Assignee:
            Unassigned
            Reporter:
            Kevin Zhao
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:

              Development