Repository navigation
fix(db): type the integer columns of applications, node relations and page contents - #3203
rubenvdlinde wants to merge 5 commits into
Conversation
a1b94a7 to
2471ff4
Compare
877a03b to
05b7968
Compare
2471ff4 to
6368c26
Compare
05b7968 to
2f29f7a
Compare
6368c26 to
c203b3a
Compare
2f29f7a to
d3b372b
Compare
c203b3a to
5cf92cf
Compare
d3b372b to
c3582d0
Compare
AndyScherzinger
left a comment
There was a problem hiding this comment.
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.phpline 36) has noaddType, soPageMapperstill bindstables_contexts_page.context_idasPARAM_STR. The same holds forView::$externalId,RowCellUsergroup::$valueTypeandColumn::$orderWeight. A reflection scan of every entity inlib/Dbon 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 withoutIQueryBuilder::PARAM_INT:PageContentMapper.phplines 34, 47-48 and 61,ContextNodeRelationMapper.phplines 38-39 and 64, andContextMapper.phplines 270-271. Nothing fails today, since every supported database casts the parameter, but they sit next toPARAM_INTfor 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'); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
| */ | ||
| 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)), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Thanks, that argument was dead and would have broken the backport. It is gone, together with the whole test file.
cc @blizzz
| } | ||
| } | ||
|
|
||
| public function testANodeRelationAndAPageContentAreReadBackAsIntegers(): void { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Agreed, it guarded nothing and could leave rows behind. I removed it.
cc @blizzz
| * @param class-string<Entity> $class | ||
| * @param list<string> $properties | ||
| */ | ||
| public function testTheMapperBindsTheIntegerColumnsAsIntegers(string $class, array $properties): void { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
| /** | ||
| * @return array<string, array{class-string<Entity>, list<string>}> | ||
| */ | ||
| public static function integerProperties(): array { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
5cf92cf to
ee0564c
Compare
c3582d0 to
835dc1f
Compare
ee0564c to
00c1b92
Compare
835dc1f to
53b72ca
Compare
00c1b92 to
6ffaf94
Compare
53b72ca to
a8dedc7
Compare
|
Hello there, 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.) |
6ffaf94 to
f7549c7
Compare
a8dedc7 to
de1cdd9
Compare
f7549c7 to
b464a42
Compare
de1cdd9 to
1898aac
Compare
b464a42 to
c0627d7
Compare
1898aac to
2b6795c
Compare
|
Thanks for the scan. I typed the four other integer properties you found ( 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>
c0627d7 to
b57a52f
Compare
2b6795c to
39e110a
Compare
Sits on #3138 (menu export and import), though the bug is older and on
maintoo.Context,ContextNodeRelationandPageContentnow 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()andQBMapper::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 forownerType,contextId,nodeId,nodeType,pageIdandnodeRelIdValues read back from the database were already integers, because the properties are typed
?intand PHP converts a numeric string on assignment. A non-numeric value would still throw aTypeErrorthere instead of being cast.What changes
The six
addType()calls use the property names.Contexthad the same pattern forowner_type, so it's included.The scope grew from there:
addType()IQueryBuilder::PARAM_INTHow it was tested
On Nextcloud 34 with PostgreSQL, in
EntityVirtualPropertiesTest, for every entity:Without the change this fails for seven entities: Column, Context, ContextNodeRelation, Page, PageContent, RowCellUsergroup and View. Two tests in
ContextServiceTestcover 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
/backport to stableX.X🤖 AI (if applicable)