neo4j/neo4j — pull request #2488 by nigelsmall (2014-05-31 16:04:16+00:00)
Title: Added "directions" to REST path repr

The existing representation for Paths returned over the REST interface provides no information with which to identify relationships within the path that run backwards. To that end, the following two paths return identical representations:

```
(A)-[:LOVES]->(B)-[:HATES]->(C)-[:KNOWS]->(D)
(A)-[:LOVES]->(B)<-[:HATES]-(C)-[:KNOWS]->(D)
```

This is easily seen with the following `curl` calls:

```bash
$ curl -X POST http://localhost:7474/db/data/cypher -H 'Content-Type:application/json' -d '{"query":"CREATE p=(a)-[:LOVES]->(b)-[:HATES]->(c)-[:KNOWS]->(d) RETURN p"}'
{
"columns" : [ "p" ],
"data" : [ [ {
"start" : "http://localhost:7474/db/data/node/0",
"nodes" : [ "http://localhost:7474/db/data/node/0", "http://localhost:7474/db/data/node/1", "http://localhost:7474/db/data/node/2", "http://localhost:7474/db/data/node/3" ],
"length" : 3,
"relationships" : [ "http://localhost:7474/db/data/relationship/0", "http://localhost:7474/db/data/relationship/1", "http://localhost:7474/db/data/relationship/2" ],
"end" : "http://localhost:7474/db/data/node/3"
} ] ]
}
```
```bash
$ curl -X POST http://localhost:7474/db/data/cypher -H 'Content-Type:application/json' -d '{"query":"CREATE p=(a)-[:LOVES]->(b)<-[:HATES]-(c)-[:KNOWS]->(d) RETURN p"}'
{
"columns" : [ "p" ],
"data" : [ [ {
"start" : "http://localhost:7474/db/data/node/4",
"nodes" : [ "http://localhost:7474/db/data/node/4", "http://localhost:7474/db/data/node/5", "http://localhost:7474/db/data/node/6", "http://localhost:7474/db/data/node/7" ],
"length" : 3,
"relationships" : [ "http://localhost:7474/db/data/relationship/3", "http://localhost:7474/db/data/relationship/4", "http://localhost:7474/db/data/relationship/5" ],
"end" : "http://localhost:7474/db/data/node/7"
} ] ]
}
```

Therefore, without this information, clients need to make additional REST calls to learn the exact "shape" of the path.

This patch provides an extra `directions` key in the JSON representation that contains a list of strings as its value. Each string gives the direction of its corresponding relationship by containing either `->` or `<-` for forward and backward relationships respectively. This gives output such as:

```bash
$ curl -X POST http://localhost:7474/db/data/cypher -H 'Content-Type:application/json' -d '{"query":"CREATE p=(a)-[:LOVES]->(b)<-[:HATES]-(c)-[:KNOWS]->(d) RETURN p"}'
{
"columns" : [ "p" ],
"data" : [ [ {
"start" : "http://localhost:7474/db/data/node/4",
"nodes" : [ "http://localhost:7474/db/data/node/4", "http://localhost:7474/db/data/node/5", "http://localhost:7474/db/data/node/6", "http://localhost:7474/db/data/node/7" ],
"length" : 3,
"relationships" : [ "http://localhost:7474/db/data/relationship/3", "http://localhost:7474/db/data/relationship/4", "http://localhost:7474/db/data/relationship/5" ],
"end" : "http://localhost:7474/db/data/node/7"
"directions": [ "->", "<-", "->" ]
} ] ]
}
```

Extra unit tests have also been added for the `PathRepresentation` class as these did not appear to previously exist.