Skip to content

allow to customize json search function #188

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 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -161,10 +161,6 @@ public ArgValue convert(String s) {
Map.entry(BETWEEN, "(%1$s >= %2$s && %1$s <= %3$s)")
);

private static final String JSONB_PATH_EXISTS = "jsonb_path_exists";

private static final String JSONB_PATH_EXISTS_TZ = "jsonb_path_exists_tz";

private final ComparisonOperator operator;
private final String keyPath;
private final List<ArgValue> values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public class JsonbSupport {

public static boolean DATE_TIME_SUPPORT = false;

/**
* Postgresql {@code jsonb_path_exists} function to use
*/
public static String JSONB_PATH_EXISTS = "jsonb_path_exists";

/**
* Postgresql {@code jsonb_path_exists_tz} function to use
*/
public static String JSONB_PATH_EXISTS_TZ = "jsonb_path_exists_tz";

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hi. This code essentially introduces global mutable variables which is well known anti-pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

well, whole jsonb support is based on static methods so i didnt seen any other way to make fields accessible. i used same "pattern" as for enabling date time support.

Copy link
Collaborator

@nstdio nstdio Aug 19, 2025

Choose a reason for hiding this comment

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

I'm sure there are other ways to achieve this. Yeah, configuring date time support in this way was a quite a mistake I must say.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i moved configuration to QuerySupport

private static final Set<Database> JSON_SUPPORT = EnumSet.of(Database.POSTGRESQL);

private static final Map<ComparisonOperator, ComparisonOperator> NEGATE_OPERATORS =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ void testJsonbPathExpressionWithTemporal(ComparisonOperator operator, String key
assertEquals(expectedJsonbPath, expression.jsonbPath());
}

@ParameterizedTest
@MethodSource("customized")
void testJsonbPathExpressionCustomized(ComparisonOperator operator, String keyPath, List<String> arguments, String expectedJsonbFunction, String expectedJsonbPath) {
String jsonbPathExists = JsonbSupport.JSONB_PATH_EXISTS;
String jsonbPathExistsTz = JsonbSupport.JSONB_PATH_EXISTS_TZ;
try {
JsonbSupport.JSONB_PATH_EXISTS = "my_jsonb_path_exists";
JsonbSupport.JSONB_PATH_EXISTS_TZ = "my_jsonb_path_exists_tz";
JsonbSupport.DATE_TIME_SUPPORT = true;
JsonbExpressionBuilder builder = new JsonbExpressionBuilder(operator, keyPath, arguments);
var expression = builder.getJsonPathExpression();
assertEquals(expectedJsonbFunction, expression.jsonbFunction());
assertEquals(expectedJsonbPath, expression.jsonbPath());
} catch (Exception e) {
throw e;
} finally {
JsonbSupport.JSONB_PATH_EXISTS = jsonbPathExists;
JsonbSupport.JSONB_PATH_EXISTS_TZ = jsonbPathExistsTz;
}
}

static Stream<Arguments> data() {
return Stream.of(
allOperators(),
Expand Down Expand Up @@ -78,6 +99,17 @@ static Stream<Arguments> conversion() {
).filter(Objects::nonNull);
}

static Stream<Arguments> customized() {

return Stream.of(
arguments(RSQLOperators.EQUAL, "json.equal_key", Collections.singletonList("value"), "my_jsonb_path_exists", "$.equal_key ? (@ == \"value\")"),
arguments(RSQLOperators.GREATER_THAN, "json.greater_than_key", Collections.singletonList("value"), "my_jsonb_path_exists", "$.greater_than_key ? (@ > \"value\")"),
arguments(RSQLOperators.EQUAL, "json.equal_key", Collections.singletonList("1970-01-01T00:00:00.000"), "my_jsonb_path_exists", "$.equal_key ? (@.datetime() == \"1970-01-01T00:00:00.000\".datetime())"),
arguments(RSQLOperators.EQUAL, "json.equal_key", Collections.singletonList("1970-01-01T00:00:00.000Z"), "my_jsonb_path_exists_tz", "$.equal_key ? (@.datetime() == \"1970-01-01T00:00:00.000Z\".datetime())"),
null
).filter(Objects::nonNull);
}

static Stream<Arguments> temporal() {

return Stream.of(
Expand Down