Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Symfony\Tests\Fixtures;

use Symfony\Component\Validator\Constraints as Assert;

class DummySequentiallyValidatedEntityWithNestedGroups
{
#[Assert\Sequentially([
new Assert\NotBlank(groups: ['some group']),
new Assert\Range(min: '0.01'),
])]
public ?string $dummy = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use ApiPlatform\Symfony\Tests\Fixtures\DummyNumericValidatedEntity;
use ApiPlatform\Symfony\Tests\Fixtures\DummyRangeValidatedEntity;
use ApiPlatform\Symfony\Tests\Fixtures\DummySequentiallyValidatedEntity;
use ApiPlatform\Symfony\Tests\Fixtures\DummySequentiallyValidatedEntityWithNestedGroups;
use ApiPlatform\Symfony\Tests\Fixtures\DummyUniqueValidatedEntity;
use ApiPlatform\Symfony\Tests\Fixtures\DummyValidatedChoiceEntity;
use ApiPlatform\Symfony\Tests\Fixtures\DummyValidatedEntity;
Expand Down Expand Up @@ -450,6 +451,32 @@ public function testCreateWithSequentiallyConstraint(): void
$this->assertArrayHasKey('pattern', $schema);
}

public function testSequentiallyConstraintDoesNotMarkRequiredFromNestedConstraintWithDifferentGroup(): void
{
$validatorClassMetadata = new ClassMetadata(DummySequentiallyValidatedEntityWithNestedGroups::class);
(new AttributeLoader())->loadClassMetadata($validatorClassMetadata);

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

$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$decoratedPropertyMetadataFactory->create(DummySequentiallyValidatedEntityWithNestedGroups::class, 'dummy', [])->willReturn(
(new ApiProperty())->withNativeType(Type::string())
)->shouldBeCalled();

$validationPropertyMetadataFactory = new ValidatorPropertyMetadataFactory(
$validatorMetadataFactory->reveal(),
$decoratedPropertyMetadataFactory->reveal(),
[]
);

$propertyMetadata = $validationPropertyMetadataFactory->create(DummySequentiallyValidatedEntityWithNestedGroups::class, 'dummy');

$this->assertFalse($propertyMetadata->isRequired());
}

public function testCreateWithCompoundConstraint(): void
{
$validatorClassMetadata = new ClassMetadata(DummyCompoundValidatedEntity::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ private function getPropertyConstraints(

foreach ($validatorPropertyMetadata->findConstraints($validationGroup) as $propertyConstraint) {
if ($propertyConstraint instanceof Sequentially || $propertyConstraint instanceof Compound) {
$constraints[] = $propertyConstraint->getNestedConstraints();
foreach ($propertyConstraint->getNestedConstraints() as $nestedConstraint) {
if (\in_array($validationGroup, $nestedConstraint->groups, true)) {
$constraints[] = [$nestedConstraint];
}
}
} else {
$constraints[] = [$propertyConstraint];
}
Expand Down
Loading