Skip to content

Commit

Permalink
Fixed deserialization of SearchRequest when _source is an array (op…
Browse files Browse the repository at this point in the history
…ensearch-project#1117) (opensearch-project#1118)

* Add failing test



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



* Fix docker compose error



---------


(cherry picked from commit 45b805a)

Signed-off-by: Thomas Farr <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent c0efd41 commit 3324f59
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 @@ -15,6 +15,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### 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 @@ -62,4 +65,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 3324f59

Please sign in to comment.