Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added UUID property describer #2098

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -56,6 +56,7 @@
"symfony/stopwatch": "^5.4 || ^6.0 || ^7.0",
"symfony/templating": "^5.4 || ^6.0 || ^7.0",
"symfony/twig-bundle": "^5.4 || ^6.0 || ^7.0",
"symfony/uid": "^5.4 || ^6.0 || ^7.0",
"symfony/validator": "^5.4 || ^6.0 || ^7.0",
"willdurand/hateoas-bundle": "^1.0 || ^2.0"
},
Expand Down
12 changes: 12 additions & 0 deletions config/symfony_uid.xml
@@ -0,0 +1,12 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<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>
</services>

</container>
4 changes: 4 additions & 0 deletions src/DependencyInjection/NelmioApiDocExtension.php
Expand Up @@ -171,6 +171,10 @@ public function load(array $configs, ContainerBuilder $container): void
$container->registerForAutoconfiguration(ModelDescriberInterface::class)
->addTag('nelmio_api_doc.model_describer');

if (class_exists(\Symfony\Component\Uid\AbstractUid::class)) {
$loader->load('symfony_uid.xml');
}

// Import services needed for each library
$loader->load('php_doc.xml');

Expand Down
32 changes: 32 additions & 0 deletions src/PropertyDescriber/UuidPropertyDescriber.php
@@ -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;

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);
}
}
12 changes: 12 additions & 0 deletions tests/Functional/Controller/ApiController80.php
Expand Up @@ -26,6 +26,7 @@
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithNullableSchemaSet;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithObjectType;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithRef;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithUuid;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\RangeInteger;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\SymfonyConstraints80;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\SymfonyConstraintsWithValidationGroups;
Expand Down Expand Up @@ -414,6 +415,17 @@ public function entityWithObjectType()
{
}

/**
* @Route("/entity-with-uuid", methods={"GET", "POST"})
*
* @OA\Response(response=200, description="success", @OA\JsonContent(
* ref=@Model(type=EntityWithUuid::class),
* ))
*/
public function entityWithUuid()
{
}

/**
* @Route("/form-with-alternate-type", methods={"POST"})
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Functional/Controller/ApiController81.php
Expand Up @@ -27,6 +27,7 @@
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithNullableSchemaSet;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithObjectType;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithRef;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithUuid;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\QueryModel\ArrayQueryModel;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\QueryModel\FilterQueryModel;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\QueryModel\PaginationQueryModel;
Expand Down Expand Up @@ -345,6 +346,18 @@ public function entityWithObjectType()
{
}

#[Route('/entity-with-uuid', methods: ['GET', 'POST'])]
#[OA\Response(
response: 200,
description: 'success',
content: new OA\JsonContent(
ref: new Model(type: EntityWithUuid::class),
),
)]
public function entityWithUuid()
{
}

#[Route('/form-with-alternate-type', methods: ['POST'])]
#[OA\Response(
response: 204,
Expand Down
26 changes: 26 additions & 0 deletions tests/Functional/Entity/EntityWithUuid.php
@@ -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;
}
}
18 changes: 18 additions & 0 deletions tests/Functional/FunctionalTest.php
Expand Up @@ -896,6 +896,24 @@ public function testEntitiesWithOverriddenSchemaTypeDoNotReadOtherProperties():
self::assertSame(Generator::UNDEFINED, $model->properties);
}

public function testEntityWithUuid(): void
{
self::assertEquals([
'schema' => 'EntityWithUuid',
'type' => 'object',
'required' => ['id', 'name'],
'properties' => [
'id' => [
'type' => 'string',
'format' => 'uuid',
],
'name' => [
'type' => 'string',
],
],
], json_decode($this->getModel('EntityWithUuid')->toJson(), true));
}

public function testEntitiesWithRefInSchemaDoNoReadOtherProperties(): void
{
$model = $this->getModel('EntityWithRef');
Expand Down
77 changes: 77 additions & 0 deletions tests/PropertyDescriber/UuidPropertyDescriberTest.php
@@ -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
}
}