Skip to content

Commit 3456a77

Browse files
authored
Merge pull request #3085 from mduesterhoeft/equals-object
Consider properties for equality of ObjectProperty
2 parents b30b153 + 27db627 commit 3456a77

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

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

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

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)