From fe05f8cfa28022ba0743d1216716bcfe936b9ef8 Mon Sep 17 00:00:00 2001 From: Michael Edgar Date: Tue, 22 Oct 2024 14:07:16 -0400 Subject: [PATCH] Exclude deprecated model from coverage calculation, add hashCode tests Signed-off-by: Michael Edgar --- .../openapi/model/BaseModelSupport.java | 6 ++-- .../smallrye/openapi/model/BaseModelTest.java | 32 +++++++++++++++++++ pom.xml | 7 ++-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/model/src/main/java/io/smallrye/openapi/model/BaseModelSupport.java b/model/src/main/java/io/smallrye/openapi/model/BaseModelSupport.java index e78c1d414..c2b526f8f 100644 --- a/model/src/main/java/io/smallrye/openapi/model/BaseModelSupport.java +++ b/model/src/main/java/io/smallrye/openapi/model/BaseModelSupport.java @@ -34,7 +34,7 @@ static int hash(Map stack, BaseModel model) { stack.put(model, model); for (Map.Entry e : model.properties.entrySet()) { - result = 31 * result + (e == null ? 0 : hash(stack, e)); + result = 31 * result + hash(stack, e); } stack.remove(model); @@ -58,12 +58,12 @@ private static int hash(Map stack, Object value) { @SuppressWarnings("unchecked") Map map = (Map) value; for (Map.Entry e : map.entrySet()) { - result = 31 * result + (e == null ? 0 : hash(stack, e)); + result = 31 * result + hash(stack, e); } } else if (value instanceof List) { List list = (List) value; for (Object e : list) { - result = 31 * result + (e == null ? 0 : hash(stack, e)); + result = 31 * result + hash(stack, e); } } else { result = Objects.hash(value); diff --git a/model/src/test/java/io/smallrye/openapi/model/BaseModelTest.java b/model/src/test/java/io/smallrye/openapi/model/BaseModelTest.java index 6c270a502..673f10e75 100644 --- a/model/src/test/java/io/smallrye/openapi/model/BaseModelTest.java +++ b/model/src/test/java/io/smallrye/openapi/model/BaseModelTest.java @@ -2,9 +2,11 @@ import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNull; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.eclipse.microprofile.openapi.models.Constructible; @@ -62,4 +64,34 @@ void removeFromNonMap() { assertEquals("Hello", test.getProperty("test", Object.class)); } + + @Test + void testHashCodeEquality() { + TestMapModel test1 = new TestMapModel(); + test1.setMapProperty("p1", Map.of("k1", List.of("value1"))); + test1.setListProperty("p2", List.of(new TestMapModel())); + test1.setProperty("p3", test1); + + TestMapModel test2 = new TestMapModel(); + test2.setMapProperty("p1", Map.of("k1", List.of("value1"))); + test2.setListProperty("p2", List.of(new TestMapModel())); + test2.setProperty("p3", test2); + + assertEquals(test1.hashCode(), test2.hashCode()); + } + + @Test + void testHashCodeInequality() { + TestMapModel test1 = new TestMapModel(); + test1.setMapProperty("p1", Map.of("k1", List.of("value1"))); + test1.setListProperty("p2", List.of(new TestMapModel())); + test1.setProperty("p3", test1); + + TestMapModel test2 = new TestMapModel(); + test2.setMapProperty("p1", Map.of("k1", List.of("value2"))); + test2.setListProperty("p2", List.of(new TestMapModel())); + test2.setProperty("p3", test2); + + assertNotEquals(test1.hashCode(), test2.hashCode()); + } } diff --git a/pom.xml b/pom.xml index fabb28347..d463b5afd 100644 --- a/pom.xml +++ b/pom.xml @@ -37,8 +37,11 @@ 6.2.3.Final 5.1.2.Final - - testsuite/data/**/*.java + + + testsuite/data/**/*.java, + core/src/main/java/io/smallrye/openapi/api/models/**/*.java +