-
-
Notifications
You must be signed in to change notification settings - Fork 933
Add support for generating property schema with Choice restriction #4162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alanpoulain
merged 1 commit into
api-platform:main
from
norkunas:property-schema-choice-restriction
Mar 26, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
...ridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaChoiceRestriction.php
This file contains hidden or 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 <dunglas@gmail.com> | ||
* | ||
* 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\Choice; | ||
|
||
/** | ||
* @author Tomas Norkūnas <norkunas.tom@gmail.com> | ||
*/ | ||
final class PropertySchemaChoiceRestriction implements PropertySchemaRestrictionMetadataInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* @param Choice $constraint | ||
*/ | ||
public function create(Constraint $constraint, PropertyMetadata $propertyMetadata): array | ||
{ | ||
$choices = []; | ||
|
||
if (\is_callable($choices = $constraint->callback)) { | ||
$choices = $choices(); | ||
} elseif (\is_array($constraint->choices)) { | ||
$choices = $constraint->choices; | ||
} | ||
|
||
if (!$choices) { | ||
return []; | ||
} | ||
|
||
$restriction = []; | ||
|
||
if (!$constraint->multiple) { | ||
$restriction['enum'] = $choices; | ||
|
||
return $restriction; | ||
} | ||
|
||
$restriction['type'] = 'array'; | ||
$restriction['items'] = ['type' => Type::BUILTIN_TYPE_STRING === $propertyMetadata->getType()->getBuiltinType() ? 'string' : 'number', 'enum' => $choices]; | ||
|
||
if (null !== $constraint->min) { | ||
$restriction['minItems'] = $constraint->min; | ||
} | ||
|
||
if (null !== $constraint->max) { | ||
$restriction['maxItems'] = $constraint->max; | ||
} | ||
|
||
return $restriction; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(Constraint $constraint, PropertyMetadata $propertyMetadata): bool | ||
{ | ||
return $constraint instanceof Choice && null !== ($type = $propertyMetadata->getType()) && \in_array($type->getBuiltinType(), [Type::BUILTIN_TYPE_STRING, Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_FLOAT], true); | ||
} | ||
} |
This file contains hidden or 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
94 changes: 94 additions & 0 deletions
94
...e/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaChoiceRestrictionTest.php
This file contains hidden or 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,94 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* 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\PropertySchemaChoiceRestriction; | ||
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\Choice; | ||
use Symfony\Component\Validator\Constraints\Positive; | ||
|
||
/** | ||
* @author Tomas Norkūnas <norkunas.tom@gmail.com> | ||
*/ | ||
final class PropertySchemaChoiceRestrictionTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
private $propertySchemaChoiceRestriction; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->propertySchemaChoiceRestriction = new PropertySchemaChoiceRestriction(); | ||
} | ||
|
||
/** | ||
* @dataProvider supportsProvider | ||
*/ | ||
public function testSupports(Constraint $constraint, PropertyMetadata $propertyMetadata, bool $expectedResult): void | ||
{ | ||
self::assertSame($expectedResult, $this->propertySchemaChoiceRestriction->supports($constraint, $propertyMetadata)); | ||
} | ||
|
||
public function supportsProvider(): \Generator | ||
{ | ||
yield 'supported' => [new Choice(['choices' => ['a', 'b']]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING)), true]; | ||
|
||
yield 'not supported constraint' => [new Positive(), new PropertyMetadata(), false]; | ||
yield 'not supported type' => [new Choice(['choices' => [new \stdClass(), new \stdClass()]]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_OBJECT)), false]; | ||
} | ||
|
||
/** | ||
* @dataProvider createProvider | ||
*/ | ||
public function testCreate(Constraint $constraint, PropertyMetadata $propertyMetadata, array $expectedResult): void | ||
{ | ||
self::assertSame($expectedResult, $this->propertySchemaChoiceRestriction->create($constraint, $propertyMetadata)); | ||
} | ||
|
||
public function createProvider(): \Generator | ||
{ | ||
yield 'single string choice' => [new Choice(['choices' => ['a', 'b']]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING)), ['enum' => ['a', 'b']]]; | ||
yield 'multi string choice' => [new Choice(['choices' => ['a', 'b'], 'multiple' => true]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING)), ['type' => 'array', 'items' => ['type' => 'string', 'enum' => ['a', 'b']]]]; | ||
yield 'multi string choice min' => [new Choice(['choices' => ['a', 'b'], 'multiple' => true, 'min' => 2]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING)), ['type' => 'array', 'items' => ['type' => 'string', 'enum' => ['a', 'b']], 'minItems' => 2]]; | ||
yield 'multi string choice max' => [new Choice(['choices' => ['a', 'b', 'c', 'd'], 'multiple' => true, 'max' => 4]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING)), ['type' => 'array', 'items' => ['type' => 'string', 'enum' => ['a', 'b', 'c', 'd']], 'maxItems' => 4]]; | ||
yield 'multi string choice min/max' => [new Choice(['choices' => ['a', 'b', 'c', 'd'], 'multiple' => true, 'min' => 2, 'max' => 4]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING)), ['type' => 'array', 'items' => ['type' => 'string', 'enum' => ['a', 'b', 'c', 'd']], 'minItems' => 2, 'maxItems' => 4]]; | ||
|
||
yield 'single int choice' => [new Choice(['choices' => [1, 2]]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT)), ['enum' => [1, 2]]]; | ||
yield 'multi int choice' => [new Choice(['choices' => [1, 2], 'multiple' => true]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT)), ['type' => 'array', 'items' => ['type' => 'number', 'enum' => [1, 2]]]]; | ||
yield 'multi int choice min' => [new Choice(['choices' => [1, 2], 'multiple' => true, 'min' => 2]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT)), ['type' => 'array', 'items' => ['type' => 'number', 'enum' => [1, 2]], 'minItems' => 2]]; | ||
yield 'multi int choice max' => [new Choice(['choices' => [1, 2, 3, 4], 'multiple' => true, 'max' => 4]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT)), ['type' => 'array', 'items' => ['type' => 'number', 'enum' => [1, 2, 3, 4]], 'maxItems' => 4]]; | ||
yield 'multi int choice min/max' => [new Choice(['choices' => [1, 2, 3, 4], 'multiple' => true, 'min' => 2, 'max' => 4]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT)), ['type' => 'array', 'items' => ['type' => 'number', 'enum' => [1, 2, 3, 4]], 'minItems' => 2, 'maxItems' => 4]]; | ||
|
||
yield 'single float choice' => [new Choice(['choices' => [1.1, 2.2]]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_FLOAT)), ['enum' => [1.1, 2.2]]]; | ||
yield 'multi float choice' => [new Choice(['choices' => [1.1, 2.2], 'multiple' => true]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_FLOAT)), ['type' => 'array', 'items' => ['type' => 'number', 'enum' => [1.1, 2.2]]]]; | ||
yield 'multi float choice min' => [new Choice(['choices' => [1.1, 2.2], 'multiple' => true, 'min' => 2]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_FLOAT)), ['type' => 'array', 'items' => ['type' => 'number', 'enum' => [1.1, 2.2]], 'minItems' => 2]]; | ||
yield 'multi float choice max' => [new Choice(['choices' => [1.1, 2.2, 3.3, 4.4], 'multiple' => true, 'max' => 4]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_FLOAT)), ['type' => 'array', 'items' => ['type' => 'number', 'enum' => [1.1, 2.2, 3.3, 4.4]], 'maxItems' => 4]]; | ||
yield 'multi float choice min/max' => [new Choice(['choices' => [1.1, 2.2, 3.3, 4.4], 'multiple' => true, 'min' => 2, 'max' => 4]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_FLOAT)), ['type' => 'array', 'items' => ['type' => 'number', 'enum' => [1.1, 2.2, 3.3, 4.4]], 'minItems' => 2, 'maxItems' => 4]]; | ||
|
||
yield 'single choice callback' => [new Choice(['callback' => [ChoiceCallback::class, 'getChoices']]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING)), ['enum' => ['a', 'b', 'c', 'd']]]; | ||
yield 'multi choice callback' => [new Choice(['callback' => [ChoiceCallback::class, 'getChoices'], 'multiple' => true]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING)), ['type' => 'array', 'items' => ['type' => 'string', 'enum' => ['a', 'b', 'c', 'd']]]]; | ||
} | ||
} | ||
|
||
final class ChoiceCallback | ||
{ | ||
public static function getChoices(): array | ||
{ | ||
return ['a', 'b', 'c', 'd']; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,73 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* 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 DummyValidatedChoiceEntity | ||
{ | ||
/** | ||
* @var string | ||
* | ||
* @Assert\Choice(choices={"a", "b"}) | ||
*/ | ||
public $dummySingleChoice; | ||
|
||
/** | ||
* @var string | ||
* | ||
* @Assert\Choice(callback={DummyValidatedChoiceEntity::class, "getChoices"}) | ||
*/ | ||
public $dummySingleChoiceCallback; | ||
|
||
/** | ||
* @var string[] | ||
* | ||
* @Assert\Choice(choices={"a", "b"}, multiple=true) | ||
*/ | ||
public $dummyMultiChoice; | ||
|
||
/** | ||
* @var string[] | ||
* | ||
* @Assert\Choice(callback={DummyValidatedChoiceEntity::class, "getChoices"}, multiple=true) | ||
*/ | ||
public $dummyMultiChoiceCallback; | ||
|
||
/** | ||
* @var string[] | ||
* | ||
* @Assert\Choice(choices={"a", "b", "c", "d"}, multiple=true, min=2) | ||
*/ | ||
public $dummyMultiChoiceMin; | ||
|
||
/** | ||
* @var string[] | ||
* | ||
* @Assert\Choice(choices={"a", "b", "c", "d"}, multiple=true, max=4) | ||
*/ | ||
public $dummyMultiChoiceMax; | ||
|
||
/** | ||
* @var string[] | ||
* | ||
* @Assert\Choice(choices={"a", "b", "c", "d"}, multiple=true, min=2, max=4) | ||
*/ | ||
public $dummyMultiChoiceMinMax; | ||
|
||
public static function getChoices(): array | ||
{ | ||
return ['a', 'b', 'c', 'd']; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.