elasticsearch-java icon indicating copy to clipboard operation
elasticsearch-java copied to clipboard

Possible bug building SortOptions object from json

Open aianta opened this issue 2 years ago • 1 comments

Java API client version

8.7.1

Java version

19.0.1

Elasticsearch Version

8.4.3

Problem description

I'm trying to create a SortOptions object for an elastic search query using the Create API objects from JSON data method. But I keep getting the following exception:

class co.elastic.clients.json.JsonpDeserializer$3 cannot be cast to class co.elastic.clients.json.ObjectDeserializer (co.elastic.clients.json.JsonpDeserializer$3 and co.elastic.clients.json.ObjectDeserializer are in unnamed module of loader 'app')
java.lang.ClassCastException: class co.elastic.clients.json.JsonpDeserializer$3 cannot be cast to class co.elastic.clients.json.ObjectDeserializer (co.elastic.clients.json.JsonpDeserializer$3 and co.elastic.clients.json.ObjectDeserializer are in unnamed module of loader 'app')
    at co.elastic.clients.util.WithJsonObjectBuilderBase.withJson(WithJsonObjectBuilderBase.java:53)
    at co.elastic.clients.json.WithJson.withJson(WithJson.java:57)
    at ElasticsearchFromJsonTest.lambda$parseSortOptions$0(ElasticsearchFromJsonTest.java:27)
    at co.elastic.clients.elasticsearch._types.SortOptions.of(SortOptions.java:119)
    at ElasticsearchFromJsonTest.parseSortOptions(ElasticsearchFromJsonTest.java:27)
...

Code sample:

import co.elastic.clients.elasticsearch._types.SortOptions;

import org.junit.jupiter.api.Test;
import java.io.StringReader;

public class ElasticsearchFromJsonTest {

    @Test
    void parseSortOptions(){

        String test = """
                {
                  "sort" : [
                    { "term" : "asc"}
                  ]
                }
                """;

        SortOptions so = SortOptions.of(b->b.withJson(new StringReader(test)));

    }

}

I've also tried setting test to:

[{"term" : "asc"}]

And got the same results. I'll admit the 'bug' may just be in my simple minded interpretation of what subsection/format of json input is required to get this to work 😓, on the plus side that would be an easy fix.

aianta avatar May 11 '23 14:05 aianta

Hello, thank you for the report! Definitely a bug, we'll investigate this.

l-trotta avatar Apr 18 '24 15:04 l-trotta