Skip to content

Commit 47d98c8

Browse files
authored
Merge pull request #174 from TomHAnderson/hotfix/json-type
Fixed parseLiteral for Json type
2 parents ddaa0b9 + 3f255c2 commit 47d98c8

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/Type/Json.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ class Json extends ScalarType
2323
public string|null $description = 'The `json` scalar type represents json data.';
2424

2525
#[Override]
26-
public function parseLiteral(ASTNode $valueNode, array|null $variables = null): string
26+
public function parseLiteral(ASTNode $valueNode, array|null $variables = null): array|null
2727
{
2828
// @codeCoverageIgnoreStart
2929
if (! $valueNode instanceof StringValueNode) {
3030
throw new Error('Query error: Can only parse strings got: ' . $valueNode->kind, $valueNode);
3131
}
3232

33-
return $valueNode->value;
33+
return $this->parseValue($valueNode->value);
3434
// @codeCoverageIgnoreEnd
3535
}
3636

@@ -56,8 +56,14 @@ public function parseValue(mixed $value): array|null
5656
}
5757

5858
#[Override]
59-
public function serialize(mixed $value): false|string
59+
public function serialize(mixed $value): string|false
6060
{
61-
return json_encode($value);
61+
$return = json_encode($value);
62+
63+
if (! $return) {
64+
throw new Error('Could not serialize JSON data');
65+
}
66+
67+
return $return;
6268
}
6369
}

test/Feature/Type/JsonTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,18 @@ public function testParseLiteral(): void
4949
{
5050
$jsonType = new Json();
5151
$node = new StringValueNode([]);
52-
$node->value = 'search string';
52+
$node->value = '{"field": "value"}';
5353
$result = $jsonType->parseLiteral($node);
5454

55-
$this->assertTrue(true);
55+
$this->assertEquals(['field' => 'value'], $result);
56+
}
57+
58+
public function testSerializeFails(): void
59+
{
60+
$this->expectException(Error::class);
61+
62+
$jsonType = new Json();
63+
$jsonType->serialize(["name" => "\xB1\x31"]);
5664
}
5765

5866
public function testContains(): void

0 commit comments

Comments
 (0)