Skip to content

Commit 6a945e1

Browse files
committed
jackson ObjectMapper: add JDK8 & JSR310 modules
without these various core java types introduced with JDK8 (more than a decade ago!) are not usable. we should enable them centrally so that not everyone has to do this manually. note that with jackson 3.x this will no longer be needed as it includes it by default: FasterXML/jackson-databind#982 Signed-off-by: Ralph Ursprung <Ralph.Ursprung@avaloq.com>
1 parent c5ac29c commit 6a945e1

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
55
### Added
66
- Added support for Index Management plugin APIs ([#1604](https://github.com/opensearch-project/opensearch-java/pull/1604))
77
- Jackson `ObjectMapper` modules are now being auto-detected ([#1614](https://github.com/opensearch-project/opensearch-java/pull/1614))
8+
- Jackson `ObjectMapper` JDK8 & JSR310 modules are now always present ([#1614](https://github.com/opensearch-project/opensearch-java/pull/1614))
89

910
### Dependencies
1011
- Bump `org.owasp.dependencycheck` from 12.1.1 to 12.1.2 ([#1608](https://github.com/opensearch-project/opensearch-java/pull/1608), [#1607](https://github.com/opensearch-project/opensearch-java/pull/1607))

USER_GUIDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ OpenSearchTransport transport = new RestClientTransport(restClient, new JacksonJ
9999
OpenSearchClient client = new OpenSearchClient(transport);
100100
```
101101

102-
The `JacksonJsonpMapper` class (2.x versions) only supports Java 7 objects by default. [Java 8 modules](https://github.com/FasterXML/jackson-modules-java8) to support JDK8 classes such as the Date and Time API (JSR-310), `Optional`, and more can be used by including [the additional datatype dependency](https://github.com/FasterXML/jackson-modules-java8#usage) and adding the module. Auto-detection for these modules is enabled, adding them to the classpath is enough.
102+
Auto-detection for `JsonMapper` modules is enabled, adding them to the classpath is enough. JSR310 & JDK8 support are enabled by default.
103103

104104
Upcoming OpenSearch `3.0.0` release brings HTTP/2 support and as such, the `RestClientTransport` would switch to HTTP/2 if available (for both HTTPS and/or HTTP protocols). The desired protocol could be forced using `RestClientBuilder.HttpClientConfigCallback`.
105105

java-client/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ dependencies {
217217
// Apache 2.0
218218
implementation("com.fasterxml.jackson.core", "jackson-core", jacksonVersion)
219219
implementation("com.fasterxml.jackson.core", "jackson-databind", jacksonDatabindVersion)
220+
implementation("com.fasterxml.jackson.datatype", "jackson-datatype-jsr310", jacksonDatabindVersion)
221+
implementation("com.fasterxml.jackson.datatype", "jackson-datatype-jdk8", jacksonDatabindVersion)
220222
testImplementation("com.fasterxml.jackson.datatype", "jackson-datatype-jakarta-jsonp", jacksonVersion)
221223

222224
// For AwsSdk2Transport

java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
package org.opensearch.client.opensearch.integTest;
1010

1111
import java.io.IOException;
12+
import java.time.LocalDateTime;
1213
import java.util.ArrayList;
1314
import java.util.Collections;
1415
import java.util.List;
16+
import java.util.Optional;
1517
import org.joda.time.DateTime;
1618
import org.joda.time.DateTimeZone;
1719
import org.joda.time.format.DateTimeFormat;
@@ -604,9 +606,34 @@ public void testGetIdWithPlusSign() throws Exception {
604606
}
605607
}
606608

609+
public void testJacksonJava8TypesSerDe() throws Exception {
610+
final String id = "java8";
611+
final AppData appData = new AppData();
612+
// use a date where month & day could be swapped depending on the format to ensure that this can't happen.
613+
// i.e. month != day and day <= 12.
614+
final LocalDateTime testDate = LocalDateTime.of(2000, 12, 1, 0, 0);
615+
appData.setDate(Optional.of(testDate));
616+
617+
{
618+
final IndexResponse indexResponse = javaClient().index(b -> b.index("index").id(id).document(appData));
619+
assertEquals("index", indexResponse.index());
620+
assertEquals(id, indexResponse.id());
621+
}
622+
{
623+
final GetResponse<AppData> getResponse = javaClient().get(b -> b.index("index").id(id), AppData.class);
624+
assertTrue(getResponse.found());
625+
assertEquals("index", getResponse.index());
626+
assertEquals(id, getResponse.id());
627+
assertNotNull(getResponse.source());
628+
assertTrue(getResponse.source().getDate().isPresent());
629+
assertEquals(testDate, getResponse.source().getDate().get());
630+
}
631+
}
632+
607633
public static class AppData {
608634
private int intValue;
609635
private String msg;
636+
private Optional<LocalDateTime> date = Optional.empty();
610637

611638
public int getIntValue() {
612639
return intValue;
@@ -623,5 +650,13 @@ public String getMsg() {
623650
public void setMsg(String msg) {
624651
this.msg = msg;
625652
}
653+
654+
public Optional<LocalDateTime> getDate() {
655+
return date;
656+
}
657+
658+
public void setDate(Optional<LocalDateTime> date) {
659+
this.date = date;
660+
}
626661
}
627662
}

0 commit comments

Comments
 (0)