From fde17383543bcc9c1aaa67eb80411fb6f3c9127b Mon Sep 17 00:00:00 2001 From: bowenlan-amzn Date: Fri, 10 May 2024 09:44:07 -0700 Subject: [PATCH 1/3] Fix the deserialization of SortOptions Signed-off-by: bowenlan-amzn --- .../client/opensearch/_types/SortOptions.java | 3 +- .../opensearch/_types/SortOptionsTest.java | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 java-client/src/test/java/org/opensearch/client/opensearch/_types/SortOptionsTest.java 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..edb3f4a6cc --- /dev/null +++ b/java-client/src/test/java/org/opensearch/client/opensearch/_types/SortOptionsTest.java @@ -0,0 +1,33 @@ +package org.opensearch.client.opensearch._types; + +import jakarta.json.stream.JsonParser; +import org.junit.Test; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.jackson.JacksonJsonpMapper; + +import java.io.StringReader; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class SortOptionsTest { + + @Test + public void testSortOptions() { + String jsonString = "[{\n" + + " \"entityId\": {\n" + + " \"order\": \"asc\"\n" + + " }\n" + + " }]"; + 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()); + } +} From 491f57483ce75e14e422b52341c01487422a0487 Mon Sep 17 00:00:00 2001 From: "Signed-off-by: bowenlan-amzn" Date: Fri, 10 May 2024 09:49:01 -0700 Subject: [PATCH 2/3] add changelog Signed-off-by: bowenlan-amzn --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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 From fe9ee6de07842fb9a8bc1df9945ab22fa59897df Mon Sep 17 00:00:00 2001 From: bowenlan-amzn Date: Fri, 10 May 2024 09:56:29 -0700 Subject: [PATCH 3/3] spotless Signed-off-by: bowenlan-amzn --- .../client/opensearch/_types/SortOptionsTest.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) 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 index edb3f4a6cc..ed2584b12c 100644 --- 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 @@ -1,24 +1,19 @@ 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; -import java.io.StringReader; -import java.util.List; - -import static org.junit.Assert.assertEquals; - public class SortOptionsTest { @Test public void testSortOptions() { - String jsonString = "[{\n" + - " \"entityId\": {\n" + - " \"order\": \"asc\"\n" + - " }\n" + - " }]"; + String jsonString = "[{\"entityId\":{\"order\":\"asc\"}}]"; StringReader reader = new StringReader(jsonString); JacksonJsonpMapper mapper = new JacksonJsonpMapper(); JsonParser parser = mapper.jsonProvider().createParser(reader);