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
5 changes: 4 additions & 1 deletion src/PropertyDescriber/UuidPropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OpenApi\Annotations as OA;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\Ulid;

final class UuidPropertyDescriber implements PropertyDescriberInterface
{
Expand All @@ -23,7 +24,9 @@ final class UuidPropertyDescriber implements PropertyDescriberInterface
public function describe(array $types, OA\Schema $property, array $context = []): void
{
$property->type = 'string';
$property->format = 'uuid';

$isUlid = is_a($types[0]->getClassName(), Ulid::class, true);
$property->format = $isUlid ? 'ulid' : 'uuid';
}

public function supports(array $types, array $context = []): bool
Expand Down
8 changes: 8 additions & 0 deletions src/TypeDescriber/ClassDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\Type\ObjectType;
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\Ulid;
use Symfony\Contracts\Translation\TranslatableInterface;

/**
Expand All @@ -34,6 +35,13 @@ final class ClassDescriber implements TypeDescriberInterface, ModelRegistryAware

public function describe(Type $type, Schema $schema, array $context = []): void
{
if (is_a($type->getClassName(), Ulid::class, true)) {
$schema->type = 'string';
$schema->format = 'ulid';

return;
}

if (is_a($type->getClassName(), AbstractUid::class, true)) {
$schema->type = 'string';
$schema->format = 'uuid';
Expand Down
13 changes: 13 additions & 0 deletions tests/Functional/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithObjectType;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithRef;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithTranslatable;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithUlid;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithUuid;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\RangeInteger;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\SymfonyConstraints;
Expand Down Expand Up @@ -363,6 +364,18 @@ public function entityWithUuid()
{
}

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

#[Route('/entity-with-translatable', methods: ['GET', 'POST'])]
#[OA\Response(
response: 200,
Expand Down
26 changes: 26 additions & 0 deletions tests/Functional/Entity/EntityWithUlid.php
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\Ulid;

class EntityWithUlid
{
public Ulid $id;
public string $name;

public function __construct(string $name)
{
$this->id = new Ulid();
$this->name = $name;
}
}
18 changes: 18 additions & 0 deletions tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,24 @@ public function testEntityWithUuid(): void
], json_decode($this->getModel('EntityWithUuid')->toJson(), true));
}

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

public function testEntityWithTranslatable(): void
{
self::assertEquals([
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/ModelDescriber/Fixtures/UuidClass.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"ulid": {
"type": "string",
"format": "uuid"
"format": "ulid"
},
"uuidV1": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"ulid": {
"type": "string",
"format": "uuid"
"format": "ulid"
},
"uuidV1": {
"type": "string",
Expand Down
27 changes: 26 additions & 1 deletion tests/PropertyDescriber/UuidPropertyDescriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Nelmio\ApiDocBundle\PropertyDescriber\UuidPropertyDescriber;
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;

class UuidPropertyDescriberTest extends TestCase
Expand All @@ -27,6 +28,15 @@ public function testSupportsUuidPropertyType(): void
self::assertTrue($describer->supports([$type]));
}

public function testSupportsUlidPropertyType(): void
{
$type = new Type(Type::BUILTIN_TYPE_OBJECT, false, Ulid::class);

$describer = new UuidPropertyDescriber();

self::assertTrue($describer->supports([$type]));
}

public function testSupportsNoIntPropertyType(): void
{
$type = new Type(Type::BUILTIN_TYPE_INT, false);
Expand All @@ -50,12 +60,27 @@ public function testDescribeUuidPropertyType(): void
$property = $this->initProperty();

$describer = new UuidPropertyDescriber();
$describer->describe([], $property, []);

$type = new Type(Type::BUILTIN_TYPE_OBJECT, false, Uuid::class);
$describer->describe([$type], $property, []);

self::assertSame('string', $property->type);
self::assertSame('uuid', $property->format);
}

public function testDescribeUlidPropertyType(): void
{
$property = $this->initProperty();

$describer = new UuidPropertyDescriber();

$type = new Type(Type::BUILTIN_TYPE_OBJECT, false, Ulid::class);
$describer->describe([$type], $property, []);

self::assertSame('string', $property->type);
self::assertSame('ulid', $property->format);
}

private function initProperty(): \OpenApi\Annotations\Property
{
return new \OpenApi\Attributes\Property(); // union types, used in schema attribute require PHP >= 8.0.0
Expand Down