Skip to content

Commit

Permalink
Merge a3909ce into 6b02f15
Browse files Browse the repository at this point in the history
  • Loading branch information
norkunas committed Apr 1, 2021
2 parents 6b02f15 + a3909ce commit e15b8d9
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## 2.7.0

* JSON Schema: Manage Compound constraint when generating property metadata (#4180)
* Validator: Add an option to disable query parameter validation (#4165)
* JSON Schema: Add support for generating property schema with Choice restriction (#4162)
* JSON Schema: Add support for generating property schema with Range restriction (#4158)
Expand Down
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Bic;
use Symfony\Component\Validator\Constraints\CardScheme;
use Symfony\Component\Validator\Constraints\Compound;
use Symfony\Component\Validator\Constraints\Currency;
use Symfony\Component\Validator\Constraints\Date;
use Symfony\Component\Validator\Constraints\DateTime;
Expand Down Expand Up @@ -171,7 +172,7 @@ private function getPropertyConstraints(
}

foreach ($validatorPropertyMetadata->findConstraints($validationGroup) as $propertyConstraint) {
if ($propertyConstraint instanceof Sequentially) {
if ($propertyConstraint instanceof Sequentially || $propertyConstraint instanceof Compound) {
$constraints[] = $propertyConstraint->getNestedContraints();
} else {
$constraints[] = [$propertyConstraint];
Expand Down
Expand Up @@ -24,6 +24,7 @@
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use ApiPlatform\Core\Tests\Fixtures\DummyAtLeastOneOfValidatedEntity;
use ApiPlatform\Core\Tests\Fixtures\DummyCompoundValidatedEntity;
use ApiPlatform\Core\Tests\Fixtures\DummyIriWithValidationEntity;
use ApiPlatform\Core\Tests\Fixtures\DummyRangeValidatedEntity;
use ApiPlatform\Core\Tests\Fixtures\DummySequentiallyValidatedEntity;
Expand All @@ -35,6 +36,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Validator\Constraints\AtLeastOneOf;
use Symfony\Component\Validator\Constraints\Compound;
use Symfony\Component\Validator\Constraints\Sequentially;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
Expand Down Expand Up @@ -378,6 +380,37 @@ public function testCreateWithSequentiallyConstraint(): void
$this->assertArrayHasKey('pattern', $schema);
}

public function testCreateWithCompoundConstraint(): void
{
if (!class_exists(Compound::class)) {
$this->markTestSkipped();
}

$validatorClassMetadata = new ClassMetadata(DummyCompoundValidatedEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);

$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
$validatorMetadataFactory->getMetadataFor(DummyCompoundValidatedEntity::class)
->willReturn($validatorClassMetadata)
->shouldBeCalled();

$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$decoratedPropertyMetadataFactory->create(DummyCompoundValidatedEntity::class, 'dummy', [])->willReturn(
new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING))
)->shouldBeCalled();
$validationPropertyMetadataFactory = new ValidatorPropertyMetadataFactory(
$validatorMetadataFactory->reveal(),
$decoratedPropertyMetadataFactory->reveal(),
[new PropertySchemaLengthRestriction(), new PropertySchemaRegexRestriction()]
);
$schema = $validationPropertyMetadataFactory->create(DummyCompoundValidatedEntity::class, 'dummy')->getSchema();

$this->assertNotNull($schema);
$this->assertArrayHasKey('minLength', $schema);
$this->assertArrayHasKey('maxLength', $schema);
$this->assertArrayHasKey('pattern', $schema);
}

public function testCreateWithAtLeastOneOfConstraint(): void
{
if (!class_exists(AtLeastOneOf::class)) {
Expand Down
26 changes: 26 additions & 0 deletions tests/Fixtures/DummyCompoundValidatedEntity.php
@@ -0,0 +1,26 @@
<?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 ApiPlatform\Core\Tests\Fixtures\TestBundle\Validator\Constraint\DummyCompoundRequirements;

class DummyCompoundValidatedEntity
{
/**
* @var string
*
* @DummyCompoundRequirements
*/
public $dummy;
}
@@ -0,0 +1,32 @@
<?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\TestBundle\Validator\Constraint;

use Symfony\Component\Validator\Constraints\Compound;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\Regex;

/**
* @Annotation
*/
final class DummyCompoundRequirements extends Compound
{
public function getConstraints(array $options): array
{
return [
new Length(['min' => 1, 'max' => 32]),
new Regex(['pattern' => '/^[a-z]$/']),
];
}
}

0 comments on commit e15b8d9

Please sign in to comment.