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

[Backport 2.x] Ensure RestHandler.Wrapper delegates all implementations to the wrapped handler #16161

Merged
merged 2 commits into from
Oct 2, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Add support for docker compose v2 in TestFixturesPlugin ([#16049](https://github.com/opensearch-project/OpenSearch/pull/16049))
- Remove identity-related feature flagged code from the RestController ([#15430](https://github.com/opensearch-project/OpenSearch/pull/15430))
- Remove Identity FeatureFlag ([#16024](https://github.com/opensearch-project/OpenSearch/pull/16024))
- Ensure RestHandler.Wrapper delegates all implementations to the wrapped handler ([#16154](https://github.com/opensearch-project/OpenSearch/pull/16154))


### Deprecated
Expand Down
5 changes: 5 additions & 0 deletions server/src/main/java/org/opensearch/rest/RestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ public List<ReplacedRoute> replacedRoutes() {
public boolean allowSystemIndexAccessByDefault() {
return delegate.allowSystemIndexAccessByDefault();
}

@Override
public boolean supportsStreaming() {
return delegate.supportsStreaming();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.opensearch.client.node.NodeClient;
import org.opensearch.common.Table;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.rest.RestHandler.ReplacedRoute;
import org.opensearch.rest.RestHandler.Route;
import org.opensearch.rest.RestRequest.Method;
Expand All @@ -46,15 +48,22 @@
import org.opensearch.threadpool.ThreadPool;

import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import static org.hamcrest.core.StringContains.containsString;
import static org.hamcrest.object.HasToString.hasToString;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

public class BaseRestHandlerTests extends OpenSearchTestCase {
private NodeClient mockClient;
Expand Down Expand Up @@ -288,4 +297,36 @@ public void testReplaceRoutesMethod() throws Exception {
}
}

public void testRestHandlerWrapper() throws Exception {
RestHandler rh = new RestHandler() {
@Override
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
}
};
RestHandler handlerSpy = spy(rh);
RestHandler.Wrapper rhWrapper = new RestHandler.Wrapper(handlerSpy);

List<java.lang.reflect.Method> overridableMethods = Arrays.stream(RestHandler.class.getMethods())
.filter(
m -> !(Modifier.isPrivate(m.getModifiers()) || Modifier.isStatic(m.getModifiers()) || Modifier.isFinal(m.getModifiers()))
)
.collect(Collectors.toList());

for (java.lang.reflect.Method method : overridableMethods) {
int argCount = method.getParameterCount();
Object[] args = new Object[argCount];
for (int i = 0; i < argCount; i++) {
args[i] = any();
}
if (args.length > 0) {
method.invoke(rhWrapper, args);
} else {
method.invoke(rhWrapper);
}
method.invoke(verify(handlerSpy, times(1)), args);
}
verifyNoMoreInteractions(handlerSpy);
}

}
Loading