Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2024 Wren Security
* Copyright 2024-2026 Wren Security
*/
package org.forgerock.openidm.repo.jdbc.impl.query;

Expand Down Expand Up @@ -53,6 +53,13 @@ public StringSQLRenderer visitValueAssertion(NamedParameterCollector collector,
return visitBooleanAssertion(collector, config, operand, field, valueAssertion);
}

if (config.valueType == ValueType.JSON_LIST) {
if (!"=".equals(operand)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this make sense? Filtering on array values is well defined. I don't see why other operands should not be supported.

throw new UnsupportedOperationException("Unsupported operand '" + operand + "' for JSON_LIST column");
}
return visitJsonListAssertion(collector, config, field, valueAssertion);
}

String paramValue;
try {
paramValue = valueAssertion instanceof String
Expand Down Expand Up @@ -89,6 +96,11 @@ protected StringSQLRenderer visitBooleanAssertion(NamedParameterCollector collec
+ "${" + paramName + "}");
}

protected StringSQLRenderer visitJsonListAssertion(NamedParameterCollector collector, MappedColumnConfig config,
JsonPointer field, Object valueAssertion) {
throw new UnsupportedOperationException("JSON_LIST value assertion is not supported for this database");
}

@Override
public StringSQLRenderer visitPresentFilter(NamedParameterCollector collector, JsonPointer field) {
MappedColumnConfig config = configResolver.resolve(field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
 * information: "Portions copyright [year] [name of copyright owner]".
 *
 * Copyright 2015 ForgeRock AS.
* Portions Copyright 2024 Wren Security.
* Portions Copyright 2024-2026 Wren Security.
 */
package org.forgerock.openidm.repo.jdbc.impl.vendor;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.Map;
import java.util.stream.Collectors;
import org.forgerock.json.JsonPointer;
Expand Down Expand Up @@ -77,6 +78,19 @@ protected StringSQLRenderer visitBooleanAssertion(NamedParameterCollector collec
String paramName = collector.register("v", valueAssertion);
return new StringSQLRenderer(config.columnName + " " + operand + " " + "${" + paramName + "}");
}

@Override
protected StringSQLRenderer visitJsonListAssertion(NamedParameterCollector collector,
MappedColumnConfig config, JsonPointer field, Object valueAssertion) {
String jsonValue;
try {
jsonValue = objectMapper.writeValueAsString(valueAssertion);
} catch (JsonProcessingException e) {
throw new IllegalStateException("Failed to serialize JSON value.", e);
}
String paramName = collector.register("v", "[" + jsonValue + "]");
return new StringSQLRenderer(config.columnName + "::jsonb @> ${" + paramName + "}::jsonb");
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2024 Wren Security
* Copyright 2024-2026 Wren Security
*/
package org.forgerock.openidm.repo.jdbc.impl.vendor;

import static org.forgerock.openidm.repo.jdbc.Constants.OBJECT_ID;
import static org.testng.Assert.assertEquals;

import java.sql.Connection;
import java.util.List;
import java.util.Map;
import org.forgerock.openidm.repo.jdbc.TableHandler;
import org.forgerock.openidm.repo.jdbc.impl.handler.AbstractMappedTableHandlerTest;
import org.testng.annotations.Test;
Expand All @@ -40,4 +45,37 @@ protected TableHandler createTableHandler() throws Exception {
);
}

@Test
public void testQueryFilterJsonListEquals() throws Exception {
createResource("resource-1", Map.of("tags", List.of("foo", "bar")));
createResource("resource-2", Map.of("tags", List.of("foo", "baz")));
createResource("resource-3", Map.of("tags", List.of(1, 2)));
createResource("resource-4", Map.of("tags", List.of(1, 3)));
createResource("resource-5", Map.of("tags", List.of(true)));
createResource("resource-6", Map.of("tags", List.of(false)));

var fooResult = queryResource("tags eq \"foo\"");
assertEquals(fooResult.size(), 2);
assertEquals(fooResult.stream().map(r -> r.get(OBJECT_ID)).sorted().toList(),
List.of("resource-1", "resource-2"));

var bazResult = queryResource("tags eq \"baz\"");
assertEquals(bazResult.size(), 1);
assertEquals(bazResult.stream().map(r -> r.get(OBJECT_ID)).toList(),
List.of("resource-2"));

var numberResult = queryResource("tags eq 1");
assertEquals(numberResult.size(), 2);
assertEquals(numberResult.stream().map(r -> r.get(OBJECT_ID)).toList(),
List.of("resource-3", "resource-4"));

var booleanResult = queryResource("tags eq true");
assertEquals(booleanResult.size(), 1);
assertEquals(booleanResult.stream().map(r -> r.get(OBJECT_ID)).toList(),
List.of("resource-5"));

var noResult = queryResource("tags eq \"hello\"");
assertEquals(noResult.size(), 0);
}

}