Skip to content

Commit

Permalink
A strict JSON column must always contains valid JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerKiKi committed Jul 11, 2024
1 parent 355fc21 commit 945706b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/DBAL/Types/LocalizedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
* PHP values are expected to be an array of localized values indexed by language. The array
* might be empty, but it can never be null or empty string.
*
* For convenience of DB operation the DB value might be null or an empty string, in which case
* the PHP value will be an empty array. This allows for easy INSERT/UPDATE, and save two bytes
* in case of empty array.
* DB values are constrained by the database, so they must always be valid JSON, such as the minimal data `{}`.
*/
final class LocalizedType extends JsonType
{
Expand Down Expand Up @@ -51,7 +49,7 @@ public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform)
}

if (!$value) {
return '';
return '{}';
}

try {
Expand Down
2 changes: 1 addition & 1 deletion tests/DBAL/Types/LocalizedTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testConvertToPHPValue(): void
public function testConvertToDatabaseValue(): void
{
self::assertSame('{"fr":"foo"}', $this->type->convertToDatabaseValue(['fr' => 'foo'], $this->platform));
self::assertSame('', $this->type->convertToDatabaseValue([], $this->platform), 'micro-optimization of an empty array into an empty string to save two bytes');
self::assertSame('{}', $this->type->convertToDatabaseValue([], $this->platform), 'empty should still be valid JSON');
}

public function testConvertToDatabaseValueWillThrowIfNull(): void
Expand Down

0 comments on commit 945706b

Please sign in to comment.