Skip to content

Commit

Permalink
Accept null values in arrays (#68) (#70)
Browse files Browse the repository at this point in the history
Co-authored-by: Sylvain Wallez <sylvain@elastic.co>
  • Loading branch information
github-actions[bot] and swallez authored Dec 16, 2021
1 parent 2490527 commit 8422928
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,13 @@ public List<T> deserialize(JsonParser parser, JsonpMapper mapper, Event event) {
if (event == Event.START_ARRAY) {
List<T> result = new ArrayList<>();
while ((event = parser.next()) != Event.END_ARRAY) {
JsonpUtils.ensureAccepts(itemDeserializer, parser, event);
result.add(itemDeserializer.deserialize(parser, mapper, event));
// JSON null: add null unless the deserializer can handle it
if (event == Event.VALUE_NULL && !itemDeserializer.accepts(event)) {
result.add(null);
} else {
JsonpUtils.ensureAccepts(itemDeserializer, parser, event);
result.add(itemDeserializer.deserialize(parser, mapper, event));
}
}
return result;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
import co.elastic.clients.elasticsearch._types.query_dsl.SpanGapQuery;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.indices.IndexSettings;
import co.elastic.clients.json.JsonpDeserializer;
import jakarta.json.stream.JsonParser;
import org.junit.Test;

import java.io.StringReader;
import java.util.List;

public class BuiltinTypesTest extends ModelTestCase {
Expand All @@ -50,6 +53,38 @@ public void testLenientArray() {
assertGetterType(List.class, SearchRequest.class, "index");
}

@Test
public void testNullArrayItem() {
// See https://github.com/elastic/elasticsearch-java/issues/66

String json = "[\"a\", null, \"c\"]";

// Types that don't accept null events should end up as null values in the list
{
JsonpDeserializer<String> stringDeser = JsonpDeserializer.stringDeserializer();
assertFalse(stringDeser.accepts(JsonParser.Event.VALUE_NULL));

JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json));

List<String> stringList = JsonpDeserializer.arrayDeserializer(stringDeser).deserialize(parser, mapper);
assertEquals("a", stringList.get(0));
assertNull(stringList.get(1));
assertEquals("c", stringList.get(2));
}

// Types that do accept null events should end up as their null representation
{
assertTrue(FieldValue._DESERIALIZER.accepts(JsonParser.Event.VALUE_NULL));

JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json));
List<FieldValue> valueList = JsonpDeserializer.arrayDeserializer(FieldValue._DESERIALIZER).deserialize(parser, mapper);

assertEquals("a", valueList.get(0)._get());
assertTrue(valueList.get(1).isNull());
assertEquals("c", valueList.get(2)._get());
}
}

@Test
public void testSpanGapQuery() {
// Hand-written class
Expand Down

0 comments on commit 8422928

Please sign in to comment.