Skip to content

Commit

Permalink
document generic arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
DjordyKoert committed Jan 7, 2025
1 parent 3b69f62 commit 6417521
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@
<tag name="nelmio_api_doc.schema_describer" priority="100" />
</service>

<service id="nelmio_api_doc.schema_describer.array" class="Nelmio\ApiDocBundle\TypeDescriber\ArrayDescriber" public="false">
<tag name="nelmio_api_doc.schema_describer" />
</service>

<service id="nelmio_api_doc.schema_describer.bool" class="Nelmio\ApiDocBundle\TypeDescriber\BoolDescriber" public="false">
<tag name="nelmio_api_doc.schema_describer" />
</service>
Expand Down
61 changes: 61 additions & 0 deletions src/TypeDescriber/ArrayDescriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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\TypeDescriber;

use OpenApi\Annotations\Schema;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\TypeFactoryTrait;

/**
* @implements TypeDescriberInterface<CollectionType>
*
* @experimental
*
* @internal
*/
final class ArrayDescriber implements TypeDescriberInterface, TypeDescriberAwareInterface
{
use TypeDescriberAwareTrait;

public function describe(Type $type, Schema $schema, array $context = []): void
{
if (!$type->getCollectionKeyType() instanceof Type\CompositeTypeInterface) {

Check failure on line 32 in src/TypeDescriber/ArrayDescriber.php

View workflow job for this annotation

GitHub Actions / PHPStan

Class Symfony\Component\TypeInfo\Type\CompositeTypeInterface not found.
return;
}

$collections = array_map(
fn (Type $keyType): CollectionType => TypeFactoryTrait::collection($type->getWrappedType(), $type->getCollectionValueType(), $keyType),

Check failure on line 37 in src/TypeDescriber/ArrayDescriber.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method Symfony\Component\TypeInfo\Type\CollectionType::getWrappedType().

Check failure on line 37 in src/TypeDescriber/ArrayDescriber.php

View workflow job for this annotation

GitHub Actions / PHPStan

Unable to resolve the template type T in call to method static method Symfony\Component\TypeInfo\TypeFactoryTrait::collection()
$type->getCollectionKeyType()->getTypes(),

Check failure on line 38 in src/TypeDescriber/ArrayDescriber.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to method getTypes() on an unknown class Symfony\Component\TypeInfo\Type\CompositeTypeInterface.
);

if ($type->getCollectionKeyType() instanceof Type\UnionType) {
$describeType = Type::union(...$collections);
}

if ($type->getCollectionKeyType() instanceof Type\IntersectionType) {
$describeType = Type::intersection(...$collections);
}

if (!isset($describeType)) {
return;
}

$this->describer->describe($describeType, $schema, $context);
}

public function supports(Type $type, array $context = []): bool
{
return $type instanceof CollectionType
&& $type->getCollectionKeyType() instanceof Type\CompositeTypeInterface;

Check failure on line 59 in src/TypeDescriber/ArrayDescriber.php

View workflow job for this annotation

GitHub Actions / PHPStan

Class Symfony\Component\TypeInfo\Type\CompositeTypeInterface not found.
}
}
1 change: 1 addition & 0 deletions src/TypeDescriber/DictionaryDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function describe(Type $type, Schema $schema, array $context = []): void
public function supports(Type $type, array $context = []): bool
{
return $type instanceof CollectionType
&& false === $type->getCollectionKeyType() instanceof Type\UnionType
&& $type->getCollectionKeyType()->isIdentifiedBy(TypeIdentifier::STRING);

Check failure on line 44 in src/TypeDescriber/DictionaryDescriber.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method Symfony\Component\TypeInfo\Type::isIdentifiedBy().
}
}
1 change: 1 addition & 0 deletions src/TypeDescriber/ListDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function describe(Type $type, Schema $schema, array $context = []): void
public function supports(Type $type, array $context = []): bool
{
return $type instanceof CollectionType
&& false === $type->getCollectionKeyType() instanceof Type\UnionType
&& $type->getCollectionKeyType()->isIdentifiedBy(TypeIdentifier::INT);

Check failure on line 44 in src/TypeDescriber/ListDescriber.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method Symfony\Component\TypeInfo\Type::isIdentifiedBy().
}
}

0 comments on commit 6417521

Please sign in to comment.