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

Clueless Joe commented on AVRO-1961:
------------------------------------

Hi Niels

I don't find a simple way to contribute back the changes I made, so I 
copy/paste them here. More precise hint welcomed, sorry for this..

diff --git i/lang/java/avro/src/main/java/org/apache/avro/Schema.java 
w/lang/java/avro/src/main/java/org/apache/avro/Schema.java
index 2019c1f..f2b3963 100644
--- i/lang/java/avro/src/main/java/org/apache/avro/Schema.java
+++ w/lang/java/avro/src/main/java/org/apache/avro/Schema.java
@@ -22,13 +22,13 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Iterator;
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.IdentityHashMap;
+import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
@@ -38,10 +38,10 @@ import java.util.Set;
 
 import org.apache.avro.util.internal.JacksonUtils;
 import org.codehaus.jackson.JsonFactory;
+import org.codehaus.jackson.JsonGenerator;
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.JsonParseException;
 import org.codehaus.jackson.JsonParser;
-import org.codehaus.jackson.JsonGenerator;
 import org.codehaus.jackson.map.ObjectMapper;
 import org.codehaus.jackson.node.DoubleNode;
 
@@ -371,6 +371,20 @@ public abstract class Schema extends JsonProperties {
                        "default","doc","name","order","type","aliases");
   }
 
+  public boolean isNullable() {
+    if (!(this instanceof UnionSchema)) {
+      return getType().equals(Schema.Type.NULL);
+    }
+
+    for (Schema schema : getTypes()) {
+      if (schema.isNullable()) {
+        return true;
+      }
+    }
+
+    return false;
+  }
+
   /** A field within a record. */
   public static class Field extends JsonProperties {
 
diff --git i/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java 
w/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
index 242ee8c..bed751d 100644
--- i/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
+++ w/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
@@ -18,6 +18,7 @@
 package org.apache.avro;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
@@ -77,6 +78,20 @@ public class TestSchema {
   }
 
   @Test
+  public void testIsNullableWorksOnNullableField() {
+    Schema schema = Schema.createUnion(Schema.create(Type.NULL), 
Schema.create(Type.LONG));
+
+    assertTrue(schema.isNullable());
+  }
+
+  @Test
+  public void testIsNullableWorksOnNonNullableField() {
+    Schema schema = Schema.createUnion(Schema.create(Type.LONG));
+
+    assertFalse(schema.isNullable());
+  }
+
+  @Test
   public void testEmptyRecordSchema() {
     Schema schema = Schema.createRecord("foobar", null, null, false);
     String schemaString = schema.toString();
diff --git 
i/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
 
w/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
index 04666e4..a918e30 100644
--- 
i/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
+++ 
w/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
@@ -185,7 +185,7 @@ public class ${this.mangle($schema.getName())}#if 
($schema.isError()) extends or
   }
 
 #foreach ($field in $schema.getFields())
-#if (${this.gettersReturnOptional})
+#if (${this.gettersReturnOptional} && ${field.isNullable})
   /**
    * Gets the value of the '${this.mangle($field.name(), $schema.isError())}' 
field as an Optional<${this.javaType($field.schema())}>.
 #if ($field.doc())      * $field.doc()


> [JAVA] Generate getters that return an Optional
> -----------------------------------------------
>
>                 Key: AVRO-1961
>                 URL: https://issues.apache.org/jira/browse/AVRO-1961
>             Project: Avro
>          Issue Type: New Feature
>            Reporter: Niels Basjes
>            Assignee: Niels Basjes
>            Priority: Minor
>
> A colleague of mine indicated that having getters that return an Optional 
> (java 8 thingy) would be very useful for him.



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

Reply via email to