Skip to content

Implemented the possibility to add custom path functions #1015

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.jayway.jsonpath.internal.function.text.Concatenate;
import com.jayway.jsonpath.internal.function.text.Length;

import java.util.Collections;
import java.io.InvalidClassException;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -28,7 +28,7 @@
*/
public class PathFunctionFactory {

public static final Map<String, Class> FUNCTIONS;
private static final Map<String, Class> FUNCTIONS;

static {
// New functions should be added here and ensure the name is not overridden
Expand Down Expand Up @@ -56,7 +56,7 @@ public class PathFunctionFactory {
map.put("index", Index.class);


FUNCTIONS = Collections.unmodifiableMap(map);
FUNCTIONS = map;
}

/**
Expand Down Expand Up @@ -85,4 +85,14 @@ public static PathFunction newFunction(String name) throws InvalidPathException
}
}
}

public static void addCustomFunction(String name,Class function) throws InvalidClassException {
if(FUNCTIONS.containsKey(name)){
throw new InvalidPathException("Function with name: " + name + " already exists");
}
if (!PathFunction.class.isAssignableFrom(function)){
throw new InvalidClassException("Function with name: " + name + "must be a instance of PathFunction class");
}
FUNCTIONS.put(name,function);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not be able to override existing built-in functions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree, I will change the method to avoid override built-in functions

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.jayway.jsonpath.internal.function;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Configurations;
import com.jayway.jsonpath.InvalidPathException;
import org.junit.jupiter.api.Test;

import java.io.InvalidClassException;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

class CustomFunctionTest extends BaseFunctionTest {

private Configuration conf = Configurations.JSON_SMART_CONFIGURATION;

public class InvalidFunction {

}
@Test
void testAddInvalidFunction(){
assertThrows(InvalidClassException.class, () -> PathFunctionFactory.addCustomFunction("invalid",
InvalidFunction.class));
}

@Test
void testAddValidFunction(){
assertDoesNotThrow(()->PathFunctionFactory.addCustomFunction("toUpperCase",ToUpperCase.class));
verifyTextFunction(conf,"$['text'][0].toUpperCase()","A");
}

@Test
void testAddExistentFunction(){
assertThrows(InvalidPathException.class, () -> PathFunctionFactory.addCustomFunction("avg",
ToUpperCase.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.jayway.jsonpath.internal.function;

import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;

import java.util.List;

public class ToUpperCase implements PathFunction{
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx,
List<Parameter> parameters) {
return ((String) model).toUpperCase();
}
}