From 074fd4fc9dcfea272f063615bb1280c4d1a8d928 Mon Sep 17 00:00:00 2001 From: Alexander Strizhak Date: Sun, 26 Apr 2026 14:53:13 +0300 Subject: [PATCH 1/2] json is not always cast as an array --- src/Parser/Typecast.php | 3 ++- tests/ORM/Unit/Parser/TypecastTest.php | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Parser/Typecast.php b/src/Parser/Typecast.php index c0d5b965a..82cd1132f 100644 --- a/src/Parser/Typecast.php +++ b/src/Parser/Typecast.php @@ -71,7 +71,8 @@ public function setRules(array $rules): array $value, $this->database->getDriver()->getTimezone(), ), - 'json' => static fn(mixed $value): array => \json_decode( + // Mostly an array, but can also be a scalar type. + 'json' => static fn(mixed $value): mixed => \json_decode( $value, true, 512, diff --git a/tests/ORM/Unit/Parser/TypecastTest.php b/tests/ORM/Unit/Parser/TypecastTest.php index 952be1cca..fdacab0b5 100644 --- a/tests/ORM/Unit/Parser/TypecastTest.php +++ b/tests/ORM/Unit/Parser/TypecastTest.php @@ -184,7 +184,7 @@ public function testCastCallableWithArguments(array $callable, array $args, bool ); } - public function testCastJsonValue(): void + public function testCastJsonArrayValue(): void { $this->typecast->setRules(['foo' => 'json', 'baz' => 'json']); @@ -199,6 +199,21 @@ public function testCastJsonValue(): void $this->assertNull($data['baz']); } + public function testCastJsonScalarValue(): void + { + $this->typecast->setRules(['foo' => 'json', 'bar' => 'json', 'baz' => 'json']); + + $data = $this->typecast->cast([ + 'foo' => \json_encode(1917, \JSON_THROW_ON_ERROR), + 'bar' => \json_encode('string', \JSON_THROW_ON_ERROR), + 'baz' => \json_encode(3.14, \JSON_THROW_ON_ERROR), + ]); + + $this->assertSame(1917, $data['foo']); + $this->assertSame('string', $data['bar']); + $this->assertSame(3.14, $data['baz']); + } + public function testUncast(): void { $this->typecast->setRules(['foo' => 'json', 'baz' => 'json']); From bc917f62b6c6385c33381699044affdac311cbf1 Mon Sep 17 00:00:00 2001 From: Alexander Strizhak Date: Sun, 26 Apr 2026 15:03:20 +0300 Subject: [PATCH 2/2] up --- tests/ORM/Unit/Parser/TypecastTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/ORM/Unit/Parser/TypecastTest.php b/tests/ORM/Unit/Parser/TypecastTest.php index fdacab0b5..5a4582a60 100644 --- a/tests/ORM/Unit/Parser/TypecastTest.php +++ b/tests/ORM/Unit/Parser/TypecastTest.php @@ -212,6 +212,11 @@ public function testCastJsonScalarValue(): void $this->assertSame(1917, $data['foo']); $this->assertSame('string', $data['bar']); $this->assertSame(3.14, $data['baz']); + + $val = $this->typecast->uncast($data); + self::assertSame("1917", $val['foo']); + self::assertSame('"string"', $val['bar']); + self::assertSame("3.14", $val['baz']); } public function testUncast(): void