Skip to content

Commit c1ae512

Browse files
authored
jackson ObjectMapper: auto-detect modules (#1643)
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 07a7ffb commit c1ae512

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
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
- Added support for the security plugin APIs ([#1601](https://github.com/opensearch-project/opensearch-java/pull/1601))
8+
- Jackson `ObjectMapper` modules are now being auto-detected ([#1643](https://github.com/opensearch-project/opensearch-java/pull/1643))
89

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

USER_GUIDE.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,12 @@ 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:
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.
103+
You can also provide your own `ObjectMapper` instance if you need to pre-configure it differently, for example:
103104

104105
```java
105106
OpenSearchTransport transport = new RestClientTransport(restClient,
106-
new JacksonJsonpMapper(new ObjectMapper().registerModule(new JavaTimeModule())));
107+
new JacksonJsonpMapper(new ObjectMapper().findAndRegisterModules().configure(SerializationFeature.INDENT_OUTPUT, true)));
107108
OpenSearchClient client = new OpenSearchClient(transport);
108109
```
109110

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)