The method in DocumentModel getParentRef() is supposed to send null if the document has no parent.
It actually returns / when called on the root, due to the line:
Path parentPath = path.removeLastSegments(1);
When called in a recursive function, this produces an infinite loop.