Skip to content

Commit

Permalink
Add jms/serializer enum support (#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
javer committed Jan 5, 2024
1 parent 23d157c commit 1f99898
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ModelDescriber/JMSModelDescriber.php
Expand Up @@ -285,6 +285,15 @@ public function describeItem(array $type, OA\Schema $property, Context $context)
$property->type = 'string';
$property->format = 'date-time';
} else {
// See https://github.com/schmittjoh/serializer/blob/5a5a03a/src/Metadata/Driver/EnumPropertiesDriver.php#L51
if ('enum' === $type['name']
&& isset($type['params'][0])
&& function_exists('enum_exists')
&& enum_exists($type['params'][0])
) {
$type = ['name' => $type['params'][0]];
}

$groups = $this->computeGroups($context, $type);

$model = new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, $type['name']), $groups);
Expand Down
11 changes: 11 additions & 0 deletions Tests/Functional/Controller/JMSController81.php
Expand Up @@ -12,6 +12,7 @@
namespace Nelmio\ApiDocBundle\Tests\Functional\Controller;

use Nelmio\ApiDocBundle\Annotation\Model;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\Article81;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSComplex81;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSDualComplex;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSNamingStrategyConstraints;
Expand Down Expand Up @@ -119,4 +120,14 @@ public function minUserAction()
public function minUserNestedAction()
{
}

#[Route('/api/jms_enum', methods: ['GET'])]
#[OA\Response(
response: 200,
description: 'Success',
content: new Model(type: Article81::class))
]
public function enum()
{
}
}
34 changes: 34 additions & 0 deletions Tests/Functional/JMSFunctionalTest.php
Expand Up @@ -335,6 +335,40 @@ public function testNamingStrategyWithConstraints()
], json_decode($this->getModel('JMSNamingStrategyConstraints')->toJson(), true));
}

/**
* @requires PHP >= 8.1
*/
public function testEnumSupport()
{
$this->assertEquals([
'type' => 'object',
'properties' => [
'id' => [
'type' => 'integer',
],
'type' => [
'$ref' => '#/components/schemas/ArticleType81',
],
'int_backed_type' => [
'$ref' => '#/components/schemas/ArticleType81IntBacked',
],
'not_backed_type' => [
'$ref' => '#/components/schemas/ArticleType81NotBacked',
],
],
'schema' => 'Article81',
], json_decode($this->getModel('Article81')->toJson(), true));

$this->assertEquals([
'schema' => 'ArticleType81',
'type' => 'string',
'enum' => [
'draft',
'final',
],
], json_decode($this->getModel('ArticleType81')->toJson(), true));
}

protected static function createKernel(array $options = []): KernelInterface
{
return new TestKernel(TestKernel::USE_JMS);
Expand Down
6 changes: 6 additions & 0 deletions Tests/Functional/TestKernel.php
Expand Up @@ -348,6 +348,12 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
],
]);

if ($this->flags & self::USE_JMS && \PHP_VERSION_ID >= 80100) {
$c->loadFromExtension('jms_serializer', [
'enum_support' => true,
]);
}

$def = new Definition(VirtualTypeClassDoesNotExistsHandlerDefinedDescriber::class);
$def->addTag('nelmio_api_doc.model_describer');
$c->setDefinition('nelmio.test.jms.virtual_type.describer', $def);
Expand Down
15 changes: 15 additions & 0 deletions phpunit-baseline.json
Expand Up @@ -6799,6 +6799,21 @@
"message": "Since sensio/framework-extra-bundle 5.2: The \"sensio_framework_extra.routing.loader.annot_file\" service is deprecated since version 5.2",
"count": 1
},
{
"location": "Nelmio\\ApiDocBundle\\Tests\\Functional\\JMSFunctionalTest::testEnumSupport",
"message": "Since sensio/framework-extra-bundle 5.2: The \"sensio_framework_extra.routing.loader.annot_class\" service is deprecated since version 5.2",
"count": 1
},
{
"location": "Nelmio\\ApiDocBundle\\Tests\\Functional\\JMSFunctionalTest::testEnumSupport",
"message": "Since sensio/framework-extra-bundle 5.2: The \"sensio_framework_extra.routing.loader.annot_dir\" service is deprecated since version 5.2",
"count": 1
},
{
"location": "Nelmio\\ApiDocBundle\\Tests\\Functional\\JMSFunctionalTest::testEnumSupport",
"message": "Since sensio/framework-extra-bundle 5.2: The \"sensio_framework_extra.routing.loader.annot_file\" service is deprecated since version 5.2",
"count": 1
},
{
"location": "Nelmio\\ApiDocBundle\\Tests\\Functional\\SwaggerPHPApiComplianceTest::testAllContextsCopyRoot",
"message": "Since sensio/framework-extra-bundle 5.2: The \"sensio_framework_extra.routing.loader.annot_class\" service is deprecated since version 5.2",
Expand Down

0 comments on commit 1f99898

Please sign in to comment.