Skip to content

Commit

Permalink
Fix the deserialization of SortOptions (#981)
Browse files Browse the repository at this point in the history
* Fix the deserialization of SortOptions

Signed-off-by: bowenlan-amzn <bowenlan23@gmail.com>

* add changelog

Signed-off-by: bowenlan-amzn <bowenlan23@gmail.com>

* spotless

Signed-off-by: bowenlan-amzn <bowenlan23@gmail.com>

---------

Signed-off-by: bowenlan-amzn <bowenlan23@gmail.com>
  • Loading branch information
bowenlan-amzn authored May 13, 2024
1 parent d092df5 commit e672fdc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,9 @@ public SortOptions build() {
}

public static final JsonpDeserializer<SortOptions> _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":
Expand Down
Original file line number Diff line number Diff line change
@@ -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> 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());
}
}

0 comments on commit e672fdc

Please sign in to comment.