REST API: Incorrect ancestor order for child page?

Hi,

We’re currently replacing RPC calls in our tests with REST calls to ensure compatibility with Confluence 10.

I’m trying to create a simple space with the key TEST, containing two pages:

  • one titled parent, which is a child of the space’s home page, and
  • one titled child, which should be a child of the parent page.

The desired hierarchy looks like this:

home page  
└── parent  
    └── child

I’m using confluence-test-utils, which leverages Confluence’s official REST API. The code for creating a page is straightforward:

Content newPage =
                Content.builder(ContentType.PAGE)
                        .title(title)
                        .space(spaceKey)
                        .body(content, ContentRepresentation.STORAGE)
                        .parent(Content.builder(ContentType.PAGE, parentId).build())
                        .build();
restSession.contentService().create(newPage);

The space and its pages are created correctly, and the hierarchy is shown as expected in the UI. However, when I fetch the ancestors of the child page, I receive two entries: the home page and the parent page — but in that order. So child.getParentId() returns the ID of the home page, because it takes the first ancestor in the list.

In my understanding, this is incorrect. The direct parent of child should be the parent page, not the home page.

This functionality is important for us, as we rely on accurate parent-child relationships in our test logic. Is this a bug in the REST API? @Kusal Do you know who might be the right person to look into this?
Any help is much appreciated.

Thanks,
Daria

Hello @daria

I am assuming that you’re using the Get content by ID endpoint to fetch the ancestors of a page, then unfortunately, it’s an idiosyncrasy of that endpoint that it returns all the ancestors of a page.

The last entry in the results returned in the ancestors array is always the ‘lowest’ ancestor and therefore the parent page.

Interestingly, the Java API also assumes that getAncestors().get(0) is the parent (see com.atlassian.confluence.api.model.content.Content#getOptionalParent()), which aligns with the documentation — but not with the actual REST response order.
That makes this more than just an idiosyncrasy — it could lead to subtle bugs depending on how the data is consumed.

Another interesting observation: When fetching a page and updating it by merely incrementing the version number, the parent gets set incorrectly — since the first ancestor is always used, and that’s typically the homepage. This is definitely a bug.

Personally, I have a couple of scripts that create and move pages up and down the page hierarchy based on certain rules, and my code determines any page’s current parent by just getting the last object from the ancestors array. Works for me!

However, if you consider what you’ve encountered to be a bug, you’re more than welcome to raise it and see what traction you get.

Have fun

From what you’ve described it does indeed seem like a bug, and one which has existed for some time. Please do raise an issue for it.

It seems like you have a potential workaround in calling #getAncestors directly.