From c753b82960bf3a1d2db6dd992dbaa6d3e10c64aa Mon Sep 17 00:00:00 2001 From: Leo Date: Wed, 20 Nov 2024 01:54:44 +0900 Subject: [PATCH] refactor don't invoke the mapper's serialize method for the RangeQuery JsonData raw value Signed-off-by: Leo --- .../opensearch/client/json/JsonDataImpl.java | 6 ++++ .../opensearch/client/json/JsonpUtils.java | 28 ++++--------------- .../opensearch/json/JsonpMapperTest.java | 26 ++++++++--------- 3 files changed, 25 insertions(+), 35 deletions(-) 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..0ab2342d78 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 @@ -109,6 +109,12 @@ 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 Integer) { + generator.write((Integer) value); + } else if (value instanceof Double) { + generator.write((Double) 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 4f1e724d5e..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,31 +80,15 @@ public void serialize(T value, JsonGenerator generator) { ((JsonpSerializable) value).serialize(generator, this); return; } - - if (value instanceof String) { - generator.write((String) value); - return; - } - - if (value instanceof Integer) { - generator.write((Integer) value); - return; - } - - if (value instanceof Double) { - generator.write((Double) value); - return; - } - throw new JsonException( - "Cannot find a serializer for type " + value.getClass().getName() + ". Consider using a full-featured JsonpMapper." + "Cannot find a serializer for type " + value.getClass().getName() + ". Consider using a full-featured JsonpMapper." ); } @Override protected JsonpDeserializer getDefaultDeserializer(Class clazz) { throw new JsonException( - "Cannot find a default deserializer for type " + clazz.getName() + ". Consider using a full-featured JsonpMapper." + "Cannot find a default deserializer for type " + clazz.getName() + ". Consider using a full-featured JsonpMapper." ); } }; @@ -183,10 +167,10 @@ public static void serialize(T value, JsonGenerator generator, @Nullable Jso * (the object has been consumed from the original one). */ public static Map.Entry lookAheadFieldValue( - String name, - String defaultValue, - JsonParser parser, - JsonpMapper mapper + String name, + String defaultValue, + JsonParser parser, + JsonpMapper mapper ) { JsonLocation location = parser.getLocation(); 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 c36bcfaf9d..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 @@ -247,21 +247,21 @@ public void setChildren(List children) { 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}"; + "{\"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") - ) - ) + 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);