Skip to content

fix(db): type the integer columns of applications, node relations and page contents - #3203

Open
rubenvdlinde wants to merge 5 commits into
feat/noid/app-menu-export-importfrom
fix/noid/entity-addtype-names
Open

rubenvdlinde wants to merge 5 commits into
feat/noid/app-menu-export-importfrom
fix/noid/entity-addtype-names

Conversation

@rubenvdlinde

@rubenvdlinde rubenvdlinde commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Sits on #3138 (menu export and import), though the bug is older and on main too. Context, ContextNodeRelation and PageContent now register their field types under the property names, so the integer types they declare actually apply.

Size: 123 changed lines: 53 code and 70 tests.

What was wrong

Entity::addType() takes a property name: Entity::setter() and QBMapper::getParameterTypeForProperty() both look the type up by property. These three entities registered theirs under the column names (owner_type, context_id, node_id, node_type, page_id, node_rel_id), so:

  • getFieldTypes() listed six names that are not properties, and no type for ownerType, contextId, nodeId, nodeType, pageId and nodeRelId
  • the mapper bound those columns as strings when inserting and updating

Values read back from the database were already integers, because the properties are typed ?int and PHP converts a numeric string on assignment. A non-numeric value would still throw a TypeError there instead of being cast.

What changes

The six addType() calls use the property names. Context had the same pattern for owner_type, so it's included.

The scope grew from there:

  • four more entities register their types with addType()
  • the mapper queries bind integers with IQueryBuilder::PARAM_INT
  • the import accepts only integer node types and the user owner type

How it was tested

On Nextcloud 34 with PostgreSQL, in EntityVirtualPropertiesTest, for every entity:

  • every registered field type names a property
  • every integer, bigint, smallint and boolean column has a field type

Without the change this fails for seven entities: Column, Context, ContextNodeRelation, Page, PageContent, RowCellUsergroup and View. Two tests in ContextServiceTest cover the import refusing a node type or owner type that isn't an integer constant. The full unit suite passes on PostgreSQL (754 tests), and psalm is clean.

🖼️ Screenshots

No UI changes.

🏁 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 force-pushed the feat/noid/app-menu-export-import branch from a1b94a7 to 2471ff4 Compare November 3, 2026 14:51
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/entity-addtype-names branch from 877a03b to 05b7968 Compare November 3, 2026 14:51
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from 2471ff4 to 6368c26 Compare November 4, 2026 12:35
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/entity-addtype-names branch from 05b7968 to 2f29f7a Compare November 4, 2026 12:36
@rubenvdlinde
rubenvdlinde marked this pull request as ready for review November 6, 2026 08:24
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from 6368c26 to c203b3a Compare November 10, 2026 09:29
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/entity-addtype-names branch from 2f29f7a to d3b372b Compare November 10, 2026 09:29
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from c203b3a to 5cf92cf Compare November 10, 2026 10:13
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/entity-addtype-names branch from d3b372b to c3582d0 Compare November 10, 2026 10:14

@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 this PR on top of #3138.

The fix is correct, and nothing compares these getters with strings. Reads don't change: OCP's Entity.php has no strict_types, so the ?int properties already turned the database strings into ints under the old keys. On the instance, GET /contexts/{id} returns ints for ownerType, node_id, node_type, node_rel_id, order and permissions with and without this PR. What changes is the bind type on insert and update, and the import behaviour in the comment on ContextNodeRelation.php line 34.

Not commented inline (files not in this PR):

  • The same gap remains in other entities. Page::$contextId (Page.php line 36) has no addType, so PageMapper still binds tables_contexts_page.context_id as PARAM_STR. The same holds for View::$externalId, RowCellUsergroup::$valueType and Column::$orderWeight. A reflection scan of every entity in lib/Db on the instance finds exactly these four integer properties without a field type.
  • The custom queries of the same three mappers still bind these columns as strings, because createNamedParameter() is called without IQueryBuilder::PARAM_INT: PageContentMapper.php lines 34, 47-48 and 61, ContextNodeRelationMapper.php lines 38-39 and 64, and ContextMapper.php lines 270-271. Nothing fails today, since every supported database casts the parameter, but they sit next to PARAM_INT for the same columns in the same files. Adding the type there would make the commit message ("the mapper bound these columns as strings") hold for reads too; otherwise narrow the message to insert and update.

