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

Fixed deserialization of SearchRequest when _source is an array #1117

Merged
merged 3 commits into from
Aug 1, 2024
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
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());
}
}
Loading