Skip to content

Parameters can't be nodes, relationships or paths #109

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

Merged
merged 1 commit into from
Jan 4, 2016
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
24 changes: 21 additions & 3 deletions driver/src/main/java/org/neo4j/driver/v1/Values.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
public abstract class Values
{
public static final Value EmptyMap = value( Collections.emptyMap() );

public static Value NULL = NullValue.NULL;
public static final Value NULL = NullValue.NULL;

private Values()
{
Expand Down Expand Up @@ -271,7 +270,9 @@ public static Map<String,Value> parameters( Object... keysAndValues )
HashMap<String,Value> map = new HashMap<>( keysAndValues.length / 2 );
for ( int i = 0; i < keysAndValues.length; i += 2 )
{
map.put( keysAndValues[i].toString(), value( keysAndValues[i + 1] ) );
Object value = keysAndValues[i + 1];
assertParameter( value );
map.put( keysAndValues[i].toString(), value( value ) );
}
return map;
}
Expand Down Expand Up @@ -491,4 +492,21 @@ public Path apply( Value val )
return val.asPath();
}
};

private static void assertParameter( Object value )
{
if ( value instanceof Node )
{
throw new ClientException( "Nodes can't be used as parameters." );
}
if ( value instanceof Relationship )
{
throw new ClientException( "Relationships can't be used as parameters." );
}
if ( value instanceof Path )
{
throw new ClientException( "Paths can't be used as parameters." );
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;

import org.neo4j.driver.v1.Node;
import org.neo4j.driver.v1.Path;
import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.Relationship;
import org.neo4j.driver.v1.ResultCursor;
import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.util.TestNeo4jSession;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import static org.neo4j.driver.v1.Values.parameters;

public class ParametersIT
Expand Down Expand Up @@ -387,4 +389,52 @@ public void settingInvalidParameterTypeShouldThrowHelpfulError() throws Throwabl
// When
session.run( "anything", parameters( "k", new Object() ) );
}

@Test
public void shouldNotBePossibleToUseNodeAsParameter()
{
// GIVEN
ResultCursor cursor = session.run( "CREATE (a:Node) RETURN a" );
cursor.first();
Node node = cursor.value( 0 ).asNode();

//Expect
exception.expect( ClientException.class );
exception.expectMessage( "Nodes can't be used as parameters" );

// WHEN
session.run( "RETURN {a}", parameters( "a", node ) );
}

@Test
public void shouldNotBePossibleToUseRelationshipAsParameter()
{
// GIVEN
ResultCursor cursor = session.run( "CREATE (a:Node), (b:Node), (a)-[r:R]->(b) RETURN r" );
cursor.first();
Relationship relationship = cursor.value( 0 ).asRelationship();

//Expect
exception.expect( ClientException.class );
exception.expectMessage( "Relationships can't be used as parameters" );

// WHEN
session.run( "RETURN {a}", parameters( "a", relationship ) );
}

@Test
public void shouldNotBePossibleToUsePathAsParameter()
{
// GIVEN
ResultCursor cursor = session.run( "CREATE (a:Node), (b:Node), p=(a)-[r:R]->(b) RETURN p" );
cursor.first();
Path path = cursor.value( 0 ).asPath();

//Expect
exception.expect( ClientException.class );
exception.expectMessage( "Paths can't be used as parameters" );

// WHEN
session.run( "RETURN {a}", parameters( "a", path ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public int runCommand( String... cmd ) throws IOException
// Neo4j, however, works with a specific version of Java. This allows
// specifying which Java version to use for Neo4j separately from which
// version to use for the driver tests.
env.containsKey( "NEO4J_JAVA" ) ? env.get( "NEO4J_JAVA" ) : env.get( "JAVA_HOME" ) );
env.containsKey( "NEO4J_JAVA" ) ? env.get( "NEO4J_JAVA" ) :
System.getProperties().getProperty( "java.home" ) );
Process process = pb.command( cmd ).start();
while (true)
{
Expand Down