Skip to content

In 5.26.3, apoc.paths.toJsonTree does not correctly map full path to tree #788

Description

@rossoneri8

Expected Behavior (Mandatory)

Expected response, currently coming back from apoc.convert.toTree. Note the inclusion of path C->D->E in tree

{
  "_type": "Node",
  "_id": 4357,
  "rels": [
    {
      "_type": "Node",
      "rels._id": 47200,
      "_id": 4358,
      "rels": [
        {
          "_type": "Node",
          "rels._id": 47202,
          "_id": 4360,
          "rels": [
            {
              "_type": "Node",
              "rels._id": 47204,
              "_id": 4361,
              "rels._elementId": "5:e3ade55d-9ea1-48cf-b308-75e756f0f38d:47204",
              "name": "E",
              "_elementId": "4:e3ade55d-9ea1-48cf-b308-75e756f0f38d:4361"
            }
          ],
          "rels._elementId": "5:e3ade55d-9ea1-48cf-b308-75e756f0f38d:47202",
          "name": "D",
          "_elementId": "4:e3ade55d-9ea1-48cf-b308-75e756f0f38d:4360"
        }
      ],
      "rels._elementId": "5:e3ade55d-9ea1-48cf-b308-75e756f0f38d:47200",
      "name": "B",
      "_elementId": "4:e3ade55d-9ea1-48cf-b308-75e756f0f38d:4358"
    },
    {
      "_type": "Node",
      "rels._id": 47201,
      "_id": 4359,
      "rels": [
        {
          "_type": "Node",
          "rels._id": 47203,
          "_id": 4360,
          "rels": [
            {
              "_type": "Node",
              "rels._id": 47204,
              "_id": 4361,
              "rels._elementId": "5:e3ade55d-9ea1-48cf-b308-75e756f0f38d:47204",
              "name": "E",
              "_elementId": "4:e3ade55d-9ea1-48cf-b308-75e756f0f38d:4361"
            }
          ],
          "rels._elementId": "5:e3ade55d-9ea1-48cf-b308-75e756f0f38d:47203",
          "name": "D",
          "_elementId": "4:e3ade55d-9ea1-48cf-b308-75e756f0f38d:4360"
        }
      ],
      "rels._elementId": "5:e3ade55d-9ea1-48cf-b308-75e756f0f38d:47201",
      "name": "C",
      "_elementId": "4:e3ade55d-9ea1-48cf-b308-75e756f0f38d:4359"
    }
  ],
  "name": "A",
  "_elementId": "4:e3ade55d-9ea1-48cf-b308-75e756f0f38d:4357"
}

Actual Behavior (Mandatory)

Unexpected response, currently coming back from apoc.paths.toJsonTree. Note the exclusion of path C->D->E in tree

{
  "_type": "Node",
  "_id": 4357,
  "rels": [
    {
      "_type": "Node",
      "rels._id": 47200,
      "_id": 4358,
      "rels": [
        {
          "_type": "Node",
          "rels._id": 47202,
          "_id": 4360,
          "rels": [
            {
              "_type": "Node",
              "rels._id": 47204,
              "_id": 4361,
              "rels._elementId": "5:e3ade55d-9ea1-48cf-b308-75e756f0f38d:47204",
              "name": "E",
              "_elementId": "4:e3ade55d-9ea1-48cf-b308-75e756f0f38d:4361"
            }
          ],
          "rels._elementId": "5:e3ade55d-9ea1-48cf-b308-75e756f0f38d:47202",
          "name": "D",
          "_elementId": "4:e3ade55d-9ea1-48cf-b308-75e756f0f38d:4360"
        }
      ],
      "rels._elementId": "5:e3ade55d-9ea1-48cf-b308-75e756f0f38d:47200",
      "name": "B",
      "_elementId": "4:e3ade55d-9ea1-48cf-b308-75e756f0f38d:4358"
    },
    {
      "_type": "Node",
      "rels._id": 47201,
      "_id": 4359,
      "rels": [
        {
          "_type": "Node",
          "rels._id": 47203,
          "_id": 4360,
          "rels._elementId": "5:e3ade55d-9ea1-48cf-b308-75e756f0f38d:47203",
          "name": "D",
          "_elementId": "4:e3ade55d-9ea1-48cf-b308-75e756f0f38d:4360"
        }
      ],
      "rels._elementId": "5:e3ade55d-9ea1-48cf-b308-75e756f0f38d:47201",
      "name": "C",
      "_elementId": "4:e3ade55d-9ea1-48cf-b308-75e756f0f38d:4359"
    }
  ],
  "name": "A",
  "_elementId": "4:e3ade55d-9ea1-48cf-b308-75e756f0f38d:4357"
}

How to Reproduce the Problem

Follow steps outlined in Steps, i.e. create the simple dataset and then query it using the following query utilizing apoc.paths.toJsonTree

MATCH path=((:Node)-[:RELS]->(:Node)-[:RELS]->(:Node)-[:RELS]->(:Node))
WITH collect(path) as paths
CALL apoc.paths.toJsonTree(paths, true) YIELD value
RETURN value as tree

Simple Dataset (where it's possibile)

MERGE (a:Node {name:'A'}) 
MERGE (b:Node {name:'B'}) 
MERGE (c:Node {name:'C'}) 
MERGE (d:Node {name:'D'}) 
MERGE (e:Node {name:'E'}) 

MERGE (a)-[:RELS]->(b)
MERGE (a)-[:RELS]->(c)
MERGE (b)-[:RELS]->(d)
MERGE (c)-[:RELS]->(d)
MERGE (d)-[:RELS]->(e)

Steps (Mandatory)

  1. Create DB to match initial state
  2. Run the following query and note the lack of relationship from nodes C->D->E in tree
MATCH path=((:Node)-[:RELS]->(:Node)-[:RELS]->(:Node)-[:RELS]->(:Node))
WITH collect(path) as paths
CALL apoc.paths.toJsonTree(paths, true) YIELD value
RETURN value as tree
  1. Run the following deprecated query and note the inclusion of nodes C->D->E in tree
MATCH path=((:Node)-[:RELS]->(:Node)-[:RELS]->(:Node)-[:RELS]->(:Node))
WITH collect(path) as paths
CALL apoc.convert.toTree(paths, true) YIELD value
RETURN value as tree

Screenshots (where it's possibile)

Image

Specifications (Mandatory)

Currently used versions

Versions

  • OS: docker, neo4j:5.26.3
  • Neo4j: 5.26.3
  • Neo4j-Apoc: 5.26.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions