Skip to content

Commit 888e555

Browse files
authored
feat: implement identifier-based equals/hashCode for PromptReference (#514)
Ensure PromptReference equality is based solely on identifier and type fields, ignoring other fields like title Signed-off-by: Christian Tzolov <[email protected]>
1 parent 713ee1a commit 888e555

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

mcp/src/main/java/io/modelcontextprotocol/spec/McpSchema.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,6 +2351,22 @@ public PromptReference(String name) {
23512351
public String identifier() {
23522352
return name();
23532353
}
2354+
2355+
@Override
2356+
public boolean equals(Object obj) {
2357+
if (this == obj)
2358+
return true;
2359+
if (obj == null || getClass() != obj.getClass())
2360+
return false;
2361+
PromptReference that = (PromptReference) obj;
2362+
return java.util.Objects.equals(identifier(), that.identifier())
2363+
&& java.util.Objects.equals(type(), that.type());
2364+
}
2365+
2366+
@Override
2367+
public int hashCode() {
2368+
return java.util.Objects.hash(identifier(), type());
2369+
}
23542370
}
23552371

23562372
/**
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2025 - 2025 the original author or authors.
3+
*/
4+
5+
package io.modelcontextprotocol.spec;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
/**
12+
* Test class to verify the equals method implementation for PromptReference.
13+
*/
14+
class PromptReferenceEqualsTest {
15+
16+
@Test
17+
void testEqualsWithSameIdentifierAndType() {
18+
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title");
19+
McpSchema.PromptReference ref2 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Different Title");
20+
21+
assertTrue(ref1.equals(ref2), "PromptReferences with same identifier and type should be equal");
22+
assertEquals(ref1.hashCode(), ref2.hashCode(), "Equal objects should have same hash code");
23+
}
24+
25+
@Test
26+
void testEqualsWithDifferentIdentifier() {
27+
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt-1", "Test Title");
28+
McpSchema.PromptReference ref2 = new McpSchema.PromptReference("ref/prompt", "test-prompt-2", "Test Title");
29+
30+
assertFalse(ref1.equals(ref2), "PromptReferences with different identifiers should not be equal");
31+
}
32+
33+
@Test
34+
void testEqualsWithDifferentType() {
35+
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title");
36+
McpSchema.PromptReference ref2 = new McpSchema.PromptReference("ref/other", "test-prompt", "Test Title");
37+
38+
assertFalse(ref1.equals(ref2), "PromptReferences with different types should not be equal");
39+
}
40+
41+
@Test
42+
void testEqualsWithNull() {
43+
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title");
44+
45+
assertFalse(ref1.equals(null), "PromptReference should not be equal to null");
46+
}
47+
48+
@Test
49+
void testEqualsWithDifferentClass() {
50+
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title");
51+
String other = "not a PromptReference";
52+
53+
assertFalse(ref1.equals(other), "PromptReference should not be equal to different class");
54+
}
55+
56+
@Test
57+
void testEqualsWithSameInstance() {
58+
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title");
59+
60+
assertTrue(ref1.equals(ref1), "PromptReference should be equal to itself");
61+
}
62+
63+
@Test
64+
void testEqualsIgnoresTitle() {
65+
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Title 1");
66+
McpSchema.PromptReference ref2 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Title 2");
67+
McpSchema.PromptReference ref3 = new McpSchema.PromptReference("ref/prompt", "test-prompt", null);
68+
69+
assertTrue(ref1.equals(ref2), "PromptReferences should be equal regardless of title");
70+
assertTrue(ref1.equals(ref3), "PromptReferences should be equal even when one has null title");
71+
assertTrue(ref2.equals(ref3), "PromptReferences should be equal even when one has null title");
72+
}
73+
74+
@Test
75+
void testHashCodeConsistency() {
76+
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title");
77+
McpSchema.PromptReference ref2 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Different Title");
78+
79+
assertEquals(ref1.hashCode(), ref2.hashCode(), "Objects that are equal should have the same hash code");
80+
81+
// Call hashCode multiple times to ensure consistency
82+
int hashCode1 = ref1.hashCode();
83+
int hashCode2 = ref1.hashCode();
84+
assertEquals(hashCode1, hashCode2, "Hash code should be consistent across multiple calls");
85+
}
86+
87+
}

0 commit comments

Comments
 (0)