Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JSON Dbal type to properly encode whole number float values, preserving zero fractions. #5173

Merged
merged 3 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Types/JsonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use function json_encode;
use function stream_get_contents;

use const JSON_PRESERVE_ZERO_FRACTION;
use const JSON_THROW_ON_ERROR;

/**
Expand All @@ -35,7 +36,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
}

try {
return json_encode($value, JSON_THROW_ON_ERROR);
return json_encode($value, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION);
} catch (JsonException $e) {
throw ConversionException::conversionFailedSerialization($value, 'json', $e->getMessage(), $e);
}
Expand Down
29 changes: 27 additions & 2 deletions tests/Types/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function fopen;
use function json_encode;

use const JSON_PRESERVE_ZERO_FRACTION;
use const JSON_THROW_ON_ERROR;

class JsonTest extends TestCase
Expand Down Expand Up @@ -62,7 +63,7 @@ public function testJsonEmptyStringConvertsToPHPValue(): void
public function testJsonStringConvertsToPHPValue(): void
{
$value = ['foo' => 'bar', 'bar' => 'foo'];
$databaseValue = json_encode($value, 0, JSON_THROW_ON_ERROR);
Kova101 marked this conversation as resolved.
Show resolved Hide resolved
$databaseValue = json_encode($value, 0, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION);
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform);

self::assertEquals($value, $phpValue);
Expand All @@ -87,7 +88,10 @@ public function testJsonResourceConvertsToPHPValue(): void
{
$value = ['foo' => 'bar', 'bar' => 'foo'];
$databaseValue = fopen(
'data://text/plain;base64,' . base64_encode(json_encode($value, JSON_THROW_ON_ERROR)),
'data://text/plain;base64,' . base64_encode(json_encode(
$value,
JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION
)),
'r'
);
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform);
Expand All @@ -100,6 +104,27 @@ public function testRequiresSQLCommentHint(): void
self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
}

public function testPHPNullValueConvertsToJsonNull(): void
{
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform));
}

public function testPHPValueConvertsToJsonString(): void
{
$source = ['foo' => 'bar', 'bar' => 'foo'];
$databaseValue = $this->type->convertToDatabaseValue($source, $this->platform);

self::assertSame('{"foo":"bar","bar":"foo"}', $databaseValue);
}

public function testPHPFloatValueConvertsToJsonString(): void
{
$source = ['foo' => 11.4, 'bar' => 10.0];
$databaseValue = $this->type->convertToDatabaseValue($source, $this->platform);

self::assertSame('{"foo":11.4,"bar":10.0}', $databaseValue);
}

public function testSerializationFailure(): void
{
$object = (object) [];
Expand Down