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>
(cherry picked from commit e672fdc)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed May 13, 2024
1 parent a067f67 commit caa3d5b
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 @@ -14,6 +14,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### 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 caa3d5b

Please sign in to comment.