Skip to content

fix(contexts): keep the nodes added by an application update in the loaded shape - #3230

Open
rubenvdlinde wants to merge 2 commits into
mainfrom
fix/noid/context-update-added-node-shape
Open

rubenvdlinde wants to merge 2 commits into
mainfrom
fix/noid/context-update-added-node-shape

Conversation

@rubenvdlinde

Copy link
Copy Markdown
Contributor

Adding a table or view to an application through PUT /api/2/contexts/{id} logs a PHP warning, Undefined array key "node_type", and the new table doesn't take its place on the start page.

Size: 60 lines: 13 code and 47 tests.

What changes

ContextService::update() keeps the nodes and start page contents of the application in two arrays while it works through the submitted list. The loaded ones come from ContextMapper with snake case keys (node_type, node_id, node_rel_id). Nodes added during the update went in through jsonSerialize(), which uses camel case (nodeType, nodeId, nodeRelId). Ordering the start page afterwards reads the snake case keys, so for an added node it:

  • raised Undefined array key "node_type" and "node_id" for the node, and "node_rel_id" for its start page content,
  • couldn't match the node to its content, so the new content kept order 100 instead of the place it was submitted at,
  • returned an application whose nodes and pages mixed both shapes.

Added nodes and contents now go into those arrays in the same shape the mapper loads them in.

How it was tested

New test in ContextServiceTest: an application with one table gets a view added in front of it. The view comes back in the loaded shape, its start page content gets order 10, and the table moves to 20.

ContextServiceTest without the fix: Undefined array key "node_type" (Tests: 1, Errors: 1)
ContextServiceTest with the fix:    OK (7 tests, 18 assertions)
Full unit suite, PostgreSQL (Nextcloud 34): OK (423 tests, 837 assertions)
php-cs-fixer: no files to fix

🖼️ Screenshots

Backend only, no visual change.

🏁 Checklist

  • ⛑️ Tests (unit and/or integration) are included or not needed
  • 🔙 Backport requests are created or not needed: /backport to stableX.X
  • 📅 Milestone is set
  • 🌸 PR title is meaningful (if it should be in the changelog: is it meaningful to users?)

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI

@rubenvdlinde
rubenvdlinde marked this pull request as ready for review November 6, 2026 08:25

@AndyScherzinger AndyScherzinger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review focused on performance, security, clean code and architecture (backend only). The checks ran on a dev instance (MariaDB) with #3133 deployed and this commit applied on top, since the PR merges after #3133.

The fix is right: the hand-built arrays match ContextMapper::formatResultRows() key for key and type for type on main and on #3133, a PUT that adds a node logs no "Undefined array key" warning any more, its nodes and pages equal those of the following GET, and the added table takes its place on the start page (an application with table 1 that gets table 2 added now stores orders 10 and 20, where main stored 10 and 10).

Merging after #3133: the service change applies cleanly; the test file conflicts in setUp() in two hunks ($widgetService/$dbConnection against $permissionsService). Both resolve to #3133's side; keeping both declarations of $permissionsService is a fatal "Cannot redeclare".

CI: green.

