You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by GitBox <gi...@apache.org> on 2021/11/04 09:12:45 UTC

[GitHub] [avro] martin-g opened a new pull request #1391: AVRO-1851 Better handling for anonymous classes

martin-g opened a new pull request #1391:
URL: https://github.com/apache/avro/pull/1391


   This is a Draft PR with a fix for https://issues.apache.org/jira/browse/AVRO-1851
   There is some other bug because the Pojo's `address` is deserialized into the Pojo's `name` field.
   
   ### Jira
   
   - [ ] My PR addresses the following [Avro Jira](https://issues.apache.org/jira/browse/AVRO/) issues and references them in the PR title. For example, "AVRO-1234: My Avro PR"
     - https://issues.apache.org/jira/browse/AVRO-XXX
     - In case you are adding a dependency, check if the license complies with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   
   ### Tests
   
   - [ ] My PR adds the following unit tests __OR__ does not need testing for this extremely good reason:
   
   ### Commits
   
   - [ ] My commits all reference Jira issues in their subject lines. In addition, my commits follow the guidelines from "[How to write a good git commit message](https://chris.beams.io/posts/git-commit/)":
     1. Subject is separated from body by a blank line
     1. Subject is limited to 50 characters (not including Jira issue reference)
     1. Subject does not end with a period
     1. Subject uses the imperative mood ("add", not "adding")
     1. Body wraps at 72 characters
     1. Body explains "what" and "why", not "how"
   
   ### Documentation
   
   - [ ] In case of new functionality, my PR adds documentation that describes how to use it.
     - All the public functions and the classes in the PR contain Javadoc that explain what it does
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] martin-g commented on a change in pull request #1391: AVRO-1851 Better handling for anonymous classes

Posted by GitBox <gi...@apache.org>.
martin-g commented on a change in pull request #1391:
URL: https://github.com/apache/avro/pull/1391#discussion_r743468730



