registerJmesPathFunctions gives UnsupportedOperationException

The documentation for Checks says that “You can use registerJmesPathFunctions(io.burt.jmespath.function.Function*) to register custom functions.” However, doing that results in an UnsupportedOperationException. Am I doing it wrong or is the documentation wrong?

Relevant Gatling code (I think)

`

trait CheckSupport {


def registerJmesPathFunctions(functions: JmesPathFunction*): Unit = JmesPathFunctions.register(functions)

}

`

`

private[gatling] object JmesPathFunctions {

private[jmespath] var functions: Seq[JmesPathFunction] = Nil

def register(functions: Seq[JmesPathFunction]): Unit = {
if (functions.nonEmpty) {
throw new UnsupportedOperationException(“JmesPath functions have already been registered”)
}
this.functions = functions
}
}

`

My code
The new function

`

public class RegexFunction extends BaseFunction {

public RegexFunction() {
super(ArgumentConstraints.typeOf(JmesPathType.STRING));
}

@Override
protected T callFunction(Adapter runtime, List<FunctionArgument> arguments) {
T valueArgument = arguments.get(0).value();
T patternArgument = arguments.get(1).value();

String valueString = runtime.toString(valueArgument);
String patternString = runtime.toString(patternArgument);

Pattern pattern = Pattern.compile(patternString);

Matcher matcher = pattern.matcher(valueString);
boolean isMatch = matcher.matches();

return runtime.createBoolean(isMatch);
}
}

`

Registering the function (which results in the exception)

`
registerJmesPathFunctions(new RegexFunction())

`

What’s the message of the UnsupportedOperationException?
If it’s “JmesPath functions have already been registered”, it means exactly that. You can only call registerJmesPathFunctions once.

That is exactly what it says even though I am only calling it once. Looking at the Gatling code again, I think the it is missing a “this” to refer to the object variable rather than the method parameter. Therefore it will always fail:

`

private[gatling] object JmesPathFunctions {

private[jmespath] var functions: Seq[JmesPathFunction] = Nil

def register(functions: Seq[JmesPathFunction]): Unit = {
if (**this.**functions.nonEmpty) {
throw new UnsupportedOperationException(“JmesPath functions have already been registered”)
}
this.functions = functions
}
}

`

That is exactly the error message. The Gatling code seems to be missing a “this” to refer to the object variable rather than the method parameter. Both are unfortunatelu called “functions”:

Missing “this.” shown inserted here.

`

private[gatling] object JmesPathFunctions {

private[jmespath] var functions: Seq[JmesPathFunction] = Nil

def register(functions: Seq[JmesPathFunction]): Unit = {
if (this.functions.nonEmpty) {
throw new UnsupportedOperationException(“JmesPath functions have already been registered”)
}
this.functions = functions
}
}

`

Sorry for posting the same/similar message multiple times. I only just realized there is an approval/review flow before the message appears. Consider adding that fact to the group description - unless I am the only one who did not realize this :expressionless:

Facepalm…
Want to contribute the fix?

I have made an attempt at a pull request. All done directly in GitHub.

Yep, I’ve just cherry-picked.