Skip to content

Commit 9d89c21

Browse files
committed
Parameters can't be nodes, relationships or paths
Driver should not accept nodes, relationships nor paths to be sent over the wire.
1 parent 44269ba commit 9d89c21

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

driver/src/main/java/org/neo4j/driver/v1/Values.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343
public abstract class Values
4444
{
4545
public static final Value EmptyMap = value( Collections.emptyMap() );
46-
47-
public static Value NULL = NullValue.NULL;
46+
public static final Value NULL = NullValue.NULL;
4847

4948
private Values()
5049
{
@@ -271,7 +270,9 @@ public static Map<String,Value> parameters( Object... keysAndValues )
271270
HashMap<String,Value> map = new HashMap<>( keysAndValues.length / 2 );
272271
for ( int i = 0; i < keysAndValues.length; i += 2 )
273272
{
274-
map.put( keysAndValues[i].toString(), value( keysAndValues[i + 1] ) );
273+
Object value = keysAndValues[i + 1];
274+
assertParameter( value );
275+
map.put( keysAndValues[i].toString(), value( value ) );
275276
}
276277
return map;
277278
}
@@ -491,4 +492,21 @@ public Path apply( Value val )
491492
return val.asPath();
492493
}
493494
};
495+
496+
private static void assertParameter( Object value )
497+
{
498+
if ( value instanceof Node )
499+
{
500+
throw new ClientException( "Nodes can't be used as parameters." );
501+
}
502+
if ( value instanceof Relationship )
503+
{
504+
throw new ClientException( "Relationships can't be used as parameters." );
505+
}
506+
if ( value instanceof Path )
507+
{
508+
throw new ClientException( "Paths can't be used as parameters." );
509+
}
510+
511+
}
494512
}

driver/src/test/java/org/neo4j/driver/v1/integration/ParametersIT.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@
2222
import org.junit.Test;
2323
import org.junit.rules.ExpectedException;
2424

25+
import org.neo4j.driver.v1.Node;
26+
import org.neo4j.driver.v1.Path;
2527
import org.neo4j.driver.v1.Record;
28+
import org.neo4j.driver.v1.Relationship;
2629
import org.neo4j.driver.v1.ResultCursor;
2730
import org.neo4j.driver.v1.Value;
2831
import org.neo4j.driver.v1.exceptions.ClientException;
2932
import org.neo4j.driver.v1.util.TestNeo4jSession;
3033

3134
import static org.hamcrest.CoreMatchers.equalTo;
3235
import static org.hamcrest.MatcherAssert.assertThat;
33-
3436
import static org.neo4j.driver.v1.Values.parameters;
3537

3638
public class ParametersIT
@@ -387,4 +389,52 @@ public void settingInvalidParameterTypeShouldThrowHelpfulError() throws Throwabl
387389
// When
388390
session.run( "anything", parameters( "k", new Object() ) );
389391
}
392+
393+
@Test
394+
public void shouldNotBePossibleToUseNodeAsParameter()
395+
{
396+
// GIVEN
397+
ResultCursor cursor = session.run( "CREATE (a:Node) RETURN a" );
398+
cursor.first();
399+
Node node = cursor.value( 0 ).asNode();
400+
401+
//Expect
402+
exception.expect( ClientException.class );
403+
exception.expectMessage( "Nodes can't be used as parameters" );
404+
405+
// WHEN
406+
session.run( "RETURN {a}", parameters( "a", node ) );
407+
}
408+
409+
@Test
410+
public void shouldNotBePossibleToUseRelationshipAsParameter()
411+
{
412+
// GIVEN
413+
ResultCursor cursor = session.run( "CREATE (a:Node), (b:Node), (a)-[r:R]->(b) RETURN r" );
414+
cursor.first();
415+
Relationship relationship = cursor.value( 0 ).asRelationship();
416+
417+
//Expect
418+
exception.expect( ClientException.class );
419+
exception.expectMessage( "Relationships can't be used as parameters" );
420+
421+
// WHEN
422+
session.run( "RETURN {a}", parameters( "a", relationship ) );
423+
}
424+
425+
@Test
426+
public void shouldNotBePossibleToUsePathAsParameter()
427+
{
428+
// GIVEN
429+
ResultCursor cursor = session.run( "CREATE (a:Node), (b:Node), p=(a)-[r:R]->(b) RETURN p" );
430+
cursor.first();
431+
Path path = cursor.value( 0 ).asPath();
432+
433+
//Expect
434+
exception.expect( ClientException.class );
435+
exception.expectMessage( "Paths can't be used as parameters" );
436+
437+
// WHEN
438+
session.run( "RETURN {a}", parameters( "a", path ) );
439+
}
390440
}

driver/src/test/java/org/neo4j/driver/v1/util/Neo4jInstaller.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ public int runCommand( String... cmd ) throws IOException
101101
// Neo4j, however, works with a specific version of Java. This allows
102102
// specifying which Java version to use for Neo4j separately from which
103103
// version to use for the driver tests.
104-
env.containsKey( "NEO4J_JAVA" ) ? env.get( "NEO4J_JAVA" ) : env.get( "JAVA_HOME" ) );
104+
env.containsKey( "NEO4J_JAVA" ) ? env.get( "NEO4J_JAVA" ) :
105+
System.getProperties().getProperty( "java.home" ) );
105106
Process process = pb.command( cmd ).start();
106107
while (true)
107108
{

0 commit comments

Comments
 (0)