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/23 16:51:40 UTC

[GitHub] [avro] opwvhk opened a new pull request #1412: AVRO-2867: Fix NullPointerException on record-valued defaults

opwvhk opened a new pull request #1412:
URL: https://github.com/apache/avro/pull/1412


   If the IDL file defines the schema of a field after the field, record
   valued defaults cause a NullPointerException. This PR fixes that.
   
   The fix addresses two situations:
   1. The field schema itself is a forward reference
      (tested by fixing the missing default value in `forward_ref.avpr`)
   2. The field schema contains a forward reference
      (tested by the `echo` message in the updated `simple.avdl`)
   
   Make sure you have checked _all_ steps below.
   
   ### Jira
   
   - [X] 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-2867
     - ~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
   
   - [X] My PR adds the following unit tests ~__OR__ does not need testing for this extremely good reason~: see above
   
   ### Commits
   
   - [X] 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
   
   - [X] 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] opwvhk commented on a change in pull request #1412: AVRO-2867: Fix NullPointerException on record-valued defaults

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



##########
File path: lang/java/compiler/src/test/idl/output/forward_ref.avpr
##########
@@ -8,7 +8,7 @@
       "fields": [
         { "name":"name", "type": "string", "doc":"the name" },
         { "name": "value", "type": "string", "doc": "the value" },
-        { "name": "type", "type": { "type": "enum", "name":"ValueType", "symbols": ["JSON","BASE64BIN","PLAIN"] } }
+        { "name": "type", "type": { "type": "enum", "name":"ValueType", "symbols": ["JSON","BASE64BIN","PLAIN"] }, "default": "PLAIN" }

Review comment:
       This is the default value specified in `forward_ref.avdl`. Its omission was a bug in the test case.




-- 
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] opwvhk commented on a change in pull request #1412: AVRO-2867: Fix NullPointerException on record-valued defaults

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



##########
File path: lang/java/compiler/src/main/java/org/apache/avro/compiler/idl/IsResolvedSchemaVisitor.java
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro.compiler.idl;
+
+import org.apache.avro.Schema;
+import org.apache.avro.compiler.schema.SchemaVisitor;
+import org.apache.avro.compiler.schema.SchemaVisitorAction;
+
+/**
+ * This visitor checks if the current schema is fully resolved.
+ */
+public final class IsResolvedSchemaVisitor implements SchemaVisitor<Boolean> {
+  boolean hasUnresolvedParts;
+
+  IsResolvedSchemaVisitor() {
+    hasUnresolvedParts = false;
+  }
+
+  @Override
+  public SchemaVisitorAction visitTerminal(Schema terminal) {
+    hasUnresolvedParts = SchemaResolver.isUnresolvedSchema(terminal);
+    return hasUnresolvedParts ? SchemaVisitorAction.TERMINATE : SchemaVisitorAction.CONTINUE;
+  }
+
+  @Override
+  public SchemaVisitorAction visitNonTerminal(Schema nonTerminal) {
+    hasUnresolvedParts = SchemaResolver.isUnresolvedSchema(nonTerminal);
+    if (hasUnresolvedParts) {
+      return SchemaVisitorAction.TERMINATE;
+    }
+    if (nonTerminal.getType() == Schema.Type.RECORD && !nonTerminal.hasFields()) {

Review comment:
       This branch is needed to handle cyclic dependencies.




-- 
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] opwvhk commented on a change in pull request #1412: AVRO-2867: Fix NullPointerException on record-valued defaults

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



##########
File path: lang/java/compiler/src/main/java/org/apache/avro/compiler/schema/Schemas.java
##########
@@ -141,9 +140,8 @@ public static String getJavaClassName(final Schema schema) {
             visited.put(schema, schema);
             break;
           case RECORD:
-            Iterator<Schema> reverseSchemas = schema.getFields().stream().map(Field::schema)
-                .collect(Collectors.toCollection(ArrayDeque::new)).descendingIterator();
-            terminate = visitNonTerminal(visitor, schema, dq, () -> reverseSchemas);
+            terminate = visitNonTerminal(visitor, schema, dq, () -> schema.getFields().stream().map(Field::schema)
+                .collect(Collectors.toCollection(ArrayDeque::new)).descendingIterator());

Review comment:
       By inlining the creation of the iterator, we avoid an `AvroException` when using the `IsResolvedSchemaVisitor`.
   Reason: you can't call `getFields()` on a schema that didn't have its fields set yet.




-- 
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] opwvhk commented on a change in pull request #1412: AVRO-2867: Fix NullPointerException on record-valued defaults

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



##########
File path: lang/java/compiler/src/test/idl/input/simple.avdl
##########
@@ -63,6 +60,9 @@ protocol Simple {
     union {null, @foo.foo.bar(42) @foo.foo.foo("3foo") string} prop = null;
   }
 
+  /** An MD5 hash. */
+  fixed MD5(16);
+

Review comment:
       By moving the `MD5` type here, it becomes a forward reference. Without the code changes, this breaks the default value in the `echo` message below.




-- 
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] opwvhk commented on a change in pull request #1412: AVRO-2867: Fix NullPointerException on record-valued defaults

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



##########
File path: lang/java/compiler/src/test/idl/output/simple.avpr
##########
@@ -41,7 +35,12 @@
       "default" : "A"
     }, {
       "name" : "hash",
-      "type" : "MD5",
+      "type" : {
+        "type" : "fixed",
+        "name" : "MD5",
+        "doc" : "An MD5 hash.",
+        "size" : 16
+      },

Review comment:
       Forward references are written as inlined named types when they first occur.
   This is existing, expected behaviour.




-- 
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] opwvhk commented on pull request #1412: AVRO-2867: Fix NullPointerException on record-valued defaults

