opensearch-java icon indicating copy to clipboard operation
opensearch-java copied to clipboard

[BUG] Minimal IndexRequest fails to serialize to JSON

Open bcalmac opened this issue 10 months ago • 1 comments

What is the bug?

IndexRequest.toJsonString() fails with JsonException.

How can one reproduce the bug?

Consider this minimal JUnit 5 test:

package opensearchtest;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
import org.opensearch.client.opensearch.core.IndexRequest;

public class OpenSearchSerializationTest {
    @Test
    void indexRequestToJson() {
        IndexRequest<Object> indexRequest = IndexRequest.of(i -> i
                .index("index")
                .document("document"));
        assertNotNull(indexRequest.toJsonString());
    }
}

It fails with:

jakarta.json.JsonException: Cannot find a serializer for type java.lang.String. Consider using a full-featured JsonpMapper.
	at org.opensearch.client.json.JsonpUtils$1.serialize(JsonpUtils.java:84)
	at org.opensearch.client.json.JsonpUtils.serialize(JsonpUtils.java:158)
	at org.opensearch.client.opensearch.core.IndexRequest.serialize(IndexRequest.java:288)
	at org.opensearch.client.json.PlainJsonSerializable.toJsonString(PlainJsonSerializable.java:29)
	at opensearchtest.OpenSearchSerializationTest.indexRequestToJson(OpenSearchSerializationTest.java:16)

What is your host/environment?

Java 21, opensearch-java 2.22.0

Do you have any additional context?

This is an isolated test, but in the context of our application we use toJsonString() to log the content of various requests and responses. I can probably work around with a custom serialization utility, but I was expecting this to "just work". Why expose it otherwise?

bcalmac avatar Mar 28 '25 00:03 bcalmac

Another case showing the limitation of the default mapper, making SearchRequest#toJsonString unusable:

    @Test
    public void testHistAgg() {
        HistogramAggregation.Builder builder = AggregationBuilders.histogram();
        builder.field("field").interval(1024.0).extendedBounds(ExtendedBounds.of(o -> o.min(0.0).max(4096.0)));
        SearchRequest searchRequest = SearchRequest.of(
                request -> request.index("index1").aggregations(Map.of("myagg", new Aggregation(builder.build()))));

        // throws jakarta.json.JsonException
        String searchRequestString = searchRequest.toJsonString();
        assertEquals("{...}", searchRequestString);
    }

This is throwing:

jakarta.json.JsonException: Cannot find a serializer for type java.lang.Double. Consider using a full-featured JsonpMapper.

	at org.opensearch.client.json.JsonpUtils$1.serialize(JsonpUtils.java:84)
	at org.opensearch.client.json.JsonpUtils.serialize(JsonpUtils.java:158)
	at org.opensearch.client.opensearch._types.aggregations.ExtendedBounds.serializeInternal(ExtendedBounds.java:100)
	at org.opensearch.client.opensearch._types.aggregations.ExtendedBounds.serialize(ExtendedBounds.java:93)
	at org.opensearch.client.opensearch._types.aggregations.HistogramAggregation.serializeInternal(HistogramAggregation.java:207)

bdelbosc avatar May 21 '25 17:05 bdelbosc