Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JsonArray contains for non-wrapped JsonArray/JsonObject #5414

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions src/main/java/io/vertx/core/json/JsonArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,47 @@ public JsonArray set(int pos, Object value) {
}

/**
* Does the JSON array contain the specified value? This method will scan the entire array until it finds a value
* or reaches the end.
* Returns {@code true} if this JSON Array contains the specified value.
* More formally, returns {@code true} if and only if this JSON array contains
* at least one entry {@code entry} such that {@code Objects.equals(value, entry)}.
*
* @param value the value
* @param value the value whose presence in this JSON array is to be tested
* @return true if it contains the value, false if not
*/
public boolean contains(Object value) {
return list.contains(value);
return indexOf(value) >= 0;
}

/**
* Returns the index of the last occurrence of the specified value
* in this JSON array, or -1 if this JSON array does not contain the value.
* More formally, returns the highest index {@code i} such that
* {@code Objects.equals(value, get(i))},
* or -1 if there is no such index.
*
* @param value the value whose index in this JSON array is to be returned
* @return the index of the value in the array, or -1 if the value is not in the array
*/
public int indexOf(Object value) {
// in case of JsonObject/JsonArray, the list might still contain an unwrapped Map/List, we need to check for both
if (value instanceof JsonObject) {
return indexOfFirst(value, ((JsonObject) value).getMap());
} else if (value instanceof JsonArray) {
return indexOfFirst(value, ((JsonArray) value).getList());
} else {
return list.indexOf(value);
}
}

private int indexOfFirst(Object value, Object value2) {
for (int i = 0; i < list.size(); i++) {
Object entry = list.get(i);
if (value.equals(entry) || value2.equals(entry)) {
return i;
}
}

return -1;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/io/vertx/core/json/JsonArrayTest.java
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(Collections.singletonMap("foo", "bar"));
jsonArray.add(Collections.singletonList("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
Loading