-
-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix generating property schema with Range restriction
- Loading branch information
Showing
6 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRangeRestriction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction; | ||
|
||
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; | ||
use Symfony\Component\PropertyInfo\Type; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Constraints\Range; | ||
|
||
/** | ||
* @author Tomas Norkūnas <[email protected]> | ||
*/ | ||
final class PropertySchemaRangeRestriction implements PropertySchemaRestrictionMetadataInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function create(Constraint $constraint, PropertyMetadata $propertyMetadata): array | ||
{ | ||
$restriction = []; | ||
|
||
switch ($propertyMetadata->getType()->getBuiltinType()) { | ||
case Type::BUILTIN_TYPE_INT: | ||
case Type::BUILTIN_TYPE_FLOAT: | ||
if (isset($constraint->min) && is_numeric($constraint->min)) { | ||
$restriction['minimum'] = $constraint->min; | ||
} | ||
|
||
if (isset($constraint->max) && is_numeric($constraint->max)) { | ||
$restriction['maximum'] = $constraint->max; | ||
} | ||
|
||
break; | ||
} | ||
|
||
return $restriction; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(Constraint $constraint, PropertyMetadata $propertyMetadata): bool | ||
{ | ||
return $constraint instanceof Range && null !== $propertyMetadata->getType(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...ge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRangeRestrictionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Bridge\Symfony\Validator\Metadata\Property\Restriction; | ||
|
||
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRangeRestriction; | ||
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; | ||
use ApiPlatform\Core\Tests\ProphecyTrait; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\PropertyInfo\Type; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Constraints\Length; | ||
use Symfony\Component\Validator\Constraints\Range; | ||
|
||
/** | ||
* @author Tomas Norkūnas <[email protected]> | ||
*/ | ||
final class PropertySchemaRangeRestrictionTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
private $propertySchemaRangeRestriction; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->propertySchemaRangeRestriction = new PropertySchemaRangeRestriction(); | ||
} | ||
|
||
/** | ||
* @dataProvider supportsProvider | ||
*/ | ||
public function testSupports(Constraint $constraint, PropertyMetadata $propertyMetadata, bool $expectedResult): void | ||
{ | ||
self::assertSame($expectedResult, $this->propertySchemaRangeRestriction->supports($constraint, $propertyMetadata)); | ||
} | ||
|
||
public function supportsProvider(): \Generator | ||
{ | ||
yield 'supported int' => [new Range(['min' => 1, 'max' => 10]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT)), true]; | ||
yield 'supported float' => [new Range(['min' => 1, 'max' => 10]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_FLOAT)), true]; | ||
|
||
yield 'not supported constraint' => [new Length(['min' => 1]), new PropertyMetadata(), false]; | ||
yield 'not supported type' => [new Range(['min' => 1]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING)), true]; | ||
} | ||
|
||
/** | ||
* @dataProvider createProvider | ||
*/ | ||
public function testCreate(Constraint $constraint, PropertyMetadata $propertyMetadata, array $expectedResult): void | ||
{ | ||
self::assertSame($expectedResult, $this->propertySchemaRangeRestriction->create($constraint, $propertyMetadata)); | ||
} | ||
|
||
public function createProvider(): \Generator | ||
{ | ||
yield 'int min' => [new Range(['min' => 1]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT)), ['minimum' => 1]]; | ||
yield 'int max' => [new Range(['max' => 10]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT)), ['maximum' => 10]]; | ||
|
||
yield 'float min' => [new Range(['min' => 1.5]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_FLOAT)), ['minimum' => 1.5]]; | ||
yield 'float max' => [new Range(['max' => 10.5]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_FLOAT)), ['maximum' => 10.5]]; | ||
|
||
yield 'unsupported type' => [new Range(['min' => 1]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING)), []]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Fixtures; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class DummyRangeValidatedEntity | ||
{ | ||
/** | ||
* @var int | ||
* | ||
* @Assert\Range(min=1) | ||
*/ | ||
public $dummyIntMin; | ||
|
||
/** | ||
* @var int | ||
* | ||
* @Assert\Range(max=10) | ||
*/ | ||
public $dummyIntMax; | ||
|
||
/** | ||
* @var int | ||
* | ||
* @Assert\Range(min=1, max=10) | ||
*/ | ||
public $dummyIntMinMax; | ||
|
||
/** | ||
* @var float | ||
* | ||
* @Assert\Range(min=1.5) | ||
*/ | ||
public $dummyFloatMin; | ||
|
||
/** | ||
* @var float | ||
* | ||
* @Assert\Range(max=10.5) | ||
*/ | ||
public $dummyFloatMax; | ||
|
||
/** | ||
* @var float | ||
* | ||
* @Assert\Range(min=1.5, max=10.5) | ||
*/ | ||
public $dummyFloatMinMax; | ||
} |