Skip to content

Commit eddabdb

Browse files
authored
Merge pull request #482 from zhenlineo/1.6-param-types
Adding error check of unsupported types
2 parents 7c5570d + 76afa48 commit eddabdb

File tree

14 files changed

+602
-204
lines changed

14 files changed

+602
-204
lines changed

driver/src/main/java/org/neo4j/driver/internal/messaging/PackStreamMessageFormatV1.java

Lines changed: 1 addition & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.neo4j.driver.internal.value.RelationshipValue;
4444
import org.neo4j.driver.v1.Value;
4545
import org.neo4j.driver.v1.exceptions.ClientException;
46-
import org.neo4j.driver.v1.types.Entity;
4746
import org.neo4j.driver.v1.types.Node;
4847
import org.neo4j.driver.v1.types.Path;
4948
import org.neo4j.driver.v1.types.Relationship;
@@ -252,79 +251,8 @@ void packInternalValue( InternalValue value ) throws IOException
252251
}
253252
break;
254253

255-
case NODE:
256-
{
257-
Node node = value.asNode();
258-
packNode( node );
259-
}
260-
break;
261-
262-
case RELATIONSHIP:
263-
{
264-
Relationship rel = value.asRelationship();
265-
packer.packStructHeader( 5, RELATIONSHIP );
266-
packer.pack( rel.id() );
267-
packer.pack( rel.startNodeId() );
268-
packer.pack( rel.endNodeId() );
269-
270-
packer.pack( rel.type() );
271-
272-
packProperties( rel );
273-
}
274-
break;
275-
276-
case PATH:
277-
Path path = value.asPath();
278-
packer.packStructHeader( 3, PATH );
279-
280-
// Unique nodes
281-
Map<Node,Integer> nodeIdx = Iterables.newLinkedHashMapWithSize( path.length() + 1 );
282-
for ( Node node : path.nodes() )
283-
{
284-
if ( !nodeIdx.containsKey( node ) )
285-
{
286-
nodeIdx.put( node, nodeIdx.size() );
287-
}
288-
}
289-
packer.packListHeader( nodeIdx.size() );
290-
for ( Node node : nodeIdx.keySet() )
291-
{
292-
packNode( node );
293-
}
294-
295-
// Unique rels
296-
Map<Relationship,Integer> relIdx = Iterables.newLinkedHashMapWithSize( path.length() );
297-
for ( Relationship rel : path.relationships() )
298-
{
299-
if ( !relIdx.containsKey( rel ) )
300-
{
301-
relIdx.put( rel, relIdx.size() + 1 );
302-
}
303-
}
304-
packer.packListHeader( relIdx.size() );
305-
for ( Relationship rel : relIdx.keySet() )
306-
{
307-
packer.packStructHeader( 3, UNBOUND_RELATIONSHIP );
308-
packer.pack( rel.id() );
309-
packer.pack( rel.type() );
310-
packProperties( rel );
311-
}
312-
313-
// Sequence
314-
packer.packListHeader( path.length() * 2 );
315-
for ( Path.Segment seg : path )
316-
{
317-
Relationship rel = seg.relationship();
318-
long relEndId = rel.endNodeId();
319-
long segEndId = seg.end().id();
320-
int size = relEndId == segEndId ? relIdx.get( rel ) : -relIdx.get( rel );
321-
packer.pack( size );
322-
packer.pack( nodeIdx.get( seg.end() ) );
323-
}
324-
break;
325-
326254
default:
327-
throw new IOException( "Unknown type: " + value );
255+
throw new IOException( "Unknown type: " + value.type().name() );
328256
}
329257
}
330258

@@ -333,32 +261,6 @@ public void write( Message msg ) throws IOException
333261
{
334262
msg.dispatch( this );
335263
}
336-
337-
private void packNode( Node node ) throws IOException
338-
{
339-
packer.packStructHeader( NODE_FIELDS, NODE );
340-
packer.pack( node.id() );
341-
342-
Iterable<String> labels = node.labels();
343-
packer.packListHeader( Iterables.count( labels ) );
344-
for ( String label : labels )
345-
{
346-
packer.pack( label );
347-
}
348-
349-
packProperties( node );
350-
}
351-
352-
private void packProperties( Entity entity ) throws IOException
353-
{
354-
Iterable<String> keys = entity.keys();
355-
packer.packMapHeader( entity.size() );
356-
for ( String propKey : keys )
357-
{
358-
packer.pack( propKey );
359-
packValue( entity.get( propKey ) );
360-
}
361-
}
362264
}
363265

