Skip to content

Commit

Permalink
Merge 6fb68e1 into 4717bd5
Browse files Browse the repository at this point in the history
  • Loading branch information
norkunas committed Mar 17, 2021
2 parents 4717bd5 + 6fb68e1 commit b9f794e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
Expand Up @@ -17,8 +17,10 @@
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\AtLeastOneOf;
use Symfony\Component\Validator\Constraints\Bic;
use Symfony\Component\Validator\Constraints\CardScheme;
use Symfony\Component\Validator\Constraints\Composite;
use Symfony\Component\Validator\Constraints\Currency;
use Symfony\Component\Validator\Constraints\Date;
use Symfony\Component\Validator\Constraints\DateTime;
Expand All @@ -30,6 +32,7 @@
use Symfony\Component\Validator\Constraints\Issn;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Sequentially;
use Symfony\Component\Validator\Constraints\Time;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Constraints\Uuid;
Expand Down Expand Up @@ -170,11 +173,15 @@ private function getPropertyConstraints(
}

foreach ($validatorPropertyMetadata->findConstraints($validationGroup) as $propertyConstraint) {
$constraints[] = $propertyConstraint;
if ($propertyConstraint instanceof Sequentially) {
$constraints[] = $propertyConstraint->getNestedContraints();
} else {
$constraints[] = [$propertyConstraint];
}
}
}

return $constraints;
return array_merge([], ...$constraints);
}

/**
Expand Down
Expand Up @@ -20,11 +20,13 @@
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use ApiPlatform\Core\Tests\Fixtures\DummyIriWithValidationEntity;
use ApiPlatform\Core\Tests\Fixtures\DummySequentiallyValidatedEntity;
use ApiPlatform\Core\Tests\Fixtures\DummyValidatedEntity;
use ApiPlatform\Core\Tests\ProphecyTrait;
use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Validator\Constraints\Sequentially;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
Expand Down Expand Up @@ -335,4 +337,35 @@ public function testCreateWithPropertyFormatRestriction(): void
$this->assertEquals($format, $schema['format']);
}
}

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

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

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

$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$decoratedPropertyMetadataFactory->create(DummySequentiallyValidatedEntity::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(DummySequentiallyValidatedEntity::class, 'dummy')->getSchema();

$this->assertNotNull($schema);
$this->assertArrayHasKey('minLength', $schema);
$this->assertArrayHasKey('maxLength', $schema);
$this->assertArrayHasKey('pattern', $schema);
}
}
30 changes: 30 additions & 0 deletions tests/Fixtures/DummySequentiallyValidatedEntity.php
@@ -0,0 +1,30 @@
<?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 DummySequentiallyValidatedEntity
{
/**
* @var string
*
* @Assert\Sequentially({
* @Assert\Type(type="string"),
* @Assert\Length(min=1, max=32),
* @Assert\Regex(pattern="/^[a-z]$/")
* })
*/
public $dummy;
}

0 comments on commit b9f794e

Please sign in to comment.