Validated: code check only, plus the reflection scan of lib/Db on the instance.

$this->addType('node_type', 'integer');
$this->addType('contextId', 'integer');
$this->addType('nodeId', 'integer');
$this->addType('nodeType', 'integer');

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.

Registering nodeType (and ownerType in Context.php line 48) makes Entity::setter() run settype() on the value. ContextService::importContext() passes the raw contexts.json values to setOwnerType() and setNodeType() without checking them, so archive values that used to abort the import are now coerced silently. Before, a string such as "view" hit the ?int property, threw a TypeError, and TablesMigrator::import() rolled the whole import back. Now "view" is stored as 0, a table node. "table" is stored as 0 as well, but then fails the is_numeric() guard, so the table's widget sources and its menu item are dropped without a warning. ["x"] becomes 1, and an ownerType of [7] becomes 1, which canManageContextById() later refuses as an unsupported owner type. Accepting only the integers NODE_TYPE_TABLE/NODE_TYPE_VIEW and OWNER_TYPE_USER at the import, and skipping anything else with a warning as importedKey()/importedText() do, keeps the strictness.

Validated: reproduced on a deployed instance (MariaDB, S-3138 restored and this PR at c3582d02: an archive whose application node has node_type: "view", imported into alice with occ user:import; the import succeeded and the relation was stored with node_type 0). The behaviour before this PR: 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.

You are right, the type registration made the import quietly accept values it used to refuse. The import now takes only the integer table and view node types and the user owner type, and skips anything else with a warning, so "view" no longer turns into a table node. Two tests in ContextServiceTest cover a bad node type and a bad owner type.

cc @blizzz

Comment thread tests/unit/Db/EntityFieldTypesTest.php Outdated
*/
private function mapperFor(string $class): QBMapper {
return match ($class) {
Context::class => new ContextMapper($this->connectionAdapter, $this->createMock(UserHelper::class), $this->createMock(GroupHelper::class), new MenuItemMapper($this->connectionAdapter)),

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.

ContextMapper::__construct() takes three parameters (IDBConnection, UserHelper, GroupHelper), here and on main. PHP drops the fourth, new MenuItemMapper(...), silently, and the import on line 16 exists only for it. It is dead code. It also breaks the backport the description invites (the bug is on main too, and the backport box is open): MenuItemMapper doesn't exist on main or on the stable branches, so the cherry-picked test fails with "Class OCA\Tables\Db\MenuItemMapper not found".

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.

Thanks, that argument was dead and would have broken the backport. It is gone, together with the whole test file.

cc @blizzz

Comment thread tests/unit/Db/EntityFieldTypesTest.php Outdated
}
}

