GH-1065 Fix Collection discovery in JsonMapper

Resolves #1065
This commit is contained in:
Oleg Zhurakousky
2023-08-30 15:22:52 +02:00
parent fb44a7064c
commit c1216da69c
3 changed files with 26 additions and 2 deletions

View File

@@ -115,7 +115,7 @@ public abstract class JsonMapper {
public static boolean isJsonStringRepresentsCollection(Object value) {
boolean isJson = false;
if (value instanceof Iterable && !value.getClass().getPackage().getName().startsWith("reactor.util.function")) {
if (value instanceof Collection && !value.getClass().getPackage().getName().startsWith("reactor.util.function")) {
return true;
}
if (value instanceof byte[]) {

View File

@@ -20,7 +20,10 @@ import java.util.List;
import java.util.stream.Stream;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.Gson;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
@@ -42,6 +45,28 @@ public class JsonMapperTests {
return Stream.of(new GsonMapper(new Gson()), new JacksonMapper(new ObjectMapper()));
}
@Test
public void objectNode_isJsonStringRepresentsCollection() {
ObjectNode node = JsonNodeFactory.instance.objectNode();
node.put("id", "1234ab");
node.put("foo", "bar");
/*
* Passing the ObjectNode directly results in a positive identification as
* a collection, as its distant parent JsonNode implements Iterable.
*/
assertThat(JsonMapper.isJsonStringRepresentsCollection(node)).isFalse();
String nodeAsString = node.toString();
/*
* Sending the node as a string returns false, however, as the line
* isJsonString(value) && str.startsWith("[") && str.endsWith("]")
* will not be true.
*/
assertThat(JsonMapper.isJsonStringRepresentsCollection(nodeAsString)).isFalse();
}
@ParameterizedTest
@MethodSource("params")
public void vanillaArray(JsonMapper mapper) {