diff --git a/CHANGELOG.md b/CHANGELOG.md index cb2b26bf6c..c7b67961c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -125,6 +125,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Do not double-wrap OpenSearchException on error ([#323](https://github.com/opensearch-project/opensearch-java/pull/323)) - Fix AwsSdk2TransportOptions.responseCompression ([#322](https://github.com/opensearch-project/opensearch-java/pull/322)) - Fix missing Highlight and SourceConfig in the MultisearchBody ([#442](https://github.com/opensearch-project/opensearch-java/pull/442)) +- Fix parsing /_alias error response for not existing alias ([#476](https://github.com/opensearch-project/opensearch-java/pull/476)) + ### Security diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorResponse.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorResponse.java index 2f71c65905..a3e63ec7dc 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorResponse.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorResponse.java @@ -42,10 +42,12 @@ import org.opensearch.client.json.JsonpSerializable; import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.json.UnionDeserializer; import org.opensearch.client.util.ApiTypeHelper; import org.opensearch.client.util.ObjectBuilder; import org.opensearch.client.util.ObjectBuilderBase; import jakarta.json.stream.JsonGenerator; + import java.util.function.Function; // typedef: _types.ErrorResponseBase @@ -57,6 +59,11 @@ */ @JsonpDeserializable public class ErrorResponse implements JsonpSerializable { + + private enum Kind { + OBJECT, + STRING + } private final ErrorCause error; private final int status; @@ -164,9 +171,22 @@ public ErrorResponse build() { protected static void setupErrorResponseDeserializer(ObjectDeserializer op) { - op.add(Builder::error, ErrorCause._DESERIALIZER, "error"); + op.add(Builder::error, buildErrorCauseDeserializers(), "error"); op.add(Builder::status, JsonpDeserializer.integerDeserializer(), "status"); } + protected static JsonpDeserializer buildErrorCauseDeserializers() { + return new UnionDeserializer.Builder<>(ErrorResponse::getErrorCause, false) + .addMember(Kind.OBJECT, ErrorCause._DESERIALIZER) + .addMember(Kind.STRING, JsonpDeserializer.stringDeserializer()) + .build(); + } + + private static ErrorCause getErrorCause(Kind kind, Object errorCause) { + return Kind.STRING.equals(kind) ? + ErrorCause.of(builder -> builder.type("string_error").reason((String) errorCause)) : + (ErrorCause) errorCause; + } + } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java index f17d7b6167..869e63cc1c 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java @@ -15,6 +15,8 @@ import org.opensearch.client.opensearch.indices.DataStream; import org.opensearch.client.opensearch.indices.DataStreamsStatsResponse; import org.opensearch.client.opensearch.indices.DeleteDataStreamResponse; +import org.opensearch.client.opensearch.indices.GetAliasRequest; +import org.opensearch.client.opensearch.indices.GetAliasResponse; import org.opensearch.client.opensearch.indices.GetDataStreamResponse; import org.opensearch.client.opensearch.indices.GetIndexRequest; import org.opensearch.client.opensearch.indices.GetIndexResponse; @@ -188,4 +190,19 @@ public void testDataStream() throws IOException { assertEquals(ex.status(), 404); } } + + public void testGetNotExistingIndexAlias() throws Exception { + String notExistingIndexAlias = "alias_not_exists"; + GetAliasRequest aliasRequest = new GetAliasRequest.Builder().name(notExistingIndexAlias).build(); + try { + GetAliasResponse response = javaClient().indices().getAlias(aliasRequest); + fail(); + } catch (OpenSearchException ex) { + assertNotNull(ex); + assertEquals(ex.status(), 404); + assertEquals(ex.getMessage(), + "Request failed: [string_error] " + + "alias [alias_not_exists] missing"); + } + } } \ No newline at end of file