You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "mitasov-ra (via GitHub)" <gi...@apache.org> on 2024/03/05 20:33:49 UTC

[PR] [Java] Fix NPE in Protocol#equals [avro]

mitasov-ra opened a new pull request, #2791:
URL: https://github.com/apache/avro/pull/2791

   <!--
   
   *Thank you very much for contributing to Apache Avro - we are happy that you want to help us improve Avro. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Avro a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/AVRO/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "AVRO-XXXX: [component] Title of the pull request", where *AVRO-XXXX* should be replaced by the actual issue number. 
       The *component* is optional, but can help identify the correct reviewers faster: either the language ("java", "python") or subsystem such as "build" or "doc" are good candidates.  
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests. You can [build the entire project](https://github.com/apache/avro/blob/main/BUILD.md) or just the [language-specific SDK](https://avro.apache.org/project/how-to-contribute/#unit-tests).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Every commit message references Jira issues in their subject lines. In addition, 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"
   
   -->
   
   ## What is the purpose of the change
   
   Fix of NPE in Protocol#equals.
   
   As of documentation, `namespace` is optional in `Protocol` and thus can be `null`. `Protocol#equals` contains direct dereference of `namespace`, which causes `NullPointerException` when comparing Protocols with `null` in `namespace`.
   
   This MR fixes that.
   
   ## Verifying this change
   
   *(Please pick one of the following options)*
   
   This change is a trivial rework / code cleanup without any test coverage.
   
   ## Documentation
   
   - Does this pull request introduce a new feature? (yes / no) - No
   - If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented) - not applicable
   


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


Re: [PR] [Java] Fix NPE in Protocol#equals [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2791:
URL: https://github.com/apache/avro/pull/2791#discussion_r1514533834


