Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVRO-3956: [Java] Fix NPE in Protocol#equals #2791

Merged
merged 2 commits into from Mar 19, 2024

Conversation

mitasov-ra
Copy link
Contributor

@mitasov-ra mitasov-ra commented Mar 5, 2024

AVRO-3956

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

@github-actions github-actions bot added the Java Pull Requests for Java binding label Mar 5, 2024
}

@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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@opwvhk So what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I've updated Protocol#hashCode

@martin-g
Copy link
Member

martin-g commented Mar 6, 2024

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

@mitasov-ra mitasov-ra changed the title [Java] Fix NPE in Protocol#equals AVRO-3956: [Java] Fix NPE in Protocol#equals Mar 7, 2024
@mitasov-ra mitasov-ra requested a review from opwvhk March 19, 2024 08:23
@martin-g martin-g merged commit dad0d20 into apache:main Mar 19, 2024
8 checks passed
@martin-g
Copy link
Member

Thank you, @mitasov-ra !

@mitasov-ra mitasov-ra deleted the bugfix/npe-in-protocol branch March 26, 2024 13:37
RanbirK pushed a commit to RanbirK/avro that referenced this pull request May 13, 2024
* Fix NPE in Protocol#equals

* Better Protocol#hashCode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Java Pull Requests for Java binding
Projects
None yet
3 participants