Posted by GitBox <gi...@apache.org>.
opwvhk commented on pull request #1412:
URL: https://github.com/apache/avro/pull/1412#issuecomment-999034614


   Let's merge this one, and I'll push a new commit to #1411 to resolve the conflict.


-- 
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] opwvhk commented on a change in pull request #1412: AVRO-2867: Fix NullPointerException on record-valued defaults

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



##########
File path: lang/java/compiler/src/main/java/org/apache/avro/compiler/idl/IsResolvedSchemaVisitor.java
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro.compiler.idl;
+
+import org.apache.avro.Schema;
+import org.apache.avro.compiler.schema.SchemaVisitor;
+import org.apache.avro.compiler.schema.SchemaVisitorAction;
+
+/**
+ * This visitor checks if the current schema is fully resolved.
+ */
+public final class IsResolvedSchemaVisitor implements SchemaVisitor<Boolean> {
+  boolean hasUnresolvedParts;
+
+  IsResolvedSchemaVisitor() {
+    hasUnresolvedParts = false;
+  }
+
+  @Override
+  public SchemaVisitorAction visitTerminal(Schema terminal) {
+    hasUnresolvedParts = SchemaResolver.isUnresolvedSchema(terminal);
+    return hasUnresolvedParts ? SchemaVisitorAction.TERMINATE : SchemaVisitorAction.CONTINUE;
+  }
+
+  @Override
+  public SchemaVisitorAction visitNonTerminal(Schema nonTerminal) {
+    hasUnresolvedParts = SchemaResolver.isUnresolvedSchema(nonTerminal);
+    if (hasUnresolvedParts) {
+      return SchemaVisitorAction.TERMINATE;
+    }
+    if (nonTerminal.getType() == Schema.Type.RECORD && !nonTerminal.hasFields()) {
+      // We're still initializing the type...
+      return SchemaVisitorAction.SKIP_SUBTREE;
+    }

Review comment:
       This branch is needed to handle cyclic dependencies.




-- 
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] opwvhk commented on a change in pull request #1412: AVRO-2867: Fix NullPointerException on record-valued defaults

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



##########
File path: lang/java/compiler/src/main/java/org/apache/avro/compiler/idl/ResolvingVisitor.java
##########
@@ -139,10 +139,7 @@ public SchemaVisitorAction afterVisitNonTerminal(final Schema nt) {
         List<Schema.Field> fields = nt.getFields();
         List<Schema.Field> newFields = new ArrayList<>(fields.size());
         for (Schema.Field field : fields) {
-          Schema.Field newField = new Schema.Field(field.name(), replace.get(field.schema()), field.doc(),
-              field.defaultVal(), field.order());
-          copyAllProperties(field, newField);
-          newFields.add(newField);
+          newFields.add(new Field(field, replace.get(field.schema())));

Review comment:
       In the previous version, `field.defaultVal()` results in `null` for **all** default values if the type is not yet resolved.
   By using the `Field(Field, Schema)` constructor, the underlying JSON object is used, preserving the default value.




-- 
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] opwvhk commented on a change in pull request #1412: AVRO-2867: Fix NullPointerException on record-valued defaults

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



##########
File path: lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj
##########
@@ -1349,7 +1349,7 @@ void VariableDeclarator(Schema type, List<Field> fields):
       if ("order".equals(key))
         order = Field.Order.valueOf(getTextProp(key,props,token).toUpperCase());
 
-    boolean validate = !SchemaResolver.isUnresolvedSchema(type);
+    boolean validate = SchemaResolver.isFullyResolvedSchema(type);

Review comment:
       This ensures we only validate fields if the schema is (recursively) resolved.
   
   Note that although this triggers validation less often, every default value is still validated because `ResolvingVisitor` copies the entire schema, and thus (re)validates all default values.




-- 
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 #1412: AVRO-2867: Fix NullPointerException on record-valued defaults

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


   


-- 
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