Skip to content

Commit 8ccdbd7

Browse files
garyrussellartembilan
authored andcommitted
Fix TopicPartitionOffset Hash Code (NPE)
**cherry-pick to all supported**
1 parent 80b1770 commit 8ccdbd7

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

spring-kafka/src/main/java/org/springframework/kafka/support/TopicPartitionOffset.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public boolean equals(Object o) {
213213

214214
@Override
215215
public int hashCode() {
216-
return this.topicPartition.hashCode() + this.position.hashCode();
216+
return Objects.hash(this.topicPartition, this.position);
217217
}
218218

219219
@Override

spring-kafka/src/test/java/org/springframework/kafka/support/TopicPartitionOffsetTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 the original author or authors.
2+
* Copyright 2020-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,9 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020

21+
import java.util.Objects;
22+
23+
import org.apache.kafka.common.TopicPartition;
2124
import org.junit.jupiter.api.Test;
2225

2326
import org.springframework.kafka.support.TopicPartitionOffset.SeekPosition;
@@ -35,4 +38,10 @@ void hashCodeTest() {
3538
.isNotEqualTo(new TopicPartitionOffset("foo", 1, SeekPosition.END).hashCode());
3639
}
3740

41+
@Test
42+
void hashCodeNPE() {
43+
assertThat(new TopicPartitionOffset("foo", 0).hashCode())
44+
.isEqualTo(Objects.hash(new TopicPartition("foo", 0), null));
45+
}
46+
3847
}

0 commit comments

Comments
 (0)