protocolbuffers / protocolbuffers/protobuf

DynamicMessage violates contract of hashCode()

Open
#19,080 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

keep open
Dominant language
C++
Stars
72k
Forks
16.3k
Avg merge
1d 17h
Merged PRs (30d)
140

Description

What version of protobuf and what language are you using?
Version: protoc 28.3 / protobuf-java 4.28.3
Language: Java

What operating system (Linux, Windows, ...) and version?
Windows 11

What runtime / compiler are you using (e.g., python version or gcc version)
BellSoft Liberica 21.0.2

What did you do?

syntax = "proto3";
option java_multiple_files = true;

message ExampleMessage {
  uint32 i = 1;
}
ExampleMessage message = ExampleMessage.newBuilder().build();
DynamicMessage dynamicMessage = DynamicMessage.parseFrom(ExampleMessage.getDescriptor(), message.toByteArray());
System.out.println("objects equal:   " + message.equals(dynamicMessage));
System.out.println("hashcodes equal: " + (message.hashCode() == dynamicMessage.hashCode()));

Output:

objects equal:   true
hashcodes equal: false

What did you expect to see
The DynamicMessage's hashcode should be the same as the real message's hashcode since equals() returns true.

What did you see instead?
The hashcodes are not the same. This issue only seems to happen if the message has a field which is not set.

Anything else we should know about your project / environment
n/a

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The report identifies Java DynamicMessage and a generated ExampleMessage as the entry points; first reproduce the equality/hashCode mismatch with the provided proto and Java snippet, then inspect their equality and hashCode behavior. Done means equal messages produce identical hash codes, including when fields are unset, with regression coverage for this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.