From 9176e1b7bbe4f64e8d113fd424ed349fa90697cc Mon Sep 17 00:00:00 2001 From: szczepanczykd <48957041+szczepanczykd@users.noreply.github.com> Date: Thu, 11 May 2023 00:27:15 +0200 Subject: [PATCH 1/2] fix: handle String error deserialization for ErrorCause object (#301) (#476) * fix: handle String error deserialization for ErrorCause object (#301) Signed-off-by: Dominik Szczepanczyk * update CHANGELOG.md Signed-off-by: Dominik Szczepanczyk --------- Signed-off-by: Dominik Szczepanczyk --- CHANGELOG.md | 2 ++ .../opensearch/_types/ErrorResponse.java | 22 ++++++++++++++++++- .../integTest/AbstractIndicesClientIT.java | 17 ++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8a2bdc407..03096472b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,6 +100,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 From 8d770afebc9ffe514b4baaa57ad4a3a75268752e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Szczepa=C5=84czyk?= <48957041+szczepanczykd@users.noreply.github.com> Date: Tue, 23 May 2023 07:37:50 +0200 Subject: [PATCH 2/2] fix: change int type to long (#489) * fix: change int type to long Signed-off-by: Dominik Szczepanczyk * update CHANGELOG.md Signed-off-by: Dominik Szczepanczyk * fix: change totalDataSetSizeInBytes to Long too Signed-off-by: Dominik Szczepanczyk * fix: skip removing opensearch-observability index in AfterTest cleanup Signed-off-by: Dominik Szczepanczyk --------- Signed-off-by: Dominik Szczepanczyk --- CHANGELOG.md | 2 +- .../client/opensearch/_types/StoreStats.java | 30 +++++++++---------- .../OpenSearchJavaClientTestCase.java | 3 +- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e91b224385..059f0a0b20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -103,7 +103,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - 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)) - +- Fix StoryStats numeric value out of range of int ([#489](https://github.com/opensearch-project/opensearch-java/pull/489)) ### Security diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/StoreStats.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/StoreStats.java index 6abe2f6626..6931c5c4ee 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/StoreStats.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/StoreStats.java @@ -57,18 +57,18 @@ public class StoreStats implements JsonpSerializable { @Nullable private final String size; - private final int sizeInBytes; + private final long sizeInBytes; @Nullable private final String reserved; - private final int reservedInBytes; + private final long reservedInBytes; @Nullable private final String totalDataSetSize; @Nullable - private final Integer totalDataSetSizeInBytes; + private final Long totalDataSetSizeInBytes; // --------------------------------------------------------------------------------------------- @@ -98,7 +98,7 @@ public final String size() { /** * Required - API name: {@code size_in_bytes} */ - public final int sizeInBytes() { + public final long sizeInBytes() { return this.sizeInBytes; } @@ -113,7 +113,7 @@ public final String reserved() { /** * Required - API name: {@code reserved_in_bytes} */ - public final int reservedInBytes() { + public final long reservedInBytes() { return this.reservedInBytes; } @@ -129,7 +129,7 @@ public final String totalDataSetSize() { * API name: {@code total_data_set_size_in_bytes} */ @Nullable - public final Integer totalDataSetSizeInBytes() { + public final Long totalDataSetSizeInBytes() { return this.totalDataSetSizeInBytes; } @@ -183,18 +183,18 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder op) { op.add(Builder::size, JsonpDeserializer.stringDeserializer(), "size"); - op.add(Builder::sizeInBytes, JsonpDeserializer.integerDeserializer(), "size_in_bytes"); + op.add(Builder::sizeInBytes, JsonpDeserializer.longDeserializer(), "size_in_bytes"); op.add(Builder::reserved, JsonpDeserializer.stringDeserializer(), "reserved"); - op.add(Builder::reservedInBytes, JsonpDeserializer.integerDeserializer(), "reserved_in_bytes"); + op.add(Builder::reservedInBytes, JsonpDeserializer.longDeserializer(), "reserved_in_bytes"); op.add(Builder::totalDataSetSize, JsonpDeserializer.stringDeserializer(), "total_data_set_size"); - op.add(Builder::totalDataSetSizeInBytes, JsonpDeserializer.integerDeserializer(), + op.add(Builder::totalDataSetSizeInBytes, JsonpDeserializer.longDeserializer(), "total_data_set_size_in_bytes"); } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/OpenSearchJavaClientTestCase.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/OpenSearchJavaClientTestCase.java index 6dacb20f41..7cae6e2599 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/OpenSearchJavaClientTestCase.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/OpenSearchJavaClientTestCase.java @@ -39,6 +39,7 @@ import java.util.TreeSet; public abstract class OpenSearchJavaClientTestCase extends OpenSearchRestTestCase implements OpenSearchTransportSupport { + private static final List systemIndices = List.of(".opensearch-observability", ".opendistro_security"); private static OpenSearchClient javaClient; private static OpenSearchClient adminJavaClient; @@ -137,7 +138,7 @@ protected void wipeAllOSIndices() throws IOException { .indices(r -> r.headers("index,creation.date").expandWildcards(ExpandWildcard.All)); for (IndicesRecord index : response.valueBody()) { - if (index.index() != null && !".opendistro_security".equals(index.index())) { + if (index.index() != null && !systemIndices.contains(index.index())) { adminJavaClient().indices().delete(new DeleteIndexRequest.Builder().index(index.index()).build()); } }