From fdecd4f5eaf14431aba707f21fb67a31c3a8f9a7 Mon Sep 17 00:00:00 2001 From: Kristian Kraljic Date: Tue, 19 Nov 2024 16:55:12 +0100 Subject: [PATCH] Fix JsonArray contains for non-wrapped JsonArray/JsonObject --- .../src/main/java/io/vertx/core/json/JsonArray.java | 12 +++++++++++- .../test/java/io/vertx/tests/json/JsonArrayTest.java | 4 ++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/vertx-core/src/main/java/io/vertx/core/json/JsonArray.java b/vertx-core/src/main/java/io/vertx/core/json/JsonArray.java index ddba7e134ef..ea5e86611e6 100644 --- a/vertx-core/src/main/java/io/vertx/core/json/JsonArray.java +++ b/vertx-core/src/main/java/io/vertx/core/json/JsonArray.java @@ -456,7 +456,17 @@ public JsonArray set(int pos, Object value) { * @return true if it contains the value, false if not */ public boolean contains(Object value) { - return list.contains(value); + if (list.contains(value)) { + return true; + } + + if (value instanceof JsonObject) { + return list.contains(((JsonObject) value).getMap()); + } else if (value instanceof JsonArray) { + return list.contains(((JsonArray) value).getList()); + } + + return false; } /** diff --git a/vertx-core/src/test/java/io/vertx/tests/json/JsonArrayTest.java b/vertx-core/src/test/java/io/vertx/tests/json/JsonArrayTest.java index f7277d3e8ba..bada9d5433a 100644 --- a/vertx-core/src/test/java/io/vertx/tests/json/JsonArrayTest.java +++ b/vertx-core/src/test/java/io/vertx/tests/json/JsonArrayTest.java @@ -658,6 +658,8 @@ public void testContains() { JsonArray arr = new JsonArray(); jsonArray.add(obj); jsonArray.add(arr); + jsonArray.add(Map.of("foo", "bar")); + jsonArray.add(List.of("baz")); assertFalse(jsonArray.contains("eek")); assertFalse(jsonArray.contains(false)); assertFalse(jsonArray.contains(321)); @@ -668,6 +670,8 @@ public void testContains() { assertTrue(jsonArray.contains(123)); assertTrue(jsonArray.contains(obj)); assertTrue(jsonArray.contains(arr)); + assertTrue(jsonArray.contains(new JsonObject().put("foo", "bar"))); + assertTrue(jsonArray.contains(new JsonArray().add("baz"))); } @Test