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

[FEATURE] Provide an easy way to deserialize classes annotated as @JsonpDeserializable #618

Closed
campidelli-wcq opened this issue Sep 11, 2023 · 3 comments · Fixed by #1148
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@campidelli-wcq
Copy link

Is your feature request related to a problem?

We are migrating from Elasticsearch to Opensearch and we have some JSON files that are used to create index templates. That is how we used to do it:

    public void putIndexTemplate(final String pTemplateName, final InputStream pTemplateJson) throws IOException {
        PutIndexTemplateRequest putIndexTemplateRequest = new PutIndexTemplateRequest.Builder()
            .name(pTemplateName)
            .withJson(pTemplateJson)
            .build();
       PutIndexTemplateResponse putIndexTemplateResponse = client.indices().putIndexTemplate(putIndexTemplateRequest);
        log.info(putIndexTemplateResponse.toString());
    }

But that .withJson method is no longer available.

What solution would you like?

We could have the .withJson method added to the Builder, since the request class already has a JsonpDeserializer<PutIndexTemplateRequest> _DESERIALIZER. Or another alternative that would allow us to keep using the builder.

What alternatives have you considered?

First I have tried to use the built-in deserializer, like this:

public void putIndexTemplate(final String pTemplateName, final InputStream pTemplateJson) throws IOException {
        JsonpMapper mapper = client._transport().jsonpMapper();
        JsonParser parser = mapper.jsonProvider().createParser(pTemplateJson);

        PutIndexTemplateRequest request = PutIndexTemplateRequest._DESERIALIZER.deserialize(parser, mapper);
        // request is immutable, I can't set request.setName(pTemplateName)
        PutIndexTemplateResponse response = client.indices().putIndexTemplate(request);

        log.info(response.toString());
}

Then I had to build my own Deserializer to return a Builder instead:

public final class PutIndexTemplateRequestBuilderDeserializer {
    private static final ObjectDeserializer<PutIndexTemplateRequest.Builder> od = new ObjectDeserializer<>(PutIndexTemplateRequest.Builder::new);

    static {
        od.add(PutIndexTemplateRequest.Builder::meta, stringMapDeserializer(JsonData._DESERIALIZER), "_meta");
        od.add(PutIndexTemplateRequest.Builder::composedOf, arrayDeserializer(stringDeserializer()), "composed_of");
        od.add(PutIndexTemplateRequest.Builder::dataStream, DataStream._DESERIALIZER, "data_stream");
        od.add(PutIndexTemplateRequest.Builder::indexPatterns, arrayDeserializer(stringDeserializer()), "index_patterns");
        od.add(PutIndexTemplateRequest.Builder::priority, JsonpDeserializer.integerDeserializer(), "priority");
        od.add(PutIndexTemplateRequest.Builder::template, IndexTemplateMapping._DESERIALIZER, "template");
        od.add(PutIndexTemplateRequest.Builder::version, JsonpDeserializer.longDeserializer(), "version");
    }

    public static PutIndexTemplateRequest.Builder deserialize(JsonpMapper mapper, JsonParser parser) {
        return od.deserialize(parser, mapper);
    }
}

And finally use it in my class:

    public void putIndexTemplate(final String pTemplateName, final InputStream pTemplateJson) throws IOException {
        JsonpMapper mapper = client._transport().jsonpMapper();
        JsonParser parser = mapper.jsonProvider().createParser(pTemplateJson);

        PutIndexTemplateRequest request = PutIndexTemplateRequestBuilderDeserializer
            .deserialize(mapper, parser)
            .name(pTemplateName)
            .build();
        PutIndexTemplateResponse response = client.indices().putIndexTemplate(request);

        log.info(response.toString());
    }

Do you have any additional context?

One last thing, there is no documentation whatsoever describing how to do this, and I think it is not a very unusual scenario.

Thanks for considering it.

@campidelli-wcq campidelli-wcq added enhancement New feature or request untriaged labels Sep 11, 2023
@dblock
Copy link
Member

dblock commented Sep 11, 2023

I think this is a dup with #257, would love for someone to contribute this feature!

@wbeckler wbeckler added good first issue Good for newcomers and removed untriaged labels Sep 19, 2023
@pranishd1
Copy link

Hi, I would like to work in this issue.

@VachaShah
Copy link
Collaborator

All yours @pranishd1, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants