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

Provide a way to use an alternate JSON mapper #328

Open
MT-Jacobs opened this issue Jul 11, 2022 · 2 comments
Open

Provide a way to use an alternate JSON mapper #328

MT-Jacobs opened this issue Jul 11, 2022 · 2 comments
Labels
type: improvement A minor improvement to an existing feature

Comments

@MT-Jacobs
Copy link

Feature description

Using the default Micronaut ObjectMapper is good and all when setting up Elasticsearch clients:

https://github.com/micronaut-projects/micronaut-elasticsearch/blob/48e0b0c/elasticsearch/src/main/java/io/micronaut/elasticsearch/DefaultElasticsearchClientFactory.java#L83-L88

but let's say our service is using multiple features that rely on using a shared ObjectMapper, like the HTTP server and elasticsearch-java.

If you provide any serialization specialization for Elasticsearch, it impacts the HTTP server, and vice-versa.

As one example, the default ObjectMapper that Micronaut provides doesn't serialize dates correctly; passing an Instant within a class results in the fields being serialized to an array, which to ES looks like we're passing multiple dates of very low values (2022 in the Unix Epoch is 2,022 seconds after Jan 1 1970). The workaround is to apply a @JsonFormat override to every Instant:

@JsonFormat(shape = JsonFormat.Shape.STRING)

If we attempt to use write-dates-as-timestamps as documented here:

micronaut-projects/micronaut-core#240 (comment)

this will fix elasticsearch-java's serialization behavior, but it will also change the http-server serialization behavior.

One solution would be to provide an alternate ObjectMapper from the default one for micronaut-elasticsearch. Is this a workable idea?

@graemerocher
Copy link
Contributor

yeah that is certainly an idea, wonder if we need to add support in Micronaut Serialization for configuring alternative mappers

@graemerocher graemerocher added the type: improvement A minor improvement to an existing feature label Aug 11, 2022
@MT-Jacobs
Copy link
Author

Not sure how I missed this before, but I bet we could use @JacksonFeatures to facilitate this the same way that declarative HTTP clients do:

https://docs.micronaut.io/3.7.4/guide/#clientJackson

Will definitely require a code change here though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A minor improvement to an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants