opensearch-project / opensearch-project/opensearch-java

When deserializing to SearchResponse<JsonData> getting error for suggest field

Open
#1,024 16 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Java
Stars
165
Forks
250
Avg merge
1d 18h
Merged PRs (30d)
26

Description

What is the bug?

While trying to deserialize this response I am getting StringOutofBoundException
Response -
{"took":4,"timed_out":false,"_shards":{"failed":0.0,"successful":1.0,"total":1.0,"skipped":0.0},"hits":{"total":{"relation":"eq","value":0},"hits":[]},"aggregations":{"cardinality#itemCount":{"value":0}},"suggest":{"correction":[{"length":5,"offset":0,"text":"talbe","options":[{"text":"table","freq":22,"score":0.8}]}]}}

Exception -
java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 10
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4608)
at java.base/java.lang.String.substring(String.java:2711)
at org.opensearch.client.json.ExternallyTaggedUnion.lambda$arrayDeserializer$0(ExternallyTaggedUnion.java:152)
at org.opensearch.client.json.JsonpDeserializer$3.deserialize(JsonpDeserializer.java:138)
at org.opensearch.client.json.JsonpDeserializer.deserialize(JsonpDeserializer.java:87)
at org.opensearch.client.json.ObjectDeserializer$FieldObjectDeserializer.deserialize(ObjectDeserializer.java:81)
at org.opensearch.client.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:185)
at org.opensearch.client.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:146)
at org.opensearch.client.json.JsonpDeserializer.deserialize(JsonpDeserializer.java:87)
at org.opensearch.client.json.ObjectBuilderDeserializer.deserialize(ObjectBuilderDeserializer.java:91)

Method to deserialize -

private static final SearchResponse deserializeSearchResponse(String astrJson) throws IOException {
		SearchResponse<JsonData> searchResponseCopy = null;
		JsonFactory jsonFactory = new JsonFactory();
		JsonParser jsonParser = jsonFactory.createParser(astrJson);
		JacksonJsonpParser jsonpParser = new JacksonJsonpParser(jsonParser);
		try {
			JsonpDeserializer<JsonData> jsonpDeserializer = JsonpDeserializer.of(JsonData.class);
		       JsonpDeserializer<SearchResponse<JsonData>> searchResponseDeserializer = SearchResponse.createSearchResponseDeserializer(jsonpDeserializer);
			searchResponseCopy = searchResponseDeserializer.deserialize(jsonpParser, new JacksonJsonpMapper());
		} catch (Exception ex) {
			LOGGER.warn("Failed to deserialize {}", astrJson, ex);
			throw ex;
		}
		return searchResponseCopy;
}

**Method used to serialize - **

private static final String serializeSearchRequest(SearchRequest searchRequest)
			throws IOException {
		String strJson = null;
		try {
			JacksonJsonpMapper jsonpMapper = new JacksonJsonpMapper();

			StringWriter writer = new StringWriter();
			JacksonJsonpGenerator generator = new JacksonJsonpGenerator(new JsonFactory().createGenerator(writer));

			searchRequest.serialize(generator, jsonpMapper);
			generator.flush();
			strJson = writer.toString();
		} catch (Exception ex) {
			LOGGER.warn("Failed to serialize {}", searchRequest, ex);
			throw ex;
		}
		return strJson;
	} 
How can one reproduce the bug?

Create a request with suggester, serialize the response and then deserialize it.

What is the expected behavior?

No Error and get deserialized response

What is your host/environment?

Operating system, version.

Do you have any screenshots?

If applicable, add screenshots to help explain your problem.

Do you have any additional context?

Java client version - 2.10.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with ExternallyTaggedUnion.arrayDeserializer, the stack-trace entry involved in deserializing the suggest field, and reproduce using the provided SearchResponse JSON and deserializeSearchResponse method. Done means a response containing suggest.correction deserializes without StringIndexOutOfBoundsException and returns the expected SearchResponse.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.