public function testANodeRelationAndAPageContentAreReadBackAsIntegers(): void {

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 test passes with or without the fix, as the description concedes ("6 of 7"). The ?int properties already coerce the database strings, so it guards nothing, and it costs two inserts, two selects and two deletes against the live database on every non-local run. Its setup and teardown are also asymmetric: both inserts run before the try, so a failing PageContent insert leaves the relation row behind, and an exception from delete($content) in finally skips delete($relation). In non-local mode DatabaseTestCase uses the instance's real connection without a transaction, so that orphan stays. The class docblock's "written and read as integers" isn't asserted by anything else. Dropping the test removes all of this.

Validated: code check only, plus the read-back on the instance's MariaDB with the old keys, which returns ints.

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, it guarded nothing and could leave rows behind. I removed it.

cc @blizzz

Comment thread tests/unit/Db/EntityFieldTypesTest.php Outdated
* @param class-string<Entity> $class
* @param list<string> $properties
*/
public function testTheMapperBindsTheIntegerColumnsAsIntegers(string $class, array $properties): void {

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 re-tests Nextcloud's QBMapper through reflection on a protected framework method. getParameterTypeForProperty() reads only $entity->getFieldTypes(), which the test above already asserts, so both tests fail on exactly the same keys and this one catches nothing extra. It brings mapperFor(), three mapper constructions, two mocks and six imports, about 30 of the file's 107 lines, and ties the suite to a protected server method. Without it and the read-back test, nothing here needs the database, so the class can extend PHPUnit's TestCase like WidgetTest, TableTest and ViewTest next to it, rather than DatabaseTestCase, whose first setUp() runs every app migration when the file runs alone.

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.

You are right, it only repeated the field type check through a protected server method. It is removed, and the file it lived in is gone.

cc @blizzz

Comment thread tests/unit/Db/EntityFieldTypesTest.php Outdated
/**
* @return array<string, array{class-string<Entity>, list<string>}>
*/
public static function integerProperties(): array {

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 provider lists three entities by hand, but the general rule checked on line 50 (every field-type key is a property) belongs to every entity. EntityVirtualPropertiesTest already discovers all 21 entities and loads their columns in checkEntityClass(). Two assertions there, that every field-type key is a property and that every integer or boolean column has a field type, cover all entities, fail on the base tree on exactly the three this PR fixes, and would also have caught the four in the review body. As it is, a future column-named addType in any of the other 18 entities stays green.

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.

Good idea, that test already knows every entity and its columns. I moved both checks into EntityVirtualPropertiesTest and dropped the new file. On the tree before this PR it failed on exactly the three entities this PR fixes and the four you found, and all of them are typed now.

cc @blizzz

@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from 5cf92cf to ee0564c Compare November 13, 2026 21:04
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/entity-addtype-names branch from c3582d0 to 835dc1f Compare November 13, 2026 21:04
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from ee0564c to 00c1b92 Compare November 16, 2026 09:23
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/entity-addtype-names branch from 835dc1f to 53b72ca Compare November 16, 2026 09:23
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from 00c1b92 to 6ffaf94 Compare November 17, 2026 09:11
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/entity-addtype-names branch from 53b72ca to a8dedc7 Compare November 17, 2026 09:12
@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.)

@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from 6ffaf94 to f7549c7 Compare November 19, 2026 08:28
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/entity-addtype-names branch from a8dedc7 to de1cdd9 Compare November 19, 2026 08:28
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from f7549c7 to b464a42 Compare November 19, 2026 13:36
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/entity-addtype-names branch from de1cdd9 to 1898aac Compare November 19, 2026 13:36
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from b464a42 to c0627d7 Compare November 20, 2026 10:53
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/entity-addtype-names branch from 1898aac to 2b6795c Compare November 20, 2026 10:53
@rubenvdlinde

Copy link
Copy Markdown
Contributor Author

Thanks for the scan. I typed the four other integer properties you found (Page::$contextId, View::$externalId, Column::$orderWeight, RowCellUsergroup::$valueType) and added PARAM_INT to the custom queries of the three mappers, so the commit message now holds for reads too.

cc @blizzz

… page contents

`Context`, `ContextNodeRelation` and `PageContent` registered their
field types under the column names (`owner_type`, `context_id`,
`node_id`, `node_type`, `page_id`, `node_rel_id`), while the entity
looks them up by property name. The casts never applied and the mapper
bound these columns as strings. They are now registered by property
name.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
EntityVirtualPropertiesTest now asserts for every entity that each
registered field type names a property and that each integer or boolean
column has a field type. It replaces EntityFieldTypesTest, which listed
three entities by hand and wrote to the database.

Assisted-by: ClaudeCode:claude-opus-5-5

Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
…r group cells

Page::$contextId, View::$externalId, Column::$orderWeight and
RowCellUsergroup::$valueType had no field type, so the mapper bound
them as strings and reads returned them untyped.

Assisted-by: ClaudeCode:claude-opus-5-5

Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
The custom queries of ContextMapper, ContextNodeRelationMapper and
PageContentMapper bound node ids, node types, page ids and node relation
ids without a parameter type, so they went to the database as strings.

Assisted-by: ClaudeCode:claude-opus-5-5

Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
Now that node and owner types are typed, the entity casts whatever the
archive holds, so a node type "view" became a table node. The import
takes only the integer table and view node types and the user owner
type, and skips anything else with a warning.

Assisted-by: ClaudeCode:claude-opus-5-5

Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from c0627d7 to b57a52f Compare November 21, 2026 10:53
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/entity-addtype-names branch from 2b6795c to 39e110a Compare November 21, 2026 10:53
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.

2 participants