##########
lang/java/avro/src/main/java/org/apache/avro/Protocol.java:
##########
@@ -390,13 +391,14 @@ public boolean equals(Object o) {
     if (!(o instanceof Protocol))
       return false;
     Protocol that = (Protocol) o;
-    return this.name.equals(that.name) && this.namespace.equals(that.namespace) && this.types.equals(that.types)
-        && this.messages.equals(that.messages) && this.propsEqual(that);
+    return Objects.equals(this.name, that.name) && Objects.equals(this.namespace, that.namespace)
+        && Objects.equals(this.types, that.types) && Objects.equals(this.messages, that.messages)
+        && this.propsEqual(that);
   }
 
   @Override
   public int hashCode() {
-    return name.hashCode() + namespace.hashCode() + types.hashCode() + messages.hashCode() + propsHashCode();
+    return name.hashCode() + Objects.hash(namespace) + types.hashCode() + messages.hashCode() + propsHashCode();

Review Comment:
   Why not add the other parameters to `Objects.hash`? That would read even better.



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


Re: [PR] AVRO-3956: [Java] Fix NPE in Protocol#equals [avro]

Posted by "mitasov-ra (via GitHub)" <gi...@apache.org>.
mitasov-ra commented on code in PR #2791:
URL: https://github.com/apache/avro/pull/2791#discussion_r1516548282


##########
lang/java/avro/src/main/java/org/apache/avro/Protocol.java:
##########
@@ -390,13 +391,14 @@ public boolean equals(Object o) {
     if (!(o instanceof Protocol))
       return false;
     Protocol that = (Protocol) o;
-    return this.name.equals(that.name) && this.namespace.equals(that.namespace) && this.types.equals(that.types)
-        && this.messages.equals(that.messages) && this.propsEqual(that);
+    return Objects.equals(this.name, that.name) && Objects.equals(this.namespace, that.namespace)
+        && Objects.equals(this.types, that.types) && Objects.equals(this.messages, that.messages)
+        && this.propsEqual(that);
   }
 
   @Override
   public int hashCode() {
-    return name.hashCode() + namespace.hashCode() + types.hashCode() + messages.hashCode() + propsHashCode();
+    return name.hashCode() + Objects.hash(namespace) + types.hashCode() + messages.hashCode() + propsHashCode();

Review Comment:
   Thanks! I've updated `Protocol#hashCode`



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


Re: [PR] AVRO-3956: [Java] Fix NPE in Protocol#equals [avro]

Posted by "mitasov-ra (via GitHub)" <gi...@apache.org>.
mitasov-ra commented on code in PR #2791:
URL: https://github.com/apache/avro/pull/2791#discussion_r1515665312


##########
lang/java/avro/src/main/java/org/apache/avro/Protocol.java:
##########
@@ -390,13 +391,14 @@ public boolean equals(Object o) {
     if (!(o instanceof Protocol))
       return false;
     Protocol that = (Protocol) o;
-    return this.name.equals(that.name) && this.namespace.equals(that.namespace) && this.types.equals(that.types)
-        && this.messages.equals(that.messages) && this.propsEqual(that);
+    return Objects.equals(this.name, that.name) && Objects.equals(this.namespace, that.namespace)
+        && Objects.equals(this.types, that.types) && Objects.equals(this.messages, that.messages)
+        && this.propsEqual(that);
   }
 
   @Override
   public int hashCode() {
-    return name.hashCode() + namespace.hashCode() + types.hashCode() + messages.hashCode() + propsHashCode();
+    return name.hashCode() + Objects.hash(namespace) + types.hashCode() + messages.hashCode() + propsHashCode();

Review Comment:
   @opwvhk So what do you think?



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


Re: [PR] AVRO-3956: [Java] Fix NPE in Protocol#equals [avro]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g merged PR #2791:
URL: https://github.com/apache/avro/pull/2791


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


Re: [PR] [Java] Fix NPE in Protocol#equals [avro]

Posted by "mitasov-ra (via GitHub)" <gi...@apache.org>.
mitasov-ra commented on code in PR #2791:
URL: https://github.com/apache/avro/pull/2791#discussion_r1514539028


##########
lang/java/avro/src/main/java/org/apache/avro/Protocol.java:
##########
@@ -390,13 +391,14 @@ public boolean equals(Object o) {
     if (!(o instanceof Protocol))
       return false;
     Protocol that = (Protocol) o;
-    return this.name.equals(that.name) && this.namespace.equals(that.namespace) && this.types.equals(that.types)
-        && this.messages.equals(that.messages) && this.propsEqual(that);
+    return Objects.equals(this.name, that.name) && Objects.equals(this.namespace, that.namespace)
+        && Objects.equals(this.types, that.types) && Objects.equals(this.messages, that.messages)
+        && this.propsEqual(that);
   }
 
   @Override
   public int hashCode() {
-    return name.hashCode() + namespace.hashCode() + types.hashCode() + messages.hashCode() + propsHashCode();
+    return name.hashCode() + Objects.hash(namespace) + types.hashCode() + messages.hashCode() + propsHashCode();

Review Comment:
   New hash would be backward incompatible with the old one in that case. If that's ok, I do not see any problems switching to `Objects.hash`



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


Re: [PR] AVRO-3956: [Java] Fix NPE in Protocol#equals [avro]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on PR #2791:
URL: https://github.com/apache/avro/pull/2791#issuecomment-2007199047

   Thank you, @mitasov-ra !


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


Re: [PR] AVRO-3956: [Java] Fix NPE in Protocol#equals [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2791:
URL: https://github.com/apache/avro/pull/2791#discussion_r1515847414


##########
lang/java/avro/src/main/java/org/apache/avro/Protocol.java:
##########
@@ -390,13 +391,14 @@ public boolean equals(Object o) {
     if (!(o instanceof Protocol))
       return false;
     Protocol that = (Protocol) o;
-    return this.name.equals(that.name) && this.namespace.equals(that.namespace) && this.types.equals(that.types)
-        && this.messages.equals(that.messages) && this.propsEqual(that);
+    return Objects.equals(this.name, that.name) && Objects.equals(this.namespace, that.namespace)
+        && Objects.equals(this.types, that.types) && Objects.equals(this.messages, that.messages)
+        && this.propsEqual(that);
   }
 
   @Override
   public int hashCode() {
-    return name.hashCode() + namespace.hashCode() + types.hashCode() + messages.hashCode() + propsHashCode();
+    return name.hashCode() + Objects.hash(namespace) + types.hashCode() + messages.hashCode() + propsHashCode();

Review Comment:
   Backwards compatibility is not an issue.
   
   The `Protocol#hashCode()` method has never specified differently, and thus uses the general contract of `hashCode()`. That states that multiple invocations on the same object, if unchanged, yield the same result. However, it is explicitly stated that the result "need not remain consistent from one execution of an application to another execution of the same application".
   
   See https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Object.html#hashCode()



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


Re: [PR] [Java] Fix NPE in Protocol#equals [avro]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on PR #2791:
URL: https://github.com/apache/avro/pull/2791#issuecomment-1981347358

   @mitasov-ra Please file a Jira issue and update the PR title/description.


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