Skip to content

Commit 4eb7ea5

Browse files
rursprungXtansia
authored andcommitted
jackson ObjectMapper: auto-detect modules (opensearch-project#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 opensearch-project#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 (cherry picked from commit c1ae512) Signed-off-by: Thomas Farr <tsfarr@amazon.com>
1 parent f78d75d commit 4eb7ea5

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
33

44
## [Unreleased 2.x]
55
### Added
6+
- Added Jackson `ObjectMapper` module auto-detection ([#1643](https://github.com/opensearch-project/opensearch-java/pull/1643))
67

78
### Dependencies
89

USER_GUIDE.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ OpenSearchTransport transport = new RestClientTransport(restClient, new JacksonJ
6565
OpenSearchClient client = new OpenSearchClient(transport);
6666
```
6767

68-
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:
68+
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.
69+
You can also provide your own `ObjectMapper` instance if you need to pre-configure it differently, for example:
6970

7071
```java
71-
Transport transport = new RestClientTransport(restClient,
72-
new JacksonJsonpMapper(new ObjectMapper().registerModule(new JavaTimeModule())));
72+
OpenSearchTransport transport = new RestClientTransport(restClient,
73+
new JacksonJsonpMapper(new ObjectMapper().findAndRegisterModules().configure(SerializationFeature.INDENT_OUTPUT, true)));
7374
OpenSearchClient client = new OpenSearchClient(transport);
7475
```
7576

@@ -194,4 +195,4 @@ You can find a working sample of the above code in [IndexingBasics.java](./sampl
194195

195196
## Plugins
196197

197-
- [k-NN](guides/plugins/knn.md)
198+
- [k-NN](guides/plugins/knn.md)

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)