From 57fb10043df46bec9cc5cf503b2dafc01ff9aa5b Mon Sep 17 00:00:00 2001 From: Tom H Anderson Date: Sat, 1 Feb 2025 00:45:26 -0700 Subject: [PATCH 1/2] Fixed parseLiteral for Json type --- src/Type/Json.php | 12 +++++++++--- test/Feature/Type/JsonTest.php | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Type/Json.php b/src/Type/Json.php index 44b6318..2ed7126 100644 --- a/src/Type/Json.php +++ b/src/Type/Json.php @@ -21,14 +21,14 @@ class Json extends ScalarType // phpcs:disable SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint public string|null $description = 'The `json` scalar type represents json data.'; - public function parseLiteral(ASTNode $valueNode, array|null $variables = null): string + public function parseLiteral(ASTNode $valueNode, array|null $variables = null): array|null { // @codeCoverageIgnoreStart if (! $valueNode instanceof StringValueNode) { throw new Error('Query error: Can only parse strings got: ' . $valueNode->kind, $valueNode); } - return $valueNode->value; + return $this->parseValue($valueNode->value); // @codeCoverageIgnoreEnd } @@ -54,6 +54,12 @@ public function parseValue(mixed $value): array|null public function serialize(mixed $value): string|null { - return json_encode($value); + $return = json_encode($value); + + if (! $return) { + return null; + } + + return $return; } } diff --git a/test/Feature/Type/JsonTest.php b/test/Feature/Type/JsonTest.php index 5fe1b22..9fa9327 100644 --- a/test/Feature/Type/JsonTest.php +++ b/test/Feature/Type/JsonTest.php @@ -49,10 +49,10 @@ public function testParseLiteral(): void { $jsonType = new Json(); $node = new StringValueNode([]); - $node->value = 'search string'; + $node->value = '{"field": "value"}'; $result = $jsonType->parseLiteral($node); - $this->assertTrue(true); + $this->assertEquals(['field' => 'value'], $result); } public function testContains(): void From 3f255c28b6c97ac9f685f1d35cd18eb74087b344 Mon Sep 17 00:00:00 2001 From: Tom H Anderson Date: Sat, 20 Dec 2025 16:35:01 -0700 Subject: [PATCH 2/2] When data is not serializable, throw an Error --- src/Type/Json.php | 4 ++-- test/Feature/Type/JsonTest.php | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Type/Json.php b/src/Type/Json.php index 548d7be..e398f76 100644 --- a/src/Type/Json.php +++ b/src/Type/Json.php @@ -56,12 +56,12 @@ public function parseValue(mixed $value): array|null } #[Override] - public function serialize(mixed $value): false|string + public function serialize(mixed $value): string|false { $return = json_encode($value); if (! $return) { - return null; + throw new Error('Could not serialize JSON data'); } return $return; diff --git a/test/Feature/Type/JsonTest.php b/test/Feature/Type/JsonTest.php index 8ea0bca..8e704ab 100644 --- a/test/Feature/Type/JsonTest.php +++ b/test/Feature/Type/JsonTest.php @@ -55,6 +55,14 @@ public function testParseLiteral(): void $this->assertEquals(['field' => 'value'], $result); } + public function testSerializeFails(): void + { + $this->expectException(Error::class); + + $jsonType = new Json(); + $jsonType->serialize(["name" => "\xB1\x31"]); + } + public function testContains(): void { $driver = new Driver($this->getEntityManager(), new Config(['group' => 'DataTypesTest']));