Skip to content

Commit 945706b

Browse files
committedJul 11, 2024
A strict JSON column must always contains valid JSON
1 parent 355fc21 commit 945706b

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed
 

‎src/DBAL/Types/LocalizedType.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
* PHP values are expected to be an array of localized values indexed by language. The array
1616
* might be empty, but it can never be null or empty string.
1717
*
18-
* For convenience of DB operation the DB value might be null or an empty string, in which case
19-
* the PHP value will be an empty array. This allows for easy INSERT/UPDATE, and save two bytes
20-
* in case of empty array.
18+
* DB values are constrained by the database, so they must always be valid JSON, such as the minimal data `{}`.
2119
*/
2220
final class LocalizedType extends JsonType
2321
{
@@ -51,7 +49,7 @@ public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform)
5149
}
5250

5351
if (!$value) {
54-
return '';
52+
return '{}';
5553
}
5654

5755
try {

‎tests/DBAL/Types/LocalizedTypeTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testConvertToPHPValue(): void
3131
public function testConvertToDatabaseValue(): void
3232
{
3333
self::assertSame('{"fr":"foo"}', $this->type->convertToDatabaseValue(['fr' => 'foo'], $this->platform));
34-
self::assertSame('', $this->type->convertToDatabaseValue([], $this->platform), 'micro-optimization of an empty array into an empty string to save two bytes');
34+
self::assertSame('{}', $this->type->convertToDatabaseValue([], $this->platform), 'empty should still be valid JSON');
3535
}
3636

3737
public function testConvertToDatabaseValueWillThrowIfNull(): void

0 commit comments

Comments
 (0)