Skip to content

Commit

Permalink
Fix toJsonString default mapper serialize type (opensearch-project#1309
Browse files Browse the repository at this point in the history
…) (opensearch-project#1328)

* 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 opensearch-project#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]>
(cherry picked from commit ca3d488)
  • Loading branch information
playLeo authored Nov 25, 2024
1 parent 334864e commit 008c013
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Fixed
- Fixed `IcuCollationDecomposition`'s variants to align with those supported by OpenSearch ([#]())
- 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -109,6 +111,24 @@ public <T> T deserialize(JsonpDeserializer<T> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public <T> 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."
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -240,4 +242,29 @@ public void setChildren(List<SomeClass> 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);
}

}

0 comments on commit 008c013

Please sign in to comment.