Skip to content

Commit

Permalink
Merge 02a6efa into 69229fb
Browse files Browse the repository at this point in the history
  • Loading branch information
norkunas committed Apr 2, 2021
2 parents 69229fb + 02a6efa commit ecf7a73
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## 2.7.0

* JSON Schema: Add support for generating property schema with Url restriction (#4185)
* 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)
Expand Down
4 changes: 4 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/validator.xml
Expand Up @@ -46,6 +46,10 @@
<tag name="api_platform.metadata.property_schema_restriction"/>
</service>

<service id="api_platform.metadata.property_schema.url_restriction" class="ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaUrlRestriction" public="false">
<tag name="api_platform.metadata.property_schema_restriction"/>
</service>

<service id="api_platform.listener.view.validate" class="ApiPlatform\Core\Validator\EventListener\ValidateListener">
<argument type="service" id="api_platform.validator" />
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
Expand Down
Expand Up @@ -16,8 +16,10 @@
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Hostname;
use Symfony\Component\Validator\Constraints\Ip;
use Symfony\Component\Validator\Constraints\Ulid;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Constraints\Uuid;

/**
Expand All @@ -36,6 +38,14 @@ public function create(Constraint $constraint, PropertyMetadata $propertyMetadat
return ['format' => 'email'];
}

if ($constraint instanceof Url) {
return ['format' => 'uri'];
}

if ($constraint instanceof Hostname) {
return ['format' => 'hostname'];
}

if ($constraint instanceof Uuid) {
return ['format' => 'uuid'];
}
Expand Down
@@ -0,0 +1,59 @@
<?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\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Constraints\UrlValidator;

/**
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
*/
final class PropertySchemaUrlRestriction implements PropertySchemaRestrictionMetadataInterface
{
/**
* {@inheritdoc}
*
* @param Url $constraint
*/
public function create(Constraint $constraint, PropertyMetadata $propertyMetadata): array
{
$pattern = $constraint->relativeProtocol ? str_replace('(%s):', '(?:(%s):)?', UrlValidator::PATTERN) : UrlValidator::PATTERN;

$pattern = array_map(static function (string $line) {
$line = trim($line);

$commentPos = strrpos($line, '#');
if (false !== $commentPos) {
$line = trim(substr($line, 0, $commentPos));
}

return $line;
}, explode("\n", $pattern));

$pattern = substr(implode('', $pattern), 1, -4); // strip options and delimiters
$pattern = sprintf($pattern, implode('|', $constraint->protocols));

return ['pattern' => $pattern];
}

/**
* {@inheritdoc}
*/
public function supports(Constraint $constraint, PropertyMetadata $propertyMetadata): bool
{
return $constraint instanceof Url;
}
}
Expand Up @@ -1343,6 +1343,7 @@ private function getBaseContainerBuilderProphecyWithoutDefaultMetadataLoading(ar
'api_platform.metadata.property_schema.regex_restriction',
'api_platform.metadata.property_schema.format_restriction',
'api_platform.metadata.property_schema.unique_restriction',
'api_platform.metadata.property_schema.url_restriction',
'api_platform.metadata.property.metadata_factory.yaml',
'api_platform.metadata.property.name_collection_factory.yaml',
'api_platform.metadata.resource.filter_metadata_factory.annotation',
Expand Down
@@ -0,0 +1,56 @@
<?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\PropertySchemaUrlRestriction;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use ApiPlatform\Core\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Positive;
use Symfony\Component\Validator\Constraints\Url;

/**
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
*/
final class PropertySchemaUrlRestrictionTest extends TestCase
{
use ProphecyTrait;

private $propertySchemaUrlRestriction;

protected function setUp(): void
{
$this->propertySchemaUrlRestriction = new PropertySchemaUrlRestriction();
}

/**
* @dataProvider supportsProvider
*/
public function testSupports(Constraint $constraint, PropertyMetadata $propertyMetadata, bool $expectedResult): void
{
self::assertSame($expectedResult, $this->propertySchemaUrlRestriction->supports($constraint, $propertyMetadata));
}

public function supportsProvider(): \Generator
{
yield 'supported' => [new Url(), new PropertyMetadata(), true];
yield 'not supported' => [new Positive(), new PropertyMetadata(), false];
}

public function testCreate(): void
{
self::assertArrayHasKey('pattern', $this->propertySchemaUrlRestriction->create(new Url(), new PropertyMetadata()));
}
}
Expand Up @@ -20,6 +20,7 @@
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRangeRestriction;
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRegexRestriction;
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaUniqueRestriction;
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaUrlRestriction;
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\ValidatorPropertyMetadataFactory;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
Expand Down Expand Up @@ -547,4 +548,29 @@ public function provideChoiceConstraintCases(): \Generator
yield 'multi choice max' => ['propertyMetadata' => new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING)), 'property' => 'dummyMultiChoiceMax', 'expectedSchema' => ['type' => 'array', 'items' => ['type' => 'string', 'enum' => ['a', 'b', 'c', 'd']], 'maxItems' => 4]];
yield 'multi choice min/max' => ['propertyMetadata' => new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING)), 'property' => 'dummyMultiChoiceMinMax', 'expectedSchema' => ['type' => 'array', 'items' => ['type' => 'string', 'enum' => ['a', 'b', 'c', 'd']], 'minItems' => 2, 'maxItems' => 4]];
}

public function testCreateWithPropertyUrlRestriction(): void
{
$validatorClassMetadata = new ClassMetadata(DummyValidatedEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);

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

$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$decoratedPropertyMetadataFactory->create(DummyValidatedEntity::class, 'dummyUrl', [])->willReturn(
new PropertyMetadata()
)->shouldBeCalled();
$validationPropertyMetadataFactory = new ValidatorPropertyMetadataFactory(
$validatorMetadataFactory->reveal(),
$decoratedPropertyMetadataFactory->reveal(),
[new PropertySchemaUrlRestriction()]
);
$schema = $validationPropertyMetadataFactory->create(DummyValidatedEntity::class, 'dummyUrl')->getSchema();

$this->assertNotNull($schema);
$this->assertArrayHasKey('pattern', $schema);
}
}
7 changes: 7 additions & 0 deletions tests/Fixtures/DummyValidatedEntity.php
Expand Up @@ -77,4 +77,11 @@ class DummyValidatedEntity
* @Assert\NotNull(groups={"dummy"})
*/
public $dummyGroup;

/**
* @var string A dummy url
*
* @Assert\Url
*/
public $dummyUrl;
}

0 comments on commit ecf7a73

Please sign in to comment.