Skip to content

Commit

Permalink
test invalid types
Browse files Browse the repository at this point in the history
  • Loading branch information
DjordyKoert committed Jan 17, 2025
1 parent 37a4aa8 commit a3692c8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ModelDescriber/ObjectModelDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function describe(Model $model, OA\Schema $schema)

$type = $this->propertyInfo->getType($class, $propertyName);
if (null === $type) {
throw new \LogicException(sprintf('The PropertyInfo component was not able to guess the type of %s::$%s. You may need to add a `@var` annotation or use `@OA\Property(type="")` to make its type explicit.', $class, $propertyName));
throw new \LogicException(sprintf('The TypeInfo component was not able to guess the type of %s::$%s. You may need to add a `@var` annotation or use `@OA\Property(type="")` to make its type explicit.', $class, $propertyName));

Check warning on line 165 in src/ModelDescriber/ObjectModelDescriber.php

View check run for this annotation

Codecov / codecov/patch

src/ModelDescriber/ObjectModelDescriber.php#L165

Added line #L165 was not covered by tests
}

if ($this->propertyDescriber instanceof ModelRegistryAwareInterface) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class ObjectModelDescriberTest extends WebTestCase
{
private ObjectModelDescriber $modelDescriber;
protected ObjectModelDescriber $modelDescriber;

protected function setUp(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

namespace Nelmio\ApiDocBundle\Tests\Functional\ModelDescriber;

use Nelmio\ApiDocBundle\Model\Model;
use Nelmio\ApiDocBundle\Tests\Functional\TestKernel;
use OpenApi\Annotations as OA;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\PropertyInfo\Type as LegacyType;

final class ObjectModelDescriberTypeInfoTest extends ObjectModelDescriberTest
{
Expand Down Expand Up @@ -73,4 +76,44 @@ public static function provideFixtures(): \Generator
Fixtures\TypeInfo\DateTimeClass::class
];
}

/**
* @dataProvider provideInvalidTypes
*/
public function testInvalidType(object $class, string $expectedType, string $propertyName): void
{
$model = new Model(new LegacyType('object', false, get_class($class)));
$schema = new OA\Schema([
'type' => 'object',
]);

self::expectException(\Exception::class);
self::expectExceptionMessage(sprintf('Type "%s" not supported in %s::%s. You may need to use the `@OA\Property(type="")` annotation to specify it manually.', $expectedType, get_class($class), $propertyName));

$this->modelDescriber->describe($model, $schema);
}

public static function provideInvalidTypes(): \Generator
{
yield 'never' => [
new class {
public function getNever(): never
{
throw new \Exception('This method should never be called');
}
},
'never',
'$never',
];

yield 'void' => [
new class {
public function getVoid(): void
{
}
},
'void',
'$void',
];
}
}

0 comments on commit a3692c8

Please sign in to comment.