Skip to content

Commit 091bc4c

Browse files
committed
Consider properties for equality of ObjectProperty
1 parent d2fbf3e commit 091bc4c

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

modules/swagger-models/src/main/java/io/swagger/models/properties/ObjectProperty.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,25 @@ public ObjectProperty example(Object example) {
139139
this.setExample(example);
140140
return this;
141141
}
142+
143+
@Override
144+
public boolean equals(Object o) {
145+
if (!(o instanceof ObjectProperty)) {
146+
return false;
147+
}
148+
149+
ObjectProperty that = (ObjectProperty) o;
150+
151+
if (properties != null ? !properties.equals(that.properties) : that.properties != null) {
152+
return false;
153+
}
154+
return super.equals(o);
155+
}
156+
157+
@Override
158+
public int hashCode() {
159+
int result = super.hashCode();
160+
result = 31 * result + (properties != null ? properties.hashCode() : 0);
161+
return result;
162+
}
142163
}

modules/swagger-models/src/test/java/io/swagger/models/properties/ObjectPropertyTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import org.testng.annotations.Test;
55

66
import static org.testng.Assert.assertEquals;
7+
import static org.testng.Assert.assertNotEquals;
78
import static org.testng.Assert.assertTrue;
9+
import static org.testng.Assert.assertFalse;
810

911
public class ObjectPropertyTest {
1012

@@ -50,4 +52,38 @@ public void testRequired() {
5052
// then
5153
assertTrue(objectProperty.getRequired(), "The get required must be the same as the set one");
5254
}
55+
56+
@Test
57+
public void testNotEqualOnDifferentProperties() {
58+
//given
59+
ObjectProperty objectProperty = new ObjectProperty();
60+
objectProperty.property("some", new StringProperty());
61+
62+
ObjectProperty otherObjectProperty = new ObjectProperty();
63+
otherObjectProperty.property("someOther", new StringProperty());
64+
65+
//when
66+
boolean equals = objectProperty.equals(otherObjectProperty);
67+
68+
//then
69+
assertFalse(equals);
70+
assertNotEquals(objectProperty.hashCode(), otherObjectProperty.hashCode());
71+
}
72+
73+
@Test
74+
public void testEqualOnEqualProperties() {
75+
//given
76+
ObjectProperty objectProperty = new ObjectProperty();
77+
objectProperty.property("some", new StringProperty());
78+
79+
ObjectProperty otherObjectProperty = new ObjectProperty();
80+
otherObjectProperty.property("some", new StringProperty());
81+
82+
//when
83+
boolean equals = objectProperty.equals(otherObjectProperty);
84+
85+
//then
86+
assertTrue(equals);
87+
assertEquals(objectProperty.hashCode(), otherObjectProperty.hashCode());
88+
}
5389
}

0 commit comments

Comments
 (0)