-
-
Notifications
You must be signed in to change notification settings - Fork 836
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added symfony/uuid property describer (#2098)
This describer adds support for UUIDs from the following libraries: symfony/uid and ramsey/uuid This makes types of these classes configured as ``` "type": "string", "format": "uuid", "pattern": "^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$" ``` This is based on the [spec](https://swagger.io/docs/specification/data-models/data-types/#string). --------- Co-authored-by: djordy <[email protected]>
- Loading branch information
1 parent
599283f
commit 221a1fe
Showing
9 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the NelmioApiDocBundle package. | ||
* | ||
* (c) Nelmio | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nelmio\ApiDocBundle\PropertyDescriber; | ||
|
||
use OpenApi\Annotations as OA; | ||
use Symfony\Component\PropertyInfo\Type; | ||
use Symfony\Component\Uid\AbstractUid; | ||
|
||
final class UuidPropertyDescriber implements PropertyDescriberInterface | ||
{ | ||
public function describe(array $types, OA\Schema $property, ?array $groups = null, ?OA\Schema $schema = null) | ||
{ | ||
$property->type = 'string'; | ||
$property->format = 'uuid'; | ||
} | ||
|
||
public function supports(array $types): bool | ||
{ | ||
return 1 === count($types) | ||
&& Type::BUILTIN_TYPE_OBJECT === $types[0]->getBuiltinType() | ||
&& is_a($types[0]->getClassName(), AbstractUid::class, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the NelmioApiDocBundle package. | ||
* | ||
* (c) Nelmio | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nelmio\ApiDocBundle\Tests\Functional\Entity; | ||
|
||
use Symfony\Component\Uid\Uuid; | ||
|
||
class EntityWithUuid | ||
{ | ||
public Uuid $id; | ||
public string $name; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->id = Uuid::v1(); | ||
$this->name = $name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the NelmioApiDocBundle package. | ||
* | ||
* (c) Nelmio | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nelmio\ApiDocBundle\Tests\PropertyDescriber; | ||
|
||
use Nelmio\ApiDocBundle\PropertyDescriber\UuidPropertyDescriber; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\PropertyInfo\Type; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
class UuidPropertyDescriberTest extends TestCase | ||
{ | ||
public function testSupportsUuidPropertyType(): void | ||
{ | ||
$type = new Type(Type::BUILTIN_TYPE_OBJECT, false, Uuid::class); | ||
|
||
$describer = new UuidPropertyDescriber(); | ||
|
||
self::assertTrue($describer->supports([$type])); | ||
} | ||
|
||
public function testSupportsNoIntPropertyType(): void | ||
{ | ||
$type = new Type(Type::BUILTIN_TYPE_INT, false); | ||
|
||
$describer = new UuidPropertyDescriber(); | ||
|
||
self::assertFalse($describer->supports([$type])); | ||
} | ||
|
||
public function testSupportsNoDifferentObjectPropertyType(): void | ||
{ | ||
$type = new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateTimeInterface::class); | ||
|
||
$describer = new UuidPropertyDescriber(); | ||
|
||
self::assertFalse($describer->supports([$type])); | ||
} | ||
|
||
public function testDescribeUuidPropertyType(): void | ||
{ | ||
$property = $this->initProperty(); | ||
$schema = $this->initSchema(); | ||
|
||
$describer = new UuidPropertyDescriber(); | ||
$describer->describe([], $property, [], $schema); | ||
|
||
self::assertSame('string', $property->type); | ||
self::assertSame('uuid', $property->format); | ||
} | ||
|
||
private function initProperty(): \OpenApi\Annotations\Property | ||
{ | ||
if (PHP_VERSION_ID < 80000) { | ||
return new \OpenApi\Annotations\Property([]); | ||
} | ||
|
||
return new \OpenApi\Attributes\Property(); // union types, used in schema attribute require PHP >= 8.0.0 | ||
} | ||
|
||
private function initSchema(): \OpenApi\Annotations\Schema | ||
{ | ||
if (PHP_VERSION_ID < 80000) { | ||
return new \OpenApi\Annotations\Schema([]); | ||
} | ||
|
||
return new \OpenApi\Attributes\Schema(); // union types, used in schema attribute require PHP >= 8.0.0 | ||
} | ||
} |