This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 5bd1d9d074bc camel-salesforce: make ReferenceId.hashCode agree with
equals (#29679)
5bd1d9d074bc is described below
commit 5bd1d9d074bc9d976398e49a8090f76028ab47eb
Author: dhairya05 <[email protected]>
AuthorDate: Thu Nov 12 17:32:15 2026 +0530
camel-salesforce: make ReferenceId.hashCode agree with equals (#29679)
equals compares the normalized error lists:
Objects.equals(getErrors(), other.getErrors())
where getErrors() turns a null list into an empty one, while hashCode read
the raw field:
result = prime * result + (errors == null ? 0 : errors.hashCode());
So a ReferenceId deserialized without an "errors" property and one carrying
an empty array are equal but hash to different values (0 against the empty
list's 1). That breaks the Object.hashCode contract, and with it any HashMap
or HashSet of reference ids: a lookup with one of the two forms misses an
entry stored under the other.
hashCode now uses getErrors() as well.
---
.../salesforce/api/dto/composite/ReferenceId.java | 3 +-
.../api/dto/composite/ReferenceIdTest.java | 55 ++++++++++++++++++++++
2 files changed, 57 insertions(+), 1 deletion(-)
diff --git
a/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/api/dto/composite/ReferenceId.java
b/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/api/dto/composite/ReferenceId.java
index 6ca7a9c18ffb..ba5f85ebdf75 100644
---
a/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/api/dto/composite/ReferenceId.java
+++
b/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/api/dto/composite/ReferenceId.java
@@ -77,7 +77,8 @@ public final class ReferenceId implements Serializable {
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result + (errors == null ? 0 : errors.hashCode());
+ // getErrors(), not errors: equals() compares the normalized lists, so
a null and an empty list must hash alike
+ result = prime * result + getErrors().hashCode();
result = prime * result + (id == null ? 0 : id.hashCode());
result = prime * result + (referenceId == null ? 0 :
referenceId.hashCode());
return result;
diff --git
a/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/api/dto/composite/ReferenceIdTest.java
b/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/api/dto/composite/ReferenceIdTest.java
new file mode 100644
index 000000000000..ecc0dae4aef9
--- /dev/null
+++
b/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/api/dto/composite/ReferenceIdTest.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.salesforce.api.dto.composite;
+
+import java.util.Collections;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+
+public class ReferenceIdTest {
+
+ @Test
+ public void missingAndEmptyErrorsShouldHashAlike() {
+ final ReferenceId withoutErrors = new ReferenceId("ref1", "001", null);
+ final ReferenceId withEmptyErrors = new ReferenceId("ref1", "001",
Collections.emptyList());
+
+ // equals() compares getErrors(), which turns a null list into an
empty one
+ assertEquals(withoutErrors, withEmptyErrors);
+ assertEquals(withoutErrors.hashCode(), withEmptyErrors.hashCode());
+ }
+
+ @Test
+ public void equalReferenceIdsShouldHashAlike() {
+ final ReferenceId one = new ReferenceId("ref1", "001", null);
+ final ReferenceId other = new ReferenceId("ref1", "001", null);
+
+ assertEquals(one, other);
+ assertEquals(one.hashCode(), other.hashCode());
+ }
+
+ @Test
+ public void differentReferenceIdsShouldNotBeEqual() {
+ final ReferenceId one = new ReferenceId("ref1", "001", null);
+
+ assertNotEquals(one, new ReferenceId("ref2", "001", null));
+ assertNotEquals(one, new ReferenceId("ref1", "002", null));
+ }
+
+}