提交 3c9136b2 编写于 作者: B Boyang Jerry Peng 提交者: Sijie Guo

Fix: check function implements correct interface (#4844)

上级 b0793a17
......@@ -369,6 +369,20 @@ public class FunctionConfigUtils {
private static void doJavaChecks(FunctionConfig functionConfig, ClassLoader clsLoader) {
try {
Class functionClass = clsLoader.loadClass(functionConfig.getClassName());
if (!org.apache.pulsar.functions.api.Function.class.isAssignableFrom(functionClass)
&& !java.util.function.Function.class.isAssignableFrom(functionClass)) {
throw new IllegalArgumentException(
String.format("Function class %s does not implement the correct interface",
functionClass.getName()));
}
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(
String.format("Function class %s must be in class path", functionConfig.getClassName()), e);
}
Class<?>[] typeArgs;
try {
typeArgs = FunctionCommon.getFunctionTypes(functionConfig, clsLoader);
......
......@@ -43,6 +43,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
......@@ -109,6 +110,13 @@ public class FunctionApiV3ResourceTest {
}
}
private static final class WrongFunction implements Consumer<String> {
@Override
public void accept(String s) {
}
}
private static final String tenant = "test-tenant";
private static final String namespace = "test-namespace";
private static final String function = "test-function";
......@@ -438,6 +446,27 @@ public class FunctionApiV3ResourceTest {
}
}
@Test(expectedExceptions = RestException.class, expectedExceptionsMessageRegExp = "Function class .*. does not implement the correct interface")
public void testRegisterFunctionImplementWrongInterface() {
try {
testRegisterFunctionMissingArguments(
tenant,
namespace,
function,
mockedInputStream,
topicsToSerDeClassName,
mockedFormData,
outputTopic,
outputSerdeClassName,
WrongFunction.class.getName(),
parallelism,
null);
} catch (RestException re){
assertEquals(re.getResponse().getStatusInfo(), Response.Status.BAD_REQUEST);
throw re;
}
}
private void testRegisterFunctionMissingArguments(
String tenant,
String namespace,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册