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

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
}