DynamicMessage violates contract of hashCode()
Author: adeloof2Created Oct 31, 2024Updated Sep 28, 2026
Labelskeep open
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: falseWhat 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
Source: protocolbuffers/protobuf