Skip to content

Commit 942013b

Browse files
committed
Add support for generating property schema format for Url and Hostname
1 parent 80fa20e commit 942013b

File tree

5 files changed

+116
-1
lines changed

5 files changed

+116
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 2.7.0
44

5+
* JSON Schema: Add support for generating property schema format for Url and Hostname (#4185)
56
* JSON Schema: Add support for generating property schema with Count restriction (#4186)
67
* JSON Schema: Manage Compound constraint when generating property metadata (#4180)
78
* Validator: Add an option to disable query parameter validation (#4165)

src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaFormat.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
1717
use Symfony\Component\Validator\Constraint;
1818
use Symfony\Component\Validator\Constraints\Email;
19+
use Symfony\Component\Validator\Constraints\Hostname;
1920
use Symfony\Component\Validator\Constraints\Ip;
2021
use Symfony\Component\Validator\Constraints\Ulid;
22+
use Symfony\Component\Validator\Constraints\Url;
2123
use Symfony\Component\Validator\Constraints\Uuid;
2224

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

41+
if ($constraint instanceof Url) {
42+
return ['format' => 'uri'];
43+
}
44+
45+
if ($constraint instanceof Hostname) {
46+
return ['format' => 'hostname'];
47+
}
48+
3949
if ($constraint instanceof Uuid) {
4050
return ['format' => 'uuid'];
4151
}
@@ -62,6 +72,6 @@ public function supports(Constraint $constraint, PropertyMetadata $propertyMetad
6272
{
6373
$schema = $propertyMetadata->getSchema();
6474

65-
return empty($schema['format']) && ($constraint instanceof Email || $constraint instanceof Uuid || $constraint instanceof Ulid || $constraint instanceof Ip);
75+
return empty($schema['format']) && ($constraint instanceof Email || $constraint instanceof Url || $constraint instanceof Hostname || $constraint instanceof Uuid || $constraint instanceof Ulid || $constraint instanceof Ip);
6676
}
6777
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Bridge\Symfony\Validator\Metadata\Property\Restriction;
15+
16+
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaFormat;
17+
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
18+
use ApiPlatform\Core\Tests\ProphecyTrait;
19+
use PHPUnit\Framework\TestCase;
20+
use Symfony\Component\Validator\Constraint;
21+
use Symfony\Component\Validator\Constraints\Email;
22+
use Symfony\Component\Validator\Constraints\Hostname;
23+
use Symfony\Component\Validator\Constraints\Ip;
24+
use Symfony\Component\Validator\Constraints\Positive;
25+
use Symfony\Component\Validator\Constraints\Ulid;
26+
use Symfony\Component\Validator\Constraints\Url;
27+
use Symfony\Component\Validator\Constraints\Uuid;
28+
29+
final class PropertySchemaFormatTest extends TestCase
30+
{
31+
use ProphecyTrait;
32+
33+
private $propertySchemaFormatRestriction;
34+
35+
protected function setUp(): void
36+
{
37+
$this->propertySchemaFormatRestriction = new PropertySchemaFormat();
38+
}
39+
40+
/**
41+
* @dataProvider supportsProvider
42+
*/
43+
public function testSupports(Constraint $constraint, PropertyMetadata $propertyMetadata, bool $expectedResult): void
44+
{
45+
self::assertSame($expectedResult, $this->propertySchemaFormatRestriction->supports($constraint, $propertyMetadata));
46+
}
47+
48+
public function supportsProvider(): \Generator
49+
{
50+
yield 'email' => [new Email(), new PropertyMetadata(), true];
51+
yield 'url' => [new Url(), new PropertyMetadata(), true];
52+
yield 'hostname' => [new Hostname(), new PropertyMetadata(), true];
53+
yield 'uuid' => [new Uuid(), new PropertyMetadata(), true];
54+
55+
if (class_exists(Ulid::class)) {
56+
yield 'ulid' => [new Ulid(), new PropertyMetadata(), true];
57+
}
58+
59+
yield 'ip' => [new Ip(), new PropertyMetadata(), true];
60+
61+
yield 'not supported' => [new Positive(), new PropertyMetadata(), false];
62+
}
63+
64+
/**
65+
* @dataProvider createProvider
66+
*/
67+
public function testCreate(Constraint $constraint, PropertyMetadata $propertyMetadata, array $expectedResult): void
68+
{
69+
self::assertSame($expectedResult, $this->propertySchemaFormatRestriction->create($constraint, $propertyMetadata));
70+
}
71+
72+
public function createProvider(): \Generator
73+
{
74+
yield 'email' => [new Email(), new PropertyMetadata(), ['format' => 'email']];
75+
yield 'url' => [new Url(), new PropertyMetadata(), ['format' => 'uri']];
76+
yield 'hostname' => [new Hostname(), new PropertyMetadata(), ['format' => 'hostname']];
77+
yield 'uuid' => [new Uuid(), new PropertyMetadata(), ['format' => 'uuid']];
78+
79+
if (class_exists(Ulid::class)) {
80+
yield 'ulid' => [new Ulid(), new PropertyMetadata(), ['format' => 'ulid']];
81+
}
82+
83+
yield 'ipv4' => [new Ip(['version' => '4']), new PropertyMetadata(), ['format' => 'ipv4']];
84+
yield 'ipv6' => [new Ip(['version' => '6']), new PropertyMetadata(), ['format' => 'ipv6']];
85+
86+
yield 'not supported' => [new Positive(), new PropertyMetadata(), []];
87+
}
88+
}

tests/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactoryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ public function testCreateWithPropertyFormatRestriction(): void
332332
'dummyUuid' => 'uuid',
333333
'dummyIpv4' => 'ipv4',
334334
'dummyIpv6' => 'ipv6',
335+
'dummyUrl' => 'uri',
336+
'dummyHostname' => 'hostname',
335337
];
336338

337339
foreach ($formats as $property => $format) {

tests/Fixtures/DummyValidatedEntity.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,18 @@ class DummyValidatedEntity
7777
* @Assert\NotNull(groups={"dummy"})
7878
*/
7979
public $dummyGroup;
80+
81+
/**
82+
* @var string A dummy url
83+
*
84+
* @Assert\Url
85+
*/
86+
public $dummyUrl;
87+
88+
/**
89+
* @var string A dummy hostname
90+
*
91+
* @Assert\Hostname()
92+
*/
93+
public $dummyHostname;
8094
}

0 commit comments

Comments
 (0)