Skip to content

Commit c5ac29c

Browse files
committed
jackson ObjectMapper: auto-detect modules
jackson 2.x still supports java 7 and thus does not automatically support java 8 classes. this will change with jackson 3.x (see e.g. this [PR adding JSR310 date support] to 3.x). PR #251 added documentation on how a custom `ObjectMapper` can be registered - but there's no reason why we can't just enable auto-detection centrally. the docs mention that performance should be considered, but this is IMHO not relevant here since we only construct it once and then keep the same `ObjectMapper` (which in turn the docs mention as best practices). Signed-off-by: Ralph Ursprung <Ralph.Ursprung@avaloq.com> [PR adding JSR310 date support]: FasterXML/jackson-databind#5032
1 parent 3bc5e7a commit c5ac29c

File tree

3 files changed

+5
-8
lines changed

3 files changed

+5
-8
lines changed

CHANGELOG.md

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

89
### Dependencies
910
- 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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +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. For example, to include JSR-310 classes:
103-
104-
```java
105-
OpenSearchTransport transport = new RestClientTransport(restClient,
106-
new JacksonJsonpMapper(new ObjectMapper().registerModule(new JavaTimeModule())));
107-
OpenSearchClient client = new OpenSearchClient(transport);
108-
```
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.
109103

110104
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`.
111105

java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonpMapper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ public JacksonJsonpMapper(ObjectMapper objectMapper, JsonFactory jsonFactory) {
6363

6464
public JacksonJsonpMapper() {
6565
this(
66-
new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, false).setSerializationInclusion(JsonInclude.Include.NON_NULL)
66+
new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, false)
67+
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
68+
.findAndRegisterModules()
6769
);
6870
}
6971

0 commit comments

Comments
 (0)