Skip to content

Commit

Permalink
Added UUID property describer
Browse files Browse the repository at this point in the history
with support for symfony/uid and ramsey/uuid
  • Loading branch information
stollr committed Apr 28, 2023
1 parent ebe6f59 commit 28f7671
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
38 changes: 38 additions & 0 deletions PropertyDescriber/UuidPropertyDescriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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 Ramsey\Uuid\UuidInterface;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Uid\Uuid;

class UuidPropertyDescriber implements PropertyDescriberInterface
{
use NullablePropertyTrait;

public function describe(array $types, OA\Schema $property, array $groups = null)
{
$property->type = 'string';
$property->format = 'uuid';
$property->pattern = '^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$';
$this->setNullableProperty($types[0], $property);
}

public function supports(array $types): bool
{
return 1 === count($types)
&& Type::BUILTIN_TYPE_OBJECT === $types[0]->getBuiltinType()
&& (is_a($types[0]->getClassName(), Uuid::class, true)
|| is_a($types[0]->getClassName(), UuidInterface::class, true));
}
}
4 changes: 4 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@
<tag name="nelmio_api_doc.object_model.property_describer" priority="-1001" />
</service>

<service id="nelmio_api_doc.object_model.property_describers.uuid" class="Nelmio\ApiDocBundle\PropertyDescriber\UuidPropertyDescriber" public="false">
<tag name="nelmio_api_doc.object_model.property_describer" />
</service>

<!-- Form Type Extensions -->

<service id="nelmio_api_doc.form.documentation_extension" class="Nelmio\ApiDocBundle\Form\Extension\DocumentationExtension">
Expand Down

0 comments on commit 28f7671

Please sign in to comment.