Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix toJsonString default mapper serialize type (#1309) #1328

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}
Loading