Skip to content

[BUG] OpenSearchIndicesClient#getAlias throws UnexpectedJsonEventException when the alias doesn't exist #301

@luontola

Description

@luontola

What is the bug?

When specifying the name of an alias with org.opensearch.client.opensearch.indices.GetAliasRequest.Builder#name and the alias doesn't exist on the server, the client will throw a JSON parsing exception instead of indicating that the alias doesn't exist.

How can one reproduce the bug?

Run the following code when the foo alias doesn't exist.

        OpenSearchClient client = new OpenSearchClient(
                new RestClientTransport(
                        RestClient.builder(HttpHost.create("http://127.0.0.1:9200"))
                                .build(),
                        new JacksonJsonpMapper()));

        try (var ignored = client._transport()) {
            GetAliasResponse response = client.indices().getAlias(
                    builder -> builder.name("foo")
            );
            System.out.println(response.result());
        }

The client will make the request GET /_alias/foo and the server will response with http status 404 and the body {"error":"alias [foo] missing","status":404}.

This causes the client to throw the exception:

org.opensearch.client.json.UnexpectedJsonEventException: Unexpected JSON event 'VALUE_STRING' instead of '[START_OBJECT, KEY_NAME]'
	at org.opensearch.client.json.JsonpUtils.ensureAccepts(JsonpUtils.java:81)
	at org.opensearch.client.json.JsonpDeserializer.deserialize(JsonpDeserializer.java:87)
	at org.opensearch.client.json.ObjectBuilderDeserializer.deserialize(ObjectBuilderDeserializer.java:92)
	at org.opensearch.client.json.DelegatingDeserializer$SameType.deserialize(DelegatingDeserializer.java:56)
	at org.opensearch.client.json.ObjectDeserializer$FieldObjectDeserializer.deserialize(ObjectDeserializer.java:85)
	at org.opensearch.client.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:189)
	at org.opensearch.client.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:150)
	at org.opensearch.client.json.JsonpDeserializer.deserialize(JsonpDeserializer.java:88)
	at org.opensearch.client.json.ObjectBuilderDeserializer.deserialize(ObjectBuilderDeserializer.java:92)
	at org.opensearch.client.json.DelegatingDeserializer$SameType.deserialize(DelegatingDeserializer.java:56)
	at org.opensearch.client.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:271)
	at org.opensearch.client.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:143)
	at org.opensearch.client.opensearch.indices.OpenSearchIndicesClient.getAlias(OpenSearchIndicesClient.java:671)
	at org.opensearch.client.opensearch.indices.OpenSearchIndicesClient.getAlias(OpenSearchIndicesClient.java:685)
	at example.Example.main(Example.java:30)

What is the expected behavior?

It would return a result that says the alias doesn't exist. I suppose with the current Java client the style is to throw an OpenSearchException with status 404.

What is your host/environment?

opensearch server 2.3.0
opensearch-java 2.1.0
opensearch-rest-client 2.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions