Skip to content

Commit 54c3aa3

Browse files
committed
fix(entity): Fix mapping of old/sub-types to actually supported database types
Signed-off-by: Joas Schilling <[email protected]>
1 parent 582af10 commit 54c3aa3

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

lib/public/AppFramework/Db/Entity.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ protected function setter(string $name, array $args): void {
112112
}
113113

114114
switch ($type) {
115+
case Types::BIGINT:
116+
case Types::SMALLINT:
117+
settype($args[0], Types::INTEGER);
118+
break;
119+
case Types::BINARY:
120+
case Types::DECIMAL:
121+
case Types::TEXT:
122+
settype($args[0], Types::STRING);
123+
break;
115124
case Types::TIME:
116125
case Types::DATE:
117126
case Types::DATETIME:
@@ -260,9 +269,22 @@ public function getUpdatedFields(): array {
260269
*
261270
* @param string $fieldName the name of the attribute
262271
* @param \OCP\DB\Types::* $type the type which will be used to match a cast
272+
* @since 31.0.0 Parameter $type is now restricted to {@see \OCP\DB\Types} constants. The formerly accidentally supported types 'int'|'bool'|'double' are mapped to Types::INTEGER|Types::BOOLEAN|Types::FLOAT accordingly.
263273
* @since 7.0.0
264274
*/
265275
protected function addType(string $fieldName, string $type): void {
276+
/** @psalm-suppress TypeDoesNotContainType */
277+
if (in_array($type, ['bool', 'double', 'int', 'array', 'object'], true)) {
278+
// Mapping legacy strings to the actual types
279+
$type = match ($type) {
280+
'int' => Types::INTEGER,
281+
'bool' => Types::BOOLEAN,
282+
'double' => Types::FLOAT,
283+
'array',
284+
'object' => Types::STRING,
285+
};
286+
}
287+
266288
$this->_fieldTypes[$fieldName] = $type;
267289
}
268290

tests/lib/AppFramework/Db/EntityTest.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,31 @@ class TestEntity extends Entity {
3939
protected $name;
4040
protected $email;
4141
protected $testId;
42+
protected $smallInt;
43+
protected $bigInt;
4244
protected $preName;
4345
protected $trueOrFalse;
4446
protected $anotherBool;
47+
protected $text;
4548
protected $longText;
4649
protected $time;
4750
protected $datetime;
4851

4952
public function __construct($name = null) {
5053
$this->addType('testId', Types::INTEGER);
51-
$this->addType('trueOrFalse', 'bool');
54+
$this->addType('smallInt', Types::SMALLINT);
55+
$this->addType('bigInt', Types::BIGINT);
5256
$this->addType('anotherBool', Types::BOOLEAN);
57+
$this->addType('text', Types::TEXT);
5358
$this->addType('longText', Types::BLOB);
5459
$this->addType('time', Types::TIME);
5560
$this->addType('datetime', Types::DATETIME_IMMUTABLE);
61+
62+
// Legacy types
63+
$this->addType('trueOrFalse', 'bool');
64+
$this->addType('legacyInt', 'int');
65+
$this->addType('doubleNowFloat', 'double');
66+
5667
$this->name = $name;
5768
}
5869

@@ -200,10 +211,28 @@ public function testSlugify(): void {
200211
}
201212

202213

203-
public function testSetterCasts(): void {
214+
public function dataSetterCasts(): array {
215+
return [
216+
['Id', '3', 3],
217+
['smallInt', '3', 3],
218+
['bigInt', '' . PHP_INT_MAX, PHP_INT_MAX],
219+
['trueOrFalse', 0, false],
220+
['trueOrFalse', 1, true],
221+
['anotherBool', 0, false],
222+
['anotherBool', 1, true],
223+
['text', 33, '33'],
224+
['longText', PHP_INT_MAX, '' . PHP_INT_MAX],
225+
];
226+
}
227+
228+
229+
/**
230+
* @dataProvider dataSetterCasts
231+
*/
232+
public function testSetterCasts(string $field, mixed $in, mixed $out): void {
204233
$entity = new TestEntity();
205-
$entity->setId('3');
206-
$this->assertSame(3, $entity->getId());
234+
$entity->{'set' . $field}($in);
235+
$this->assertSame($out, $entity->{'get' . $field}());
207236
}
208237

209238

@@ -248,11 +277,16 @@ public function testGetFieldTypes(): void {
248277
$this->assertEquals([
249278
'id' => Types::INTEGER,
250279
'testId' => Types::INTEGER,
251-
'trueOrFalse' => 'bool',
280+
'smallInt' => Types::SMALLINT,
281+
'bigInt' => Types::BIGINT,
252282
'anotherBool' => Types::BOOLEAN,
283+
'text' => Types::TEXT,
253284
'longText' => Types::BLOB,
254285
'time' => Types::TIME,
255286
'datetime' => Types::DATETIME_IMMUTABLE,
287+
'trueOrFalse' => Types::BOOLEAN,
288+
'legacyInt' => Types::INTEGER,
289+
'doubleNowFloat' => Types::FLOAT,
256290
], $entity->getFieldTypes());
257291
}
258292

0 commit comments

Comments
 (0)