Comment thread lib/Service/ContextService.php Outdated
@@ -259,8 +259,17 @@ public function update(int $contextId, string $userId, ?string $name, ?string $i
$node['order'] ?? 100,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added contents now take part in the reorder, which the description doesn't mention: an added node is inserted with $node['order'] ?? 100, then read again and rewritten to ($index + 1) * 10 by the reorder (via updateContentOrder() → updatePageContent()). The order field of PUT nodes, documented in ContextController::update() and required in openapi.json, is therefore ignored, and every added node costs a SELECT and an UPDATE on top of its INSERT (the UI never sends order, so every node added in the dialog pays this). Recording the position while classifying the nodes (foreach ($nodes as $index => $node) … $nodesBeingAdded[$key] = ['order' => ($index + 1) * 10] + $node;) and passing it here inserts the final order directly, so the reorder skips the row; then either drop order from the PUT contract (and run composer run openapi) or honour it.

Validated: reproduced on a deployed instance (MariaDB, #3133 at 4a7b8e3a with this PR's commit applied on top, PUT /api/2/contexts/{id} as alice: nodes [t1, t2, {id: 3, type: 0, permissions: 1, order: 5}]; the query log shows the INSERT with order 5, a SELECT by id and an UPDATE to 30. On main the content kept order 5).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You were right, the added node was written twice and its order was thrown away. The update now works out each node's order while it sorts the nodes: the order the client sends, or the node's position in the list when it sends none. Added nodes are inserted at that order, so the reorder after them leaves them alone, and kept nodes get the order they were sent as well. So order stays in the PUT contract and is now actually honoured.

cc @blizzz

$currentNodes[$addedNode->getId()] = $addedNode->jsonSerialize();
$currentPages[$updatedContent->getPageId()]['content'][$updatedContent->getId()] = $updatedContent->jsonSerialize();
// The same shape as the nodes and contents the mapper loads
$currentNodes[$addedNode->getId()] = [

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This copies ContextMapper::formatResultRows()' node and content literals by hand, and the test adds a third copy; only the comment ties them together, so the next key added to the mapper's shape (as #3133 just did for pages) brings PUT ≠ GET back. create() is a fourth copy that has already diverged: insertNodesFromArray() and insertPage() still return jsonSerialize(), so POST /contexts answers a camelCase node list and camelCase pages while GET and now PUT answer the snake_case maps (the same class of bug the commit message describes). One static formatter on ContextMapper (say nodeEntry()/contentEntry()), used by formatResultRows(), here and in create(), keeps one shape; aligning POST is an API decision, since Behat's createContext and #3133's ContextServiceTest pin its current shape.

Validated: code check, plus POST /contexts on the instance (nodes as a list with nodeId/nodeType, pages with pageType/technicalName).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that a comment is a weak link between the copies. A shared node and content formatter on ContextMapper would also change what POST answers, which is an API change and touches #3133, so I will open a separate issue for it instead of growing this fix. This PR keeps the shape the mapper loads, and the tests pin it.

cc @blizzz

];
$this->pageContentMapper->method('findById')
->willReturnCallback(static fn (int $contentId): PageContent => $pageContentsById[$contentId]);
$this->pageContentMapper->method('update')->willReturnArgument(0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test checks only the in-memory response: pageContentMapper->method('update')->willReturnArgument(0) has no expectation, and the insert stubs have no expects($this->once()). update() writes the new order into the returned array before updateContentOrder() stores it, and updateContentOrder() swallows failures, so all three assertions stay green if the updateContentOrder() call is deleted, or if content 502 sits on another page so that updatePageContent() throws; the third assertion also passes on main without the fix. testUpdateReordersStartPageContentsFromSubmittedNodes next to it records the writes (expects($this->exactly(2))->method('update') plus $updatedOrders); doing the same here and asserting [502 => 10, 501 => 20] (or [501 => 20] once the comment on ContextService.php line 259 inserts at the final order) would make it fail without the fix.

Validated: code check only.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, the test only checked what was in memory. The tests now record what gets written: one insert of the added content at its final order, and exactly one update, for the kept node that moved. A second test sends explicit order values for an added and a kept node and checks those are stored. Both fail with the service change reverted.

cc @blizzz

@github-actions

Copy link
Copy Markdown
Contributor

Hello there,
Thank you so much for taking the time and effort to create a pull request to our Nextcloud project.

We hope that the review process is going smooth and is helpful for you. We want to ensure your pull request is reviewed to your satisfaction. If you have a moment, our community management team would very much appreciate your feedback on your experience with this PR review process.

Your feedback is valuable to us as we continuously strive to improve our community developer experience. Please take a moment to complete our short survey by clicking on the following link: https://cloud.nextcloud.com/apps/forms/s/i9Ago4EQRZ7TWxjfmeEpPkf6

Thank you for contributing to Nextcloud and we hope to hear from you soon!

(If you believe you should not receive this message, you can add yourself to the blocklist.)

…oaded shape

Updating an application with new tables or views stored the added nodes and start page
contents in their JSON shape (nodeType, nodeId, nodeRelId) next to the loaded ones
(node_type, node_id, node_rel_id). Ordering the start page then read keys that were not
there, which raised an "Undefined array key" warning and left the new contents
unordered, and the returned application mixed both shapes.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
Added nodes are inserted at their final order, so the reorder after
them no longer rewrites their content. A node without an order keeps
its position in the list. The tests record the orders written to the
database.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/context-update-added-node-shape branch from 03e55ff to 9eb9d29 Compare November 21, 2026 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants