Skip to content

Commit

Permalink
Fix JsonArray contains for non-wrapped JsonArray/JsonObject
Browse files Browse the repository at this point in the history
  • Loading branch information
kristian committed Nov 19, 2024
1 parent 14e9377 commit fdecd4f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 11 additions & 1 deletion vertx-core/src/main/java/io/vertx/core/json/JsonArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Expand Down

0 comments on commit fdecd4f

Please sign in to comment.