From 45b805a3dbf305c2bbd8031885338d2edf8a9e99 Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Thu, 1 Aug 2024 14:16:13 +1200 Subject: [PATCH] Fixed deserialization of SearchRequest when `_source` is an array (#1117) * Add failing test Signed-off-by: Thomas Farr * Fixed deserialization of SearchRequest when `_source` is an array Signed-off-by: Thomas Farr * Fix docker compose error Signed-off-by: Thomas Farr --------- Signed-off-by: Thomas Farr --- .github/workflows/test-integration.yml | 6 +- CHANGELOG.md | 1 + .../client/json/ObjectDeserializer.java | 7 +- .../client/json/UnionDeserializer.java | 9 ++- .../opensearch/core/SearchRequestTest.java | 75 +++++++++++++++++++ 5 files changed, 92 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test-integration.yml b/.github/workflows/test-integration.yml index 7758a38f12..07ad568359 100644 --- a/.github/workflows/test-integration.yml +++ b/.github/workflows/test-integration.yml @@ -41,8 +41,8 @@ jobs: - name: Run Docker run: | echo "PASSWORD=admin" >> $GITHUB_ENV - docker-compose --project-directory .ci/opensearch build --build-arg OPENSEARCH_VERSION=${{ matrix.entry.opensearch_version }} - docker-compose --project-directory .ci/opensearch up -d + docker compose --project-directory .ci/opensearch build --build-arg OPENSEARCH_VERSION=${{ matrix.entry.opensearch_version }} + docker compose --project-directory .ci/opensearch up -d sleep 60 - name: Sets password (new versions) @@ -64,4 +64,4 @@ jobs: - name: Stop Docker run: | - docker-compose --project-directory .ci/opensearch down \ No newline at end of file + docker compose --project-directory .ci/opensearch down \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index fdaefd86b2..afb5321fde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ This section is for maintaining a changelog for all breaking changes for the cli ### Fixed - Fixed error when deserializing a normalizer without 'type' ([#1111](https://github.com/opensearch-project/opensearch-java/pull/1111)) +- Fixed deserialization of SearchRequest when `_source` is an array ([#1117](https://github.com/opensearch-project/opensearch-java/pull/1117)) ### Security diff --git a/java-client/src/main/java/org/opensearch/client/json/ObjectDeserializer.java b/java-client/src/main/java/org/opensearch/client/json/ObjectDeserializer.java index 316b0239e3..29655e35e4 100644 --- a/java-client/src/main/java/org/opensearch/client/json/ObjectDeserializer.java +++ b/java-client/src/main/java/org/opensearch/client/json/ObjectDeserializer.java @@ -239,7 +239,12 @@ public void shortcutProperty(String name) { throw new NoSuchElementException("No deserializer was setup for '" + name + "'"); } - acceptedEvents = EventSetObjectAndString; + this.acceptedEvents = EventSetObjectAndString.clone(); + + if (this.shortcutProperty instanceof FieldObjectDeserializer) { + JsonpDeserializer shortcutDeserializer = ((FieldObjectDeserializer) this.shortcutProperty).deserializer; + this.acceptedEvents.addAll(shortcutDeserializer.nativeEvents()); + } } // ----- Object types diff --git a/java-client/src/main/java/org/opensearch/client/json/UnionDeserializer.java b/java-client/src/main/java/org/opensearch/client/json/UnionDeserializer.java index 55527cefc0..8b190a9fa1 100644 --- a/java-client/src/main/java/org/opensearch/client/json/UnionDeserializer.java +++ b/java-client/src/main/java/org/opensearch/client/json/UnionDeserializer.java @@ -179,8 +179,13 @@ public Builder addMember(Kind tag, JsonpDeserializer member = new SingleMemberHandler<>(tag, deserializer); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/core/SearchRequestTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/core/SearchRequestTest.java index bd0ca5186b..addeb30493 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/core/SearchRequestTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/core/SearchRequestTest.java @@ -9,9 +9,12 @@ package org.opensearch.client.opensearch.core; import java.util.Collections; +import java.util.List; import org.junit.Test; import org.opensearch.client.json.JsonData; import org.opensearch.client.opensearch._types.FieldValue; +import org.opensearch.client.opensearch.core.search.SourceConfig; +import org.opensearch.client.opensearch.core.search.SourceFilter; import org.opensearch.client.opensearch.model.ModelTestCase; public class SearchRequestTest extends ModelTestCase { @@ -57,4 +60,76 @@ public void toBuilder() { assertEquals(copied.index(), origin.index()); } + + @Test + public void canDeserializeSourceAsBoolean() { + String json = "{\"query\":{\"match_all\":{}},\"_source\":true,\"size\":1}"; + + SearchRequest searchRequest = fromJson(json, SearchRequest._DESERIALIZER); + + SourceConfig _source = searchRequest.source(); + assertNotNull(_source); + assertTrue(_source.isFetch()); + assertTrue(_source.fetch()); + } + + @Test + public void canDeserializeSourceAsString() { + String json = "{\"query\":{\"match_all\":{}},\"_source\":\"_id\",\"size\":1}"; + + SearchRequest searchRequest = fromJson(json, SearchRequest._DESERIALIZER); + + SourceConfig _source = searchRequest.source(); + assertNotNull(_source); + assertTrue(_source.isFilter()); + + SourceFilter filter = _source.filter(); + assertNotNull(filter); + + List includes = filter.includes(); + assertEquals(1, includes.size()); + assertEquals("_id", includes.get(0)); + + assertTrue(filter.excludes().isEmpty()); + } + + @Test + public void canDeserializeSourceAsArray() { + String json = "{\"query\":{\"match_all\":{}},\"_source\":[\"_id\"],\"size\":1}"; + + SearchRequest searchRequest = fromJson(json, SearchRequest._DESERIALIZER); + + SourceConfig _source = searchRequest.source(); + assertNotNull(_source); + assertTrue(_source.isFilter()); + + SourceFilter filter = _source.filter(); + assertNotNull(filter); + + List includes = filter.includes(); + assertEquals(1, includes.size()); + assertEquals("_id", includes.get(0)); + + assertTrue(filter.excludes().isEmpty()); + } + + @Test + public void canDeserializeSourceAsObject() { + String json = "{\"query\":{\"match_all\":{}},\"_source\":{\"includes\":[\"_id\"]},\"size\":1}"; + + SearchRequest searchRequest = fromJson(json, SearchRequest._DESERIALIZER); + + SourceConfig _source = searchRequest.source(); + assertNotNull(_source); + assertTrue(_source.isFilter()); + + SourceFilter filter = _source.filter(); + assertNotNull(filter); + + List includes = filter.includes(); + assertEquals(1, includes.size()); + assertEquals("_id", includes.get(0)); + + assertTrue(filter.excludes().isEmpty()); + } }