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
Expand Up @@ -86,6 +86,7 @@ public static function createProvider(): \Generator
}
yield 'ipv4' => [new Ip(version: '4'), new ApiProperty(), ['format' => 'ipv4']];
yield 'ipv6' => [new Ip(version: '6'), new ApiProperty(), ['format' => 'ipv6']];
yield 'ipv4 or ipv6' => [new Ip(version: 'all'), new ApiProperty(), ['oneOf' => [['format' => 'ipv4'], ['format' => 'ipv6']]]];
yield 'not supported' => [new Positive(), new ApiProperty(), []];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Symfony\Tests\Validator\Metadata\Property\Restriction;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaFormat;
use ApiPlatform\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaLengthRestriction;
use ApiPlatform\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaOneOfRestriction;
use ApiPlatform\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRegexRestriction;
Expand All @@ -24,6 +25,7 @@
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\AtLeastOneOf;
use Symfony\Component\Validator\Constraints\Ip;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Positive;
Expand All @@ -42,6 +44,7 @@ protected function setUp(): void
$this->propertySchemaOneOfRestriction = new PropertySchemaOneOfRestriction([
new PropertySchemaLengthRestriction(),
new PropertySchemaRegexRestriction(),
new PropertySchemaFormat(),
]);
}

Expand Down Expand Up @@ -91,5 +94,8 @@ public static function createProviderWithNativeType(): \Generator
yield 'native type: one supported constraint' => [new AtLeastOneOf([new Positive(), new Length(min: 3)]), (new ApiProperty())->withNativeType(Type::string()), [
'oneOf' => [['minLength' => 3]],
]];
yield 'native type: ip version all' => [new AtLeastOneOf([new Length(min: 3), new Ip(version: 'all')]), (new ApiProperty())->withNativeType(Type::string()), [
'oneOf' => [['minLength' => 3], ['oneOf' => [['format' => 'ipv4'], ['format' => 'ipv6']]]],
]];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ public function create(Constraint $constraint, ApiProperty $propertyMetadata): a
if ($constraint->version === $constraint::V4) {
return ['format' => 'ipv4'];
}
if ($constraint->version === $constraint::V6) {
return ['format' => 'ipv6'];
}

return ['format' => 'ipv6'];
return ['oneOf' => [['format' => 'ipv4'], ['format' => 'ipv6']]];
}

return [];
Expand Down
Original file line number Diff line number Diff line change
@@ -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\Tests\Fixtures\TestBundle\ApiResource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use Symfony\Component\Validator\Constraints as Assert;

#[ApiResource(
operations: [
new Get(uriTemplate: '/property_schema_format_resources/{id}'),
new GetCollection(uriTemplate: '/property_schema_format_resources'),
]
)]
final class PropertySchemaFormatResource
{
#[ApiProperty(identifier: true)]
public ?int $id = null;

#[Assert\Email]
public ?string $email = null;

#[Assert\Url]
public ?string $url = null;

#[Assert\Hostname]
public ?string $hostname = null;

#[Assert\Uuid]
public ?string $uuid = null;

#[Assert\Ulid]
public ?string $ulid = null;

#[Assert\Ip(version: Assert\Ip::V4)]
public ?string $ipv4 = null;

#[Assert\Ip(version: Assert\Ip::V6)]
public ?string $ipv6 = null;

#[Assert\Ip(version: Assert\Ip::ALL_NO_RES)]
public ?string $ip = null;
}
54 changes: 54 additions & 0 deletions tests/Functional/PropertySchemaFormatFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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\Tests\Functional;

use ApiPlatform\JsonSchema\SchemaFactoryInterface;
use ApiPlatform\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\PropertySchemaFormatResource;
use ApiPlatform\Tests\SetupClassResourcesTrait;

final class PropertySchemaFormatFunctionalTest extends ApiTestCase
{
use SetupClassResourcesTrait;

protected static ?bool $alwaysBootKernel = false;

/**
* @return class-string[]
*/
public static function getResources(): array
{
return [PropertySchemaFormatResource::class];
}

public function testJsonSchemaFormats(): void
{
/** @var SchemaFactoryInterface $schemaFactory */
$schemaFactory = self::getContainer()->get('api_platform.json_schema.schema_factory');
$schema = $schemaFactory->buildSchema(PropertySchemaFormatResource::class);

$properties = $schema['definitions']['PropertySchemaFormatResource']['properties'] ?? null;
$this->assertIsArray($properties);

$this->assertSame('email', $properties['email']['format'] ?? null);
$this->assertSame('uri', $properties['url']['format'] ?? null);
$this->assertSame('hostname', $properties['hostname']['format'] ?? null);
$this->assertSame('uuid', $properties['uuid']['format'] ?? null);
$this->assertSame('ulid', $properties['ulid']['format'] ?? null);
$this->assertSame('ipv4', $properties['ipv4']['format'] ?? null);
$this->assertSame('ipv6', $properties['ipv6']['format'] ?? null);

$this->assertSame([['format' => 'ipv4'], ['format' => 'ipv6']], $properties['ip']['oneOf'] ?? null);
}
}
Loading