Skip to content
Open
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,89 @@
<?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;

/**
* Dummy Validated Entity.
*
* @author Baptiste Meyer <baptiste.meyer@gmail.com>
*/
#[Assert\GroupSequence(['dummy', 'DummyValidatedEntityWithGroupSequence'])]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting as 8031652 works, I'm wondering if there's something that we should do rather in the contextthen inside property metadata factories but I'm unsure about this...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how it's related.
And I just discover the GroupSequence feature today. So I dunno

My issue is not related to the fact it's validated (I think it works), only to the schema changed (loosing the "required" flag) when generating the OpenAPI schema.

Adding a GroupSequence to a class is something documented on Symfony
https://symfony.com/doc/current/validation/sequence_provider.html
so should be supported in the ValidatorPropertyMetadataFactory, no ?

class DummyValidatedEntityWithGroupSequence
{
/**
* @var int A dummy ID
*/
public $dummyId;

/**
* @var string A dummy
*/
#[Assert\NotBlank]
#[Assert\Length(max: 4, min: 10)]
#[Assert\Regex(pattern: '/^dummy$/')]
public $dummy;

/**
* @var string
*/
#[Assert\Email]
#[Assert\NotBlank(allowNull: true)]
public $dummyEmail;

/**
* @var string
*/
#[Assert\Uuid]
public $dummyUuid;

/**
* @var string
*/
#[Assert\Ip]
public $dummyIpv4;

/**
* @var string
*/
#[Assert\Ip(version: '6')]
public $dummyIpv6;

/**
* @var \DateTimeInterface A dummy date
*/
#[Assert\Date]
public $dummyDate;

/**
* @var string A dummy group
*/
#[Assert\NotNull(groups: ['dummy'])]
public $dummyGroup;

/**
* @var string A dummy url
*/
#[Assert\Url()]
public $dummyUrl;

/**
* @return string[]
*/
public static function getValidationGroups(): array
{
return ['dummy'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use ApiPlatform\Symfony\Tests\Fixtures\DummyUniqueValidatedEntity;
use ApiPlatform\Symfony\Tests\Fixtures\DummyValidatedChoiceEntity;
use ApiPlatform\Symfony\Tests\Fixtures\DummyValidatedEntity;
use ApiPlatform\Symfony\Tests\Fixtures\DummyValidatedEntityWithGroupSequence;
use ApiPlatform\Symfony\Tests\Fixtures\DummyValidatedHostnameEntity;
use ApiPlatform\Symfony\Tests\Fixtures\DummyValidatedUlidEntity;
use ApiPlatform\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaChoiceRestriction;
Expand Down Expand Up @@ -141,6 +142,30 @@ public function testCreateWithPropertyWithoutConstraints(): void
$this->assertEquals($expectedPropertyMetadata, $resultedPropertyMetadata);
}

public function testCreateWithPropertyWithRequiredConstraintsAndGroupSequence(): void
{
$propertyMetadata = (new ApiProperty())->withDescription('A dummy group')->withReadable(true)->withWritable(true);
$expectedPropertyMetadata = $propertyMetadata->withRequired(true);

$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$decoratedPropertyMetadataFactory->create(DummyValidatedEntityWithGroupSequence::class, 'dummyGroup', [])->willReturn($propertyMetadata)->shouldBeCalled();

$validatorClassMetadata = new ClassMetadata(DummyValidatedEntityWithGroupSequence::class);
(new AttributeLoader())->loadClassMetadata($validatorClassMetadata);

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

$validatorPropertyMetadataFactory = new ValidatorPropertyMetadataFactory(
$validatorMetadataFactory->reveal(),
$decoratedPropertyMetadataFactory->reveal(),
[]
);
$resultedPropertyMetadata = $validatorPropertyMetadataFactory->create(DummyValidatedEntityWithGroupSequence::class, 'dummyGroup');

$this->assertEquals($expectedPropertyMetadata, $resultedPropertyMetadata);
}

public function testCreateWithPropertyWithRightValidationGroupsAndRequiredConstraints(): void
{
$propertyMetadata = (new ApiProperty())->withDescription('A dummy group')->withReadable(true)->withWritable(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ private function getValidationGroups(ValidatorClassMetadataInterface $classMetad
}
}

if ($classMetadata->hasGroupSequence()) {
return $classMetadata->getGroupSequence()->groups;
}

if (!method_exists($classMetadata, 'getDefaultGroup')) {
throw new \UnexpectedValueException(\sprintf('Validator class metadata expected to have method "%s".', 'getDefaultGroup'));
}
Expand Down
Loading