364266
static class ReaderV1 implements MessageFormat.Reader

driver/src/main/java/org/neo4j/driver/internal/messaging/PackStreamMessageFormatV2.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public MessageFormat.Reader newReader( PackInput input )
8686
return new ReaderV2( input );
8787
}
8888

89-
private static class WriterV2 extends WriterV1
89+
static class WriterV2 extends WriterV1
9090
{
9191
WriterV2( PackOutput output )
9292
{
@@ -226,7 +226,7 @@ private void packPoint3D( Point point ) throws IOException
226226
}
227227
}
228228

229-
private static class ReaderV2 extends ReaderV1
229+
static class ReaderV2 extends ReaderV1
230230
{
231231
ReaderV2( PackInput input )
232232
{

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import java.util.Map;
2222

23+
import org.neo4j.driver.internal.value.MapValue;
24+
import org.neo4j.driver.v1.exceptions.ClientException;
2325
import org.neo4j.driver.v1.summary.ResultSummary;
2426
import org.neo4j.driver.v1.util.Immutable;
2527

@@ -52,7 +54,18 @@ public class Statement
5254
public Statement( String text, Value parameters )
5355
{
5456
this.text = text;
55-
this.parameters = parameters == null ? Values.EmptyMap : parameters;
57+
if( parameters == null )
58+
{
59+
this.parameters = Values.EmptyMap;
60+
}
61+
else if ( parameters instanceof MapValue )
62+
{
63+
this.parameters = parameters;
64+
}
65+
else
66+
{
67+
throw new ClientException( "The parameters should be provided as Map type. Unsupported parameters type: " + parameters.type().name() );
68+
}
5669
}
5770

5871
/**

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@
4949
import org.neo4j.driver.internal.value.LocalDateTimeValue;
5050
import org.neo4j.driver.internal.value.LocalTimeValue;
5151
import org.neo4j.driver.internal.value.MapValue;
52+
import org.neo4j.driver.internal.value.NodeValue;
5253
import org.neo4j.driver.internal.value.NullValue;
54+
import org.neo4j.driver.internal.value.PathValue;
5355
import org.neo4j.driver.internal.value.PointValue;
56+
import org.neo4j.driver.internal.value.RelationshipValue;
5457
import org.neo4j.driver.internal.value.StringValue;
5558
import org.neo4j.driver.internal.value.TimeValue;
5659
import org.neo4j.driver.v1.exceptions.ClientException;
@@ -91,6 +94,7 @@ public static Value value( Object value )
9194
{
9295
if ( value == null ) { return NullValue.NULL; }
9396

97+
assertParameter( value );
9498
if ( value instanceof AsValue ) { return ((AsValue) value).asValue(); }
9599
if ( value instanceof Boolean ) { return value( (boolean) value ); }
96100
if ( value instanceof String ) { return value( (String) value ); }
@@ -381,7 +385,6 @@ public static Value parameters( Object... keysAndValues )
381385
for ( int i = 0; i < keysAndValues.length; i += 2 )
382386
{
383387
Object value = keysAndValues[i + 1];
384-
assertParameter( value );
385388
map.put( keysAndValues[i].toString(), value( value ) );
386389
}
387390
return value( map );
@@ -666,15 +669,15 @@ public static <T> Function<Value,List<T>> ofList( final Function<Value,T> innerM
666669

667670
private static void assertParameter( Object value )
668671
{
669-
if ( value instanceof Node )
672+
if ( value instanceof Node || value instanceof NodeValue )
670673
{
671674
throw new ClientException( "Nodes can't be used as parameters." );
672675
}
673-
if ( value instanceof Relationship )
676+
if ( value instanceof Relationship || value instanceof RelationshipValue )
674677
{
675678
throw new ClientException( "Relationships can't be used as parameters." );
676679
}
677-
if ( value instanceof Path )
680+
if ( value instanceof Path || value instanceof PathValue )
678681
{
679682
throw new ClientException( "Paths can't be used as parameters." );
680683
}

driver/src/test/java/org/neo4j/driver/internal/ValuesTest.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,18 @@
4646
import org.neo4j.driver.internal.value.LocalDateTimeValue;
4747
import org.neo4j.driver.internal.value.LocalTimeValue;
4848
import org.neo4j.driver.internal.value.MapValue;
49+
import org.neo4j.driver.internal.value.NodeValue;
50+
import org.neo4j.driver.internal.value.PathValue;
51+
import org.neo4j.driver.internal.value.RelationshipValue;
4952
import org.neo4j.driver.internal.value.TimeValue;
5053
import org.neo4j.driver.v1.Value;
5154
import org.neo4j.driver.v1.Values;
5255
import org.neo4j.driver.v1.exceptions.ClientException;
5356
import org.neo4j.driver.v1.types.IsoDuration;
57+
import org.neo4j.driver.v1.types.Node;
58+
import org.neo4j.driver.v1.types.Path;
5459
import org.neo4j.driver.v1.types.Point;
60+
import org.neo4j.driver.v1.types.Relationship;
5561

5662
import static java.util.Arrays.asList;
5763
import static org.hamcrest.CoreMatchers.equalTo;
@@ -60,6 +66,9 @@
6066
import static org.junit.Assert.assertEquals;
6167
import static org.junit.Assert.assertNotEquals;
6268
import static org.junit.Assert.assertThat;
69+
import static org.neo4j.driver.internal.util.ValueFactory.emptyNodeValue;
70+
import static org.neo4j.driver.internal.util.ValueFactory.emptyRelationshipValue;
71+
import static org.neo4j.driver.internal.util.ValueFactory.filledPathValue;
6372
import static org.neo4j.driver.v1.Values.isoDuration;
6473
import static org.neo4j.driver.v1.Values.ofDouble;
6574
import static org.neo4j.driver.v1.Values.ofFloat;
@@ -483,4 +492,76 @@ public void shouldCreateValueFromPoint3D()
483492
assertEquals( point3D, point3DValue2.asPoint() );
484493
assertEquals( point3DValue1, point3DValue2 );
485494
}
495+
496+
@Test
497+
public void shouldComplainAboutNodeValueType() throws Throwable
498+
{
499+
// Expect
500+
exception.expect( ClientException.class );
501+
exception.expectMessage( "Nodes can't be used as parameters." );
502+
503+
// When
504+
NodeValue node = emptyNodeValue();
505+
value( node );
506+
}
507+
508+
@Test
509+
public void shouldComplainAboutNodeType() throws Throwable
510+
{
511+
// Expect
512+
exception.expect( ClientException.class );
513+
exception.expectMessage( "Nodes can't be used as parameters." );
514+
515+
// When
516+
Node node = emptyNodeValue().asNode();
517+
value( node );
518+
}
519+
520+
@Test
521+
public void shouldComplainAboutRelationshipValueType() throws Throwable
522+
{
523+
// Expect
524+
exception.expect( ClientException.class );
525+
exception.expectMessage( "Relationships can't be used as parameters." );
526+
527+
// When
528+
RelationshipValue rel = emptyRelationshipValue();
529+
value( rel );
530+
}
531+
532+
@Test
533+
public void shouldComplainAboutRelationshipType() throws Throwable
534+
{
535+
// Expect
536+
exception.expect( ClientException.class );
537+
exception.expectMessage( "Relationships can't be used as parameters." );
538+
539+
// When
540+
Relationship rel = emptyRelationshipValue().asRelationship();
541+
value( rel );
542+
}
543+
544+
@Test
545+
public void shouldComplainAboutPathValueType() throws Throwable
546+
{
547+
// Expect
548+
exception.expect( ClientException.class );
549+
exception.expectMessage( "Paths can't be used as parameters." );
550+
551+
// When
552+
PathValue path = filledPathValue();
553+
value( path );
554+
}
555+
556+
@Test
557+
public void shouldComplainAboutPathType() throws Throwable
558+
{
559+
// Expect
560+
exception.expect( ClientException.class );
561+
exception.expectMessage( "Paths can't be used as parameters." );
562+
563+
// When
564+
Path path = filledPathValue().asPath();
565+
value( path );
566+
}
486567
}

0 commit comments

Comments
 (0)