From ca3d488366e72f6d6dce33266c88a6841996332f Mon Sep 17 00:00:00 2001 From: Leo Date: Mon, 25 Nov 2024 07:23:57 +0900 Subject: [PATCH] Fix toJsonString default mapper serialize type (#1309) * fix toJsonString default mapper serialize type Signed-off-by: Leo * test range query JsonData type value Signed-off-by: Leo * refactor don't invoke the mapper's serialize method for the RangeQuery JsonData raw value Signed-off-by: Leo * update CHANGELOG #1309 PR Signed-off-by: Leo * add JsonDataImpl serialize value type Signed-off-by: Leo --------- Signed-off-by: Leo Signed-off-by: Thomas Farr Co-authored-by: Leo Co-authored-by: Thomas Farr --- CHANGELOG.md | 1 + .../opensearch/client/json/JsonDataImpl.java | 20 ++++++++++++++ .../opensearch/client/json/JsonpUtils.java | 1 - .../opensearch/json/JsonpMapperTest.java | 27 +++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87ad525497..d475c6b861 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ This section is for maintaining a changelog for all breaking changes for the cli ### Fixed - Fixed an issue where `FieldSort` was not implementing `SortOptionsVariant` ([#1323](https://github.com/opensearch-project/opensearch-java/pull/1323)) +- Fixed don't invoke the mapper's serialize method for the RangeQuery JsonData raw value [#1309](https://github.com/opensearch-project/opensearch-java/pull/1309) ### Security diff --git a/java-client/src/main/java/org/opensearch/client/json/JsonDataImpl.java b/java-client/src/main/java/org/opensearch/client/json/JsonDataImpl.java index 72c9e0235b..e0e97be8b0 100644 --- a/java-client/src/main/java/org/opensearch/client/json/JsonDataImpl.java +++ b/java-client/src/main/java/org/opensearch/client/json/JsonDataImpl.java @@ -37,6 +37,8 @@ import jakarta.json.stream.JsonParser; import java.io.StringReader; import java.io.StringWriter; +import java.math.BigDecimal; +import java.math.BigInteger; class JsonDataImpl implements JsonData { private final Object value; @@ -109,6 +111,24 @@ public T deserialize(JsonpDeserializer deserializer, JsonpMapper mapper) public void serialize(JsonGenerator generator, JsonpMapper mapper) { if (value instanceof JsonValue) { generator.write((JsonValue) value); + } else if (value instanceof String) { + generator.write((String) value); + } else if (value instanceof BigDecimal) { + generator.write((BigDecimal) value); + } else if (value instanceof BigInteger) { + generator.write((BigInteger) value); + } else if (value instanceof Short) { + generator.write((Short) value); + } else if (value instanceof Integer) { + generator.write((Integer) value); + } else if (value instanceof Long) { + generator.write((Long) value); + } else if (value instanceof Float) { + generator.write((Float) value); + } else if (value instanceof Double) { + generator.write((Double) value); + } else if (value instanceof Boolean) { + generator.write((Boolean) value); } else { // Mapper provided at creation time has precedence (this.mapper != null ? this.mapper : mapper).serialize(value, generator); diff --git a/java-client/src/main/java/org/opensearch/client/json/JsonpUtils.java b/java-client/src/main/java/org/opensearch/client/json/JsonpUtils.java index d9cfdaf306..8cf51df787 100644 --- a/java-client/src/main/java/org/opensearch/client/json/JsonpUtils.java +++ b/java-client/src/main/java/org/opensearch/client/json/JsonpUtils.java @@ -80,7 +80,6 @@ public void serialize(T value, JsonGenerator generator) { ((JsonpSerializable) value).serialize(generator, this); return; } - throw new JsonException( "Cannot find a serializer for type " + value.getClass().getName() + ". Consider using a full-featured JsonpMapper." ); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonpMapperTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonpMapperTest.java index 256eb9b81f..1031c19e27 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonpMapperTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonpMapperTest.java @@ -51,11 +51,13 @@ import java.util.concurrent.atomic.AtomicInteger; import org.junit.Assert; import org.junit.Test; +import org.opensearch.client.json.JsonData; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.json.jsonb.JsonbJsonpMapper; import org.opensearch.client.opensearch.IOUtils; +import org.opensearch.client.opensearch.core.SearchRequest; import org.opensearch.client.opensearch.model.ModelTestCase; public class JsonpMapperTest extends Assert { @@ -240,4 +242,29 @@ public void setChildren(List children) { this.children = children; } } + + @Test + public void testRangeQuery() { + + String expectedStringValue = + "{\"aggregations\":{},\"query\":{\"range\":{\"rangeField\":{\"gte\":10.5,\"lte\":30,\"from\":\"2024-01-01T00:00:00Z\",\"format\":\"strict_date_optional_time\"}}},\"terminate_after\":5}"; + + SearchRequest searchRequest = SearchRequest.of( + request -> request.index("index1", "index2") + .aggregations(Collections.emptyMap()) + .terminateAfter(5L) + .query( + q -> q.range( + r -> r.field("rangeField") + .gte(JsonData.of(10.5)) + .lte(JsonData.of(30)) + .from(JsonData.of("2024-01-01T00:00:00Z")) + .format("strict_date_optional_time") + ) + ) + ); + String searchRequestString = searchRequest.toJsonString(); + assertEquals(expectedStringValue, searchRequestString); + } + }