opensearch-java: [BUG] Unable to deserialize SearchRequest with _source field as an array

What is the bug?

The error org.opensearch.client.json.UnexpectedJsonEventException: Unexpected JSON event 'START_ARRAY' instead of '[START_OBJECT, KEY_NAME, VALUE_STRING, VALUE_TRUE, VALUE_FALSE] is raised when trying to deserialize following json payload to SearchRequest.

{
  "query": {
    "match_all": {}
  },
  "_source": ["_id"],
  "size": 1
}

Context: We are implementing a kind of proxy service which accepts any OpenSearch requests and overwrite some query logic before passing them to the OpenSearch cluster. Previously, we use opensearch-rest-high-level-client with XContentParser to parse incoming requests but now we would like to migrate to opensearch-java library.

Note that if _source is removed or change to string field, it can parse successfully but we cannot do this as a workaround as we expect to accept any form of valid requests from clients.

How can one reproduce the bug?

Execute following code and then will get org.opensearch.client.json.UnexpectedJsonEventException

import com.fasterxml.jackson.core.JsonFactory;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;
import org.opensearch.client.json.jackson.JacksonJsonpParser;
import org.opensearch.client.opensearch.core.SearchRequest;

import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException {
        String jsonPayload = "{\n" +
                "  \"query\": {\n" +
                "    \"match_all\": {}\n" +
                "  },\n" +
                "  \"_source\": [\"_id\"],\n" +
                "  \"size\": 1\n" +
                "}\n";

        SearchRequest searchRequest = SearchRequest._DESERIALIZER.deserialize(new JacksonJsonpParser(new JsonFactory().createParser(jsonPayload)), new JacksonJsonpMapper());
    }
}

What is the expected behavior?

We expect SearchRequest._DESERIALIZER can parse the json payload successfully.

What is your host/environment?

  • library = opensearch-java 2.11.1
  • SDK = Amazon Correto 11
  • OS = MacOS Somona 14.5

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (4 by maintainers)

Most upvoted comments

Have found the issue (and made a fix in #1117), the actual typings in SearchRequest were correct (so generator wouldn’t have changed anything), this was actually an issue in the general deserialization logic of how we handle “shortcut” properties on objects.

ie. The real type is:

{
  "includes": ["_id"],
  "excludes": []
}

But we accept specifying just "_id" or ["_id"] as shortcutting to the includes property. The code was only handling the string case and not the array case.

Have made #1102 to fix the code generator error