diff --git a/CHANGELOG.md b/CHANGELOG.md index 36175f5200..10daf54eec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ This section is for maintaining a changelog for all breaking changes for the cli ### Removed ### Fixed +- Fix the deserialization of SortOptions ([#981](https://github.com/opensearch-project/opensearch-java/pull/981)) ### Security diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/SortOptions.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/SortOptions.java index 163f320c0e..a51d46c345 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/SortOptions.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/SortOptions.java @@ -281,10 +281,9 @@ public SortOptions build() { } public static final JsonpDeserializer _DESERIALIZER = JsonpDeserializer.lazy( - () -> JsonpDeserializer.of(EnumSet.of(JsonParser.Event.START_OBJECT, JsonParser.Event.VALUE_STRING), (parser, mapper) -> { + () -> JsonpDeserializer.of(EnumSet.of(JsonParser.Event.START_OBJECT, JsonParser.Event.VALUE_STRING), (parser, mapper, event) -> { SortOptions.Builder b = new SortOptions.Builder(); - JsonParser.Event event = parser.next(); if (event == JsonParser.Event.VALUE_STRING) { switch (parser.getString()) { case "_score": diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/_types/SortOptionsTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/_types/SortOptionsTest.java new file mode 100644 index 0000000000..ed2584b12c --- /dev/null +++ b/java-client/src/test/java/org/opensearch/client/opensearch/_types/SortOptionsTest.java @@ -0,0 +1,28 @@ +package org.opensearch.client.opensearch._types; + +import static org.junit.Assert.assertEquals; + +import jakarta.json.stream.JsonParser; +import java.io.StringReader; +import java.util.List; +import org.junit.Test; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.jackson.JacksonJsonpMapper; + +public class SortOptionsTest { + + @Test + public void testSortOptions() { + String jsonString = "[{\"entityId\":{\"order\":\"asc\"}}]"; + StringReader reader = new StringReader(jsonString); + JacksonJsonpMapper mapper = new JacksonJsonpMapper(); + JsonParser parser = mapper.jsonProvider().createParser(reader); + + List sortOptions = JsonpDeserializer.arrayDeserializer(SortOptions._DESERIALIZER).deserialize(parser, mapper); + assertEquals(1, sortOptions.size()); + assertEquals(SortOptions.Kind.Field, sortOptions.get(0)._kind()); + FieldSort fieldSort = (FieldSort) sortOptions.get(0)._get(); + assertEquals("entityId", fieldSort.field()); + assertEquals(SortOrder.Asc, fieldSort.order()); + } +}