Skip to content

Commit

Permalink
fix: handle String error deserialization for ErrorCause object (#301) (
Browse files Browse the repository at this point in the history
…#476)

* fix: handle String error deserialization for ErrorCause object (#301)

Signed-off-by: Dominik Szczepanczyk <szczepanczyk.dominik@gmail.com>

* update CHANGELOG.md

Signed-off-by: Dominik Szczepanczyk <szczepanczyk.dominik@gmail.com>

---------

Signed-off-by: Dominik Szczepanczyk <szczepanczyk.dominik@gmail.com>
  • Loading branch information
szczepanczykd authored May 10, 2023
1 parent af8d96f commit 897d0c1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -57,6 +59,11 @@
*/
@JsonpDeserializable
public class ErrorResponse implements JsonpSerializable {

private enum Kind {
OBJECT,
STRING
}
private final ErrorCause error;

private final int status;
Expand Down Expand Up @@ -164,9 +171,22 @@ public ErrorResponse build() {

protected static void setupErrorResponseDeserializer(ObjectDeserializer<ErrorResponse.Builder> op) {

op.add(Builder::error, ErrorCause._DESERIALIZER, "error");
op.add(Builder::error, buildErrorCauseDeserializers(), "error");
op.add(Builder::status, JsonpDeserializer.integerDeserializer(), "status");

}

protected static JsonpDeserializer<ErrorCause> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}
}

0 comments on commit 897d0c1

Please sign in to comment.