mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-22 12:56:05 +03:00
Scalar serialization now throws on spec-incompatible values vs returning null
This commit is contained in:
parent
0a182ac53c
commit
7625d6abf1
@ -799,7 +799,14 @@ class Executor
|
|||||||
private static function completeLeafValue(Type $returnType, &$result)
|
private static function completeLeafValue(Type $returnType, &$result)
|
||||||
{
|
{
|
||||||
Utils::invariant(method_exists($returnType, 'serialize'), 'Missing serialize method on type');
|
Utils::invariant(method_exists($returnType, 'serialize'), 'Missing serialize method on type');
|
||||||
return $returnType->serialize($result);
|
$serializedResult = $returnType->serialize($result);
|
||||||
|
|
||||||
|
if ($serializedResult === null) {
|
||||||
|
throw new \UnexpectedValueException(
|
||||||
|
'Expected a value of type "'. $returnType . '" but received: ' . Utils::printSafe($result)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return $serializedResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,6 +3,7 @@ namespace GraphQL\Type\Definition;
|
|||||||
|
|
||||||
use GraphQL\Language\AST\FloatValue;
|
use GraphQL\Language\AST\FloatValue;
|
||||||
use GraphQL\Language\AST\IntValue;
|
use GraphQL\Language\AST\IntValue;
|
||||||
|
use GraphQL\Utils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class FloatType
|
* Class FloatType
|
||||||
@ -47,7 +48,15 @@ values as specified by
|
|||||||
*/
|
*/
|
||||||
private function coerceFloat($value)
|
private function coerceFloat($value)
|
||||||
{
|
{
|
||||||
return is_numeric($value) || $value === true || $value === false ? (float) $value : null;
|
if ($value === '') {
|
||||||
|
throw new \UnexpectedValueException(
|
||||||
|
'Float cannot represent non numeric value: (empty string)'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (is_numeric($value) || $value === true || $value === false) {
|
||||||
|
return (float)$value;
|
||||||
|
}
|
||||||
|
throw new \UnexpectedValueException('Float cannot represent non numeric value: ' . Utils::printSafe($value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,6 +3,7 @@ namespace GraphQL\Type\Definition;
|
|||||||
|
|
||||||
use GraphQL\Language\AST\IntValue;
|
use GraphQL\Language\AST\IntValue;
|
||||||
use GraphQL\Language\AST\Value;
|
use GraphQL\Language\AST\Value;
|
||||||
|
use GraphQL\Utils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class IntType
|
* Class IntType
|
||||||
@ -54,13 +55,20 @@ values. Int can represent values between -(2^31) and 2^31 - 1. ';
|
|||||||
*/
|
*/
|
||||||
private function coerceInt($value)
|
private function coerceInt($value)
|
||||||
{
|
{
|
||||||
|
if ($value === '') {
|
||||||
|
throw new \UnexpectedValueException(
|
||||||
|
'Int cannot represent non 32-bit signed integer value: (empty string)'
|
||||||
|
);
|
||||||
|
}
|
||||||
if (false === $value || true === $value) {
|
if (false === $value || true === $value) {
|
||||||
return (int) $value;
|
return (int) $value;
|
||||||
}
|
}
|
||||||
if (is_numeric($value) && $value <= self::MAX_INT && $value >= self::MIN_INT) {
|
if (is_numeric($value) && $value <= self::MAX_INT && $value >= self::MIN_INT) {
|
||||||
return (int) $value;
|
return (int) $value;
|
||||||
}
|
}
|
||||||
return null;
|
throw new \UnexpectedValueException(
|
||||||
|
'Int cannot represent non 32-bit signed integer value: ' . Utils::printSafe($value)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,6 +15,7 @@ class ScalarSerializationTest extends \PHPUnit_Framework_TestCase
|
|||||||
$intType = Type::int();
|
$intType = Type::int();
|
||||||
|
|
||||||
$this->assertSame(1, $intType->serialize(1));
|
$this->assertSame(1, $intType->serialize(1));
|
||||||
|
$this->assertSame(123, $intType->serialize('123'));
|
||||||
$this->assertSame(0, $intType->serialize(0));
|
$this->assertSame(0, $intType->serialize(0));
|
||||||
$this->assertSame(-1, $intType->serialize(-1));
|
$this->assertSame(-1, $intType->serialize(-1));
|
||||||
$this->assertSame(0, $intType->serialize(0.1));
|
$this->assertSame(0, $intType->serialize(0.1));
|
||||||
@ -23,12 +24,50 @@ class ScalarSerializationTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertSame(100000, $intType->serialize(1e5));
|
$this->assertSame(100000, $intType->serialize(1e5));
|
||||||
// Maybe a safe PHP int, but bigger than 2^32, so not
|
// Maybe a safe PHP int, but bigger than 2^32, so not
|
||||||
// representable as a GraphQL Int
|
// representable as a GraphQL Int
|
||||||
$this->assertSame(null, $intType->serialize(9876504321));
|
try {
|
||||||
$this->assertSame(null, $intType->serialize(-9876504321));
|
$intType->serialize(9876504321);
|
||||||
$this->assertSame(null, $intType->serialize(1e100));
|
$this->fail('Expected exception was not thrown');
|
||||||
$this->assertSame(null, $intType->serialize(-1e100));
|
} catch (\UnexpectedValueException $e) {
|
||||||
|
$this->assertEquals('Int cannot represent non 32-bit signed integer value: 9876504321', $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$intType->serialize(-9876504321);
|
||||||
|
$this->fail('Expected exception was not thrown');
|
||||||
|
} catch (\UnexpectedValueException $e) {
|
||||||
|
$this->assertEquals('Int cannot represent non 32-bit signed integer value: -9876504321', $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$intType->serialize(1e100);
|
||||||
|
$this->fail('Expected exception was not thrown');
|
||||||
|
} catch (\UnexpectedValueException $e) {
|
||||||
|
$this->assertEquals('Int cannot represent non 32-bit signed integer value: 1.0E+100', $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$intType->serialize(-1e100);
|
||||||
|
$this->fail('Expected exception was not thrown');
|
||||||
|
} catch (\UnexpectedValueException $e) {
|
||||||
|
$this->assertEquals('Int cannot represent non 32-bit signed integer value: -1.0E+100', $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
$this->assertSame(-1, $intType->serialize('-1.1'));
|
$this->assertSame(-1, $intType->serialize('-1.1'));
|
||||||
$this->assertSame(null, $intType->serialize('one'));
|
|
||||||
|
try {
|
||||||
|
$intType->serialize('one');
|
||||||
|
$this->fail('Expected exception was not thrown');
|
||||||
|
} catch (\UnexpectedValueException $e) {
|
||||||
|
$this->assertEquals('Int cannot represent non 32-bit signed integer value: one', $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$intType->serialize('');
|
||||||
|
$this->fail('Expected exception was not thrown');
|
||||||
|
} catch (\UnexpectedValueException $e) {
|
||||||
|
$this->assertEquals('Int cannot represent non 32-bit signed integer value: (empty string)', $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
$this->assertSame(0, $intType->serialize(false));
|
$this->assertSame(0, $intType->serialize(false));
|
||||||
$this->assertSame(1, $intType->serialize(true));
|
$this->assertSame(1, $intType->serialize(true));
|
||||||
}
|
}
|
||||||
@ -42,12 +81,27 @@ class ScalarSerializationTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertSame(1.0, $floatType->serialize(1));
|
$this->assertSame(1.0, $floatType->serialize(1));
|
||||||
$this->assertSame(0.0, $floatType->serialize(0));
|
$this->assertSame(0.0, $floatType->serialize(0));
|
||||||
|
$this->assertSame(123.5, $floatType->serialize('123.5'));
|
||||||
$this->assertSame(-1.0, $floatType->serialize(-1));
|
$this->assertSame(-1.0, $floatType->serialize(-1));
|
||||||
$this->assertSame(0.1, $floatType->serialize(0.1));
|
$this->assertSame(0.1, $floatType->serialize(0.1));
|
||||||
$this->assertSame(1.1, $floatType->serialize(1.1));
|
$this->assertSame(1.1, $floatType->serialize(1.1));
|
||||||
$this->assertSame(-1.1, $floatType->serialize(-1.1));
|
$this->assertSame(-1.1, $floatType->serialize(-1.1));
|
||||||
$this->assertSame(-1.1, $floatType->serialize('-1.1'));
|
$this->assertSame(-1.1, $floatType->serialize('-1.1'));
|
||||||
$this->assertSame(null, $floatType->serialize('one'));
|
|
||||||
|
try {
|
||||||
|
$floatType->serialize('one');
|
||||||
|
$this->fail('Expected exception was not thrown');
|
||||||
|
} catch (\UnexpectedValueException $e) {
|
||||||
|
$this->assertEquals('Float cannot represent non numeric value: one', $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$floatType->serialize('');
|
||||||
|
$this->fail('Expected exception was not thrown');
|
||||||
|
} catch (\UnexpectedValueException $e) {
|
||||||
|
$this->assertEquals('Float cannot represent non numeric value: (empty string)', $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
$this->assertSame(0.0, $floatType->serialize(false));
|
$this->assertSame(0.0, $floatType->serialize(false));
|
||||||
$this->assertSame(1.0, $floatType->serialize(true));
|
$this->assertSame(1.0, $floatType->serialize(true));
|
||||||
}
|
}
|
||||||
@ -81,6 +135,6 @@ class ScalarSerializationTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertSame(true, $boolType->serialize(true));
|
$this->assertSame(true, $boolType->serialize(true));
|
||||||
$this->assertSame(false, $boolType->serialize(false));
|
$this->assertSame(false, $boolType->serialize(false));
|
||||||
|
|
||||||
// TODO: how should it behaive on '0'?
|
// TODO: how should it behave on '0'?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user