Skip to content

Commit ca3d488

Browse files
playLeoLeoXtansia
authored
Fix toJsonString default mapper serialize type (#1309)
* fix toJsonString default mapper serialize type Signed-off-by: Leo <[email protected]> * test range query JsonData type value Signed-off-by: Leo <[email protected]> * refactor don't invoke the mapper's serialize method for the RangeQuery JsonData raw value Signed-off-by: Leo <[email protected]> * update CHANGELOG #1309 PR Signed-off-by: Leo <[email protected]> * add JsonDataImpl serialize value type Signed-off-by: Leo <[email protected]> --------- Signed-off-by: Leo <[email protected]> Signed-off-by: Thomas Farr <[email protected]> Co-authored-by: Leo <[email protected]> Co-authored-by: Thomas Farr <[email protected]>
1 parent f9540f7 commit ca3d488

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
5252

5353
### Fixed
5454
- Fixed an issue where `FieldSort` was not implementing `SortOptionsVariant` ([#1323](https://github.com/opensearch-project/opensearch-java/pull/1323))
55+
- 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)
5556

5657
### Security
5758

java-client/src/main/java/org/opensearch/client/json/JsonDataImpl.java

+20
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import jakarta.json.stream.JsonParser;
3838
import java.io.StringReader;
3939
import java.io.StringWriter;
40+
import java.math.BigDecimal;
41+
import java.math.BigInteger;
4042

4143
class JsonDataImpl implements JsonData {
4244
private final Object value;
@@ -109,6 +111,24 @@ public <T> T deserialize(JsonpDeserializer<T> deserializer, JsonpMapper mapper)
109111
public void serialize(JsonGenerator generator, JsonpMapper mapper) {
110112
if (value instanceof JsonValue) {
111113
generator.write((JsonValue) value);
114+
} else if (value instanceof String) {
115+
generator.write((String) value);
116+
} else if (value instanceof BigDecimal) {
117+
generator.write((BigDecimal) value);
118+
} else if (value instanceof BigInteger) {
119+
generator.write((BigInteger) value);
120+
} else if (value instanceof Short) {
121+
generator.write((Short) value);
122+
} else if (value instanceof Integer) {
123+
generator.write((Integer) value);
124+
} else if (value instanceof Long) {
125+
generator.write((Long) value);
126+
} else if (value instanceof Float) {
127+
generator.write((Float) value);
128+
} else if (value instanceof Double) {
129+
generator.write((Double) value);
130+
} else if (value instanceof Boolean) {
131+
generator.write((Boolean) value);
112132
} else {
113133
// Mapper provided at creation time has precedence
114134
(this.mapper != null ? this.mapper : mapper).serialize(value, generator);

java-client/src/main/java/org/opensearch/client/json/JsonpUtils.java

-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public <T> void serialize(T value, JsonGenerator generator) {
8080
((JsonpSerializable) value).serialize(generator, this);
8181
return;
8282
}
83-
8483
throw new JsonException(
8584
"Cannot find a serializer for type " + value.getClass().getName() + ". Consider using a full-featured JsonpMapper."
8685
);

java-client/src/test/java/org/opensearch/client/opensearch/json/JsonpMapperTest.java

+27
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@
5151
import java.util.concurrent.atomic.AtomicInteger;
5252
import org.junit.Assert;
5353
import org.junit.Test;
54+
import org.opensearch.client.json.JsonData;
5455
import org.opensearch.client.json.JsonpDeserializer;
5556
import org.opensearch.client.json.JsonpMapper;
5657
import org.opensearch.client.json.jackson.JacksonJsonpMapper;
5758
import org.opensearch.client.json.jsonb.JsonbJsonpMapper;
5859
import org.opensearch.client.opensearch.IOUtils;
60+
import org.opensearch.client.opensearch.core.SearchRequest;
5961
import org.opensearch.client.opensearch.model.ModelTestCase;
6062

6163
public class JsonpMapperTest extends Assert {
@@ -240,4 +242,29 @@ public void setChildren(List<SomeClass> children) {
240242
this.children = children;
241243
}
242244
}
245+
246+
@Test
247+
public void testRangeQuery() {
248+
249+
String expectedStringValue =
250+
"{\"aggregations\":{},\"query\":{\"range\":{\"rangeField\":{\"gte\":10.5,\"lte\":30,\"from\":\"2024-01-01T00:00:00Z\",\"format\":\"strict_date_optional_time\"}}},\"terminate_after\":5}";
251+
252+
SearchRequest searchRequest = SearchRequest.of(
253+
request -> request.index("index1", "index2")
254+
.aggregations(Collections.emptyMap())
255+
.terminateAfter(5L)
256+
.query(
257+
q -> q.range(
258+
r -> r.field("rangeField")
259+
.gte(JsonData.of(10.5))
260+
.lte(JsonData.of(30))
261+
.from(JsonData.of("2024-01-01T00:00:00Z"))
262+
.format("strict_date_optional_time")
263+
)
264+
)
265+
);
266+
String searchRequestString = searchRequest.toJsonString();
267+
assertEquals(expectedStringValue, searchRequestString);
268+
}
269+
243270
}

0 commit comments

Comments
 (0)