Skip to content

Commit

Permalink
Fixed deserialization of SearchRequest when _source is an array (#1117
Browse files Browse the repository at this point in the history
)

* Add failing test

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

* Fixed deserialization of SearchRequest when `_source` is an array

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

* Fix docker compose error

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

---------

Signed-off-by: Thomas Farr <tsfarr@amazon.com>
  • Loading branch information
Xtansia authored Aug 1, 2024
1 parent 3d8061e commit 45b805a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -64,4 +64,4 @@ jobs:

- name: Stop Docker
run: |
docker-compose --project-directory .ci/opensearch down
docker compose --project-directory .ci/opensearch down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,13 @@ public Builder<Union, Kind, Member> addMember(Kind tag, JsonpDeserializer<? exte
);
objectMembers.add(member);
if (od.shortcutProperty() != null) {
// also add it as a string
addMember(Event.VALUE_STRING, tag, member);
// also add it as the shortcut property events
for (Event e : od.acceptedEvents()) {
if (e == Event.START_OBJECT || e == Event.KEY_NAME) {
continue;
}
addMember(e, tag, member);
}
}
} else {
UnionDeserializer.SingleMemberHandler<Union, Kind, Member> member = new SingleMemberHandler<>(tag, deserializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String> 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<String> 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<String> includes = filter.includes();
assertEquals(1, includes.size());
assertEquals("_id", includes.get(0));

assertTrue(filter.excludes().isEmpty());
}
}

0 comments on commit 45b805a

Please sign in to comment.