Skip to content
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

[NOID] fixes #3349: Can not create custom procedure with void return type using apoc.custom.declareProcedure (#3356) #3406

Merged
merged 1 commit into from
Jan 30, 2023
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
6 changes: 4 additions & 2 deletions extended/src/main/java/apoc/custom/CypherProcedures.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,16 @@ private void validateProcedure(String statement, List<FieldSignature> input, Lis
private void checkMode(QueryExecutionType.QueryType queryType, Mode mode) {
Map<QueryExecutionType.QueryType, Mode> map = Map.of(QueryExecutionType.QueryType.WRITE, Mode.WRITE,
QueryExecutionType.QueryType.READ_ONLY, Mode.READ,
QueryExecutionType.QueryType.READ_WRITE, Mode.WRITE);
QueryExecutionType.QueryType.READ_WRITE, Mode.WRITE,
QueryExecutionType.QueryType.DBMS, Mode.DBMS,
QueryExecutionType.QueryType.SCHEMA_WRITE, Mode.SCHEMA);

if (!map.get(queryType).equals(mode)) {
throw new RuntimeException(String.format("The query execution type is %s, but you provided mode %s.\n" +
"Supported modes are %s",
queryType.name(),
mode.name(),
map.values().stream().sorted().collect(Collectors.toList()).toString()));
map.values().stream().sorted().collect(Collectors.toList())));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ private String serializeSignatures(List<FieldSignature> signatures) {

public static List<FieldSignature> deserializeSignatures(String s) {
List<Map<String, Object>> mapped = Util.fromJson(s, List.class);
if (mapped.isEmpty()) return ProcedureSignature.VOID;
return mapped.stream().map(map -> {
String typeString = (String) map.get("type");
if (typeString.endsWith("?")) {
Expand Down
35 changes: 34 additions & 1 deletion extended/src/test/java/apoc/custom/CypherProceduresTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.QueryExecutionException;
import org.neo4j.graphdb.Transaction;
import org.neo4j.procedure.builtin.BuiltInDbmsProcedures;
import org.neo4j.test.rule.DbmsRule;
import org.neo4j.test.rule.ImpermanentDbmsRule;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static apoc.custom.CypherProceduresHandler.FUNCTION;
import static apoc.custom.CypherProceduresHandler.PROCEDURE;
Expand All @@ -48,9 +50,10 @@ public class CypherProceduresTest {
@Rule
public ExpectedException thrown = ExpectedException.none();


@Before
public void setup() {
TestUtil.registerProcedure(db, CypherProcedures.class);
TestUtil.registerProcedure(db, CypherProcedures.class, BuiltInDbmsProcedures.class);
}

@AfterAll
Expand Down Expand Up @@ -584,6 +587,36 @@ public void testIssue2032() {
assertProcedureFails(String.format(SIGNATURE_SYNTAX_ERROR, procedureSignature),
"call apoc.custom.declareProcedure('" + procedureSignature + "','RETURN $first + $s AS answer')");
}

@Test
public void testIssue3349() {
String procedure = """
CALL apoc.custom.declareProcedure(
'retFunctionNames() :: (name :: STRING)',
'call dbms.listConfig() YIELD name RETURN name',
'DBMS'
);""";
db.executeTransactionally(procedure);
List<String> functions = db.executeTransactionally("CALL custom.retFunctionNames()", Map.of(), result -> result
.stream()
.map(m -> (String) m.get("name"))
.collect(Collectors.toList()));
assertFalse(functions.isEmpty());


String procedureVoid = "CALL apoc.custom.declareProcedure(\n" +
" 'setTxMetadata(meta :: MAP) :: VOID',\n" +
" '\n" +
" CALL tx.setMetaData($meta)" +
" ',\n" +
" 'DBMS'\n" +
");";
db.executeTransactionally(procedureVoid);
// This should run without exception
db.executeTransactionally("CALL custom.setTxMetadata($meta)", Map.of(
"meta", Map.of("foo", "bar")
));
}


private void assertProcedureFails(String expectedMessage, String query) {
Expand Down