##########
File path: lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectDatumWithAnonymousEnum.java
##########
@@ -0,0 +1,185 @@
+package org.apache.avro.reflect;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.avro.Schema;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.io.EncoderFactory;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * https://issues.apache.org/jira/browse/AVRO-1851
+ */
+public class TestReflectDatumWithAnonymousEnum {
+  private static Pojo pojo;
+
+  @BeforeClass
+  public static void init() {
+    pojo = new Pojo();
+    Person person = new Person();
+    person.setAddress("Address");
+    pojo.setTestEnum(TestEnum.V);
+    pojo.setPerson(person);
+  }
+
+  // Properly serializes and deserializes a POJO with an enum instance (TestEnum#V)
+  @Test
+  public void handleProperlyEnumInstances() throws IOException {
+    byte[] output = serialize(pojo);
+    Pojo deserializedPojo = deserialize(output);
+    assertEquals(pojo, deserializedPojo);
+  }
+
+  // The test fails because the Schema doesn't support null value for the Person's name
+  @Test(expected = NullPointerException.class)
+  public void avroEnumWithNotNullTest() throws IOException {

Review comment:
       Removed it!
   And made the test more fancy by using anonymous instances for Pojo and Person too.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] martin-g commented on a change in pull request #1391: AVRO-1851 Better handling for anonymous classes

Posted by GitBox <gi...@apache.org>.
martin-g commented on a change in pull request #1391:
URL: https://github.com/apache/avro/pull/1391#discussion_r742660938



##########
File path: lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
##########
@@ -666,6 +666,9 @@ protected Schema createSchema(Type type, Map<String, Schema> names) {
       return result;
     } else if (type instanceof Class) { // Class
       Class<?> c = (Class<?>) type;
+      while (c.isAnonymousClass()) {
+        c = c.getSuperclass();
+      }

Review comment:
       This is the main improvement for this issue.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] martin-g commented on a change in pull request #1391: AVRO-1851 Better handling for anonymous classes

Posted by GitBox <gi...@apache.org>.
martin-g commented on a change in pull request #1391:
URL: https://github.com/apache/avro/pull/1391#discussion_r743468730



##########
File path: lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectDatumWithAnonymousEnum.java
##########
@@ -0,0 +1,185 @@
+package org.apache.avro.reflect;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.avro.Schema;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.io.EncoderFactory;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * https://issues.apache.org/jira/browse/AVRO-1851
+ */
+public class TestReflectDatumWithAnonymousEnum {
+  private static Pojo pojo;
+
+  @BeforeClass
+  public static void init() {
+    pojo = new Pojo();
+    Person person = new Person();
+    person.setAddress("Address");
+    pojo.setTestEnum(TestEnum.V);
+    pojo.setPerson(person);
+  }
+
+  // Properly serializes and deserializes a POJO with an enum instance (TestEnum#V)
+  @Test
+  public void handleProperlyEnumInstances() throws IOException {
+    byte[] output = serialize(pojo);
+    Pojo deserializedPojo = deserialize(output);
+    assertEquals(pojo, deserializedPojo);
+  }
+
+  // The test fails because the Schema doesn't support null value for the Person's name
+  @Test(expected = NullPointerException.class)
+  public void avroEnumWithNotNullTest() throws IOException {

Review comment:
       Removed it!
   And made the test more fancy by using anonymous instances for Pojo and Person too.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] swamyexe commented on a change in pull request #1391: AVRO-1851 Better handling for anonymous classes

Posted by GitBox <gi...@apache.org>.
swamyexe commented on a change in pull request #1391:
URL: https://github.com/apache/avro/pull/1391#discussion_r743115108



##########
File path: lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectDatumWithAnonymousEnum.java
##########
@@ -0,0 +1,185 @@
+package org.apache.avro.reflect;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.avro.Schema;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.io.EncoderFactory;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * https://issues.apache.org/jira/browse/AVRO-1851
+ */
+public class TestReflectDatumWithAnonymousEnum {
+  private static Pojo pojo;
+
+  @BeforeClass
+  public static void init() {
+    pojo = new Pojo();
+    Person person = new Person();
+    person.setAddress("Address");
+    pojo.setTestEnum(TestEnum.V);
+    pojo.setPerson(person);
+  }
+
+  // Properly serializes and deserializes a POJO with an enum instance (TestEnum#V)
+  @Test
+  public void handleProperlyEnumInstances() throws IOException {
+    byte[] output = serialize(pojo);
+    Pojo deserializedPojo = deserialize(output);
+    assertEquals(pojo, deserializedPojo);
+  }
+
+  // The test fails because the Schema doesn't support null value for the Person's name
+  @Test(expected = NullPointerException.class)
+  public void avroEnumWithNotNullTest() throws IOException {

Review comment:
       I agree with you on this.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] RyanSkraba merged pull request #1391: AVRO-1851: Better handling for anonymous classes

Posted by GitBox <gi...@apache.org>.
RyanSkraba merged pull request #1391:
URL: https://github.com/apache/avro/pull/1391


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] swamyexe commented on a change in pull request #1391: AVRO-1851 Better handling for anonymous classes

Posted by GitBox <gi...@apache.org>.
swamyexe commented on a change in pull request #1391:
URL: https://github.com/apache/avro/pull/1391#discussion_r743115108



##########
File path: lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectDatumWithAnonymousEnum.java
##########
@@ -0,0 +1,185 @@
+package org.apache.avro.reflect;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.avro.Schema;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.io.EncoderFactory;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * https://issues.apache.org/jira/browse/AVRO-1851
+ */
+public class TestReflectDatumWithAnonymousEnum {
+  private static Pojo pojo;
+
+  @BeforeClass
+  public static void init() {
+    pojo = new Pojo();
+    Person person = new Person();
+    person.setAddress("Address");
+    pojo.setTestEnum(TestEnum.V);
+    pojo.setPerson(person);
+  }
+
+  // Properly serializes and deserializes a POJO with an enum instance (TestEnum#V)
+  @Test
+  public void handleProperlyEnumInstances() throws IOException {
+    byte[] output = serialize(pojo);
+    Pojo deserializedPojo = deserialize(output);
+    assertEquals(pojo, deserializedPojo);
+  }
+
+  // The test fails because the Schema doesn't support null value for the Person's name
+  @Test(expected = NullPointerException.class)
+  public void avroEnumWithNotNullTest() throws IOException {

Review comment:
       I agree with you on this.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] martin-g commented on a change in pull request #1391: AVRO-1851 Better handling for anonymous classes

Posted by GitBox <gi...@apache.org>.
martin-g commented on a change in pull request #1391:
URL: https://github.com/apache/avro/pull/1391#discussion_r743468730



##########
File path: lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectDatumWithAnonymousEnum.java
##########
@@ -0,0 +1,185 @@
+package org.apache.avro.reflect;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.avro.Schema;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.io.EncoderFactory;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * https://issues.apache.org/jira/browse/AVRO-1851
+ */
+public class TestReflectDatumWithAnonymousEnum {
+  private static Pojo pojo;
+
+  @BeforeClass
+  public static void init() {
+    pojo = new Pojo();
+    Person person = new Person();
+    person.setAddress("Address");
+    pojo.setTestEnum(TestEnum.V);
+    pojo.setPerson(person);
+  }
+
+  // Properly serializes and deserializes a POJO with an enum instance (TestEnum#V)
+  @Test
+  public void handleProperlyEnumInstances() throws IOException {
+    byte[] output = serialize(pojo);
+    Pojo deserializedPojo = deserialize(output);
+    assertEquals(pojo, deserializedPojo);
+  }
+
+  // The test fails because the Schema doesn't support null value for the Person's name
+  @Test(expected = NullPointerException.class)
+  public void avroEnumWithNotNullTest() throws IOException {

Review comment:
       Removed it!
   And made the test more fancy by using anonymous instances for Pojo and Person too.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] martin-g commented on a change in pull request #1391: AVRO-1851 Better handling for anonymous classes

Posted by GitBox <gi...@apache.org>.
martin-g commented on a change in pull request #1391:
URL: https://github.com/apache/avro/pull/1391#discussion_r742965473



##########
File path: lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumWriter.java
##########
@@ -81,8 +81,7 @@ protected void writeArray(Schema schema, Object datum, Encoder out) throws IOExc
       out.writeArrayStart();
       switch (type) {
       case BOOLEAN:
-        if (elementClass.isPrimitive())

Review comment:
       Removed because there is already a check that the elementClass is a primitive one at line 79.

##########
File path: lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectionUtil.java
##########
@@ -118,10 +118,8 @@ private boolean validate(FieldAccess access) throws Exception {
     }
 
     private boolean validField(FieldAccess access, String name, Object original, Object toSet) throws Exception {
-      FieldAccessor a;
-      boolean valid = true;
-      a = accessor(access, name);
-      valid &= original.equals(a.get(this));
+      FieldAccessor a = accessor(access, name);

Review comment:
       No functional change. Just simplification

##########
File path: lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectDatumWithAnonymousEnum.java
##########
@@ -0,0 +1,185 @@
+package org.apache.avro.reflect;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.avro.Schema;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.io.EncoderFactory;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * https://issues.apache.org/jira/browse/AVRO-1851
+ */
+public class TestReflectDatumWithAnonymousEnum {
+  private static Pojo pojo;
+
+  @BeforeClass
+  public static void init() {
+    pojo = new Pojo();
+    Person person = new Person();
+    person.setAddress("Address");
+    pojo.setTestEnum(TestEnum.V);
+    pojo.setPerson(person);
+  }
+
+  // Properly serializes and deserializes a POJO with an enum instance (TestEnum#V)
+  @Test
+  public void handleProperlyEnumInstances() throws IOException {
+    byte[] output = serialize(pojo);
+    Pojo deserializedPojo = deserialize(output);
+    assertEquals(pojo, deserializedPojo);
+  }
+
+  // The test fails because the Schema doesn't support null value for the Person's name
+  @Test(expected = NullPointerException.class)
+  public void avroEnumWithNotNullTest() throws IOException {

Review comment:
       I am not sure this test is needed here. Most probably this functionality is already covered in another test. It has nothing to do with "anonymous enums". It just checks that a non-nullable field (Person#name) cannot have a null value with non-union schema.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] swamyexe commented on a change in pull request #1391: AVRO-1851 Better handling for anonymous classes

Posted by GitBox <gi...@apache.org>.
swamyexe commented on a change in pull request #1391:
URL: https://github.com/apache/avro/pull/1391#discussion_r743115108



##########
File path: lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectDatumWithAnonymousEnum.java
##########
@@ -0,0 +1,185 @@
+package org.apache.avro.reflect;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.avro.Schema;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.io.EncoderFactory;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * https://issues.apache.org/jira/browse/AVRO-1851
+ */
+public class TestReflectDatumWithAnonymousEnum {
+  private static Pojo pojo;
+
+  @BeforeClass
+  public static void init() {
+    pojo = new Pojo();
+    Person person = new Person();
+    person.setAddress("Address");
+    pojo.setTestEnum(TestEnum.V);
+    pojo.setPerson(person);
+  }
+
+  // Properly serializes and deserializes a POJO with an enum instance (TestEnum#V)
+  @Test
+  public void handleProperlyEnumInstances() throws IOException {
+    byte[] output = serialize(pojo);
+    Pojo deserializedPojo = deserialize(output);
+    assertEquals(pojo, deserializedPojo);
+  }
+
+  // The test fails because the Schema doesn't support null value for the Person's name
+  @Test(expected = NullPointerException.class)
+  public void avroEnumWithNotNullTest() throws IOException {

Review comment:
       I agree with you on this.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org