Skip to content

Commit

Permalink
Sort field in Hit deserializer should handle null events (#169)
Browse files Browse the repository at this point in the history
Signed-off-by: Rene Cordier <rcordier@linagora.com>
  • Loading branch information
Arsnael authored Jun 23, 2022
1 parent 09b02f6 commit 3335c98
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ static JsonpDeserializer<Integer> intOrNullDeserializer(int defaultValue) {
return new JsonpDeserializerBase.IntOrNullDeserializer(defaultValue);
}

static JsonpDeserializer<String> stringOrNullDeserializer() {
return new JsonpDeserializerBase.StringOrNullDeserializer();
}

static JsonpDeserializer<Number> numberDeserializer() {
return JsonpDeserializerBase.NUMBER;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,30 @@ public Integer deserialize(JsonParser parser, JsonpMapper mapper, Event event) {
}
}

static final class StringOrNullDeserializer extends JsonpDeserializerBase<String> {
static final EnumSet<Event> nativeEvents = EnumSet.of(Event.VALUE_STRING, Event.VALUE_NULL);
static final EnumSet<Event> acceptedEvents = EnumSet.of(Event.KEY_NAME, Event.VALUE_STRING,
Event.VALUE_NUMBER, Event.VALUE_FALSE, Event.VALUE_TRUE, Event.VALUE_NULL);

StringOrNullDeserializer() {
super(acceptedEvents, nativeEvents);
}

@Override
public String deserialize(JsonParser parser, JsonpMapper mapper, Event event) {
if (event == Event.VALUE_NULL) {
return null;
}
if (event == Event.VALUE_TRUE) {
return "true";
}
if (event == Event.VALUE_FALSE) {
return "false";
}
return parser.getString();
}
}

static final JsonpDeserializer<Double> DOUBLE_OR_NAN =
new JsonpDeserializerBase<Double>(
EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_STRING, Event.VALUE_NULL),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ protected static <TDocument> void setupHitDeserializer(ObjectDeserializer<Hit.Bu
op.add(Builder::seqNo, JsonpDeserializer.longDeserializer(), "_seq_no");
op.add(Builder::primaryTerm, JsonpDeserializer.longDeserializer(), "_primary_term");
op.add(Builder::version, JsonpDeserializer.longDeserializer(), "_version");
op.add(Builder::sort, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringDeserializer()), "sort");
op.add(Builder::sort, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringOrNullDeserializer()), "sort");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

package org.opensearch.client.opensearch.model;

import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch._types.SortOptions;
import org.opensearch.client.opensearch._types.SortOrder;
Expand All @@ -42,8 +43,12 @@
import org.opensearch.client.opensearch.indices.IndexSettings;
import org.junit.Test;

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

import jakarta.json.stream.JsonParser;

public class BuiltinTypesTest extends ModelTestCase {

@Test
Expand Down Expand Up @@ -229,4 +234,21 @@ public void testNullableInt() {
assertEquals(0, stats.maxLength());
assertEquals(0.0, stats.entropy(), 0.01);
}

@Test
public void testNullableStringInArray() {
// stringOrNullDeserializer allows to handle null events in string arrays
String json = "[\"lettuce\", null, \"tomato\"]";
JsonParser jsonParser = mapper.jsonProvider().createParser(new StringReader(json));
JsonpDeserializer<String> stringDeserializer = JsonpDeserializer.stringOrNullDeserializer();

List<String> result = JsonpDeserializer.arrayDeserializer(stringDeserializer).deserialize(jsonParser, mapper);

List<String> expected = new ArrayList<>();
expected.add("lettuce");
expected.add(null);
expected.add("tomato");

assertEquals(result, expected);
}
}

0 comments on commit 3335c98

Please sign in to comment.