Skip to content

Commit

Permalink
fix: custom JMS enum type handling (#2372)
Browse files Browse the repository at this point in the history
| Q | A |

|---------------|---------------------------------------------------------------------------------------------------------------------------|
| Bug fix? | yes |
| New feature? | no <!-- please update src/**/CHANGELOG.md files --> |
| Deprecations? | no <!-- please update UPGRADE-*.md and
src/**/CHANGELOG.md files --> |
| Issues | Fix #2327 <!-- prefix each issue number with "Fix #", no need
to create an issue if none exists, explain below instead --> |

JMS implemented the enum type annotations inconsistently compared to the
other type annotations. I've created
schmittjoh/serializer#1561 to fix that (which
has been merged and released with 3.31.0), and this PR adds
compatibility for both methods.

---------

Co-authored-by: Djordy Koert <[email protected]>
  • Loading branch information
bobvandevijver and DjordyKoert authored Oct 31, 2024
1 parent 3a58743 commit 1e283ef
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/ModelDescriber/JMSModelDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,14 @@ public function describeItem(array $type, OA\Schema $property, Context $context,
if ('enum' === $type['name']
&& isset($type['params'][0])
&& function_exists('enum_exists')
&& enum_exists($type['params'][0])
) {
$type = ['name' => $type['params'][0]];
$typeParam = $type['params'][0];
if (isset($typeParam['name'])) {
$typeParam = $typeParam['name'];
}
if (is_string($typeParam) && enum_exists($typeParam)) {
$type['name'] = $typeParam;
}
}

$groups = $this->computeGroups($context, $type);
Expand Down
11 changes: 11 additions & 0 deletions tests/Functional/Controller/JMSController81.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Nelmio\ApiDocBundle\Tests\Functional\Entity\DiscriminatorMap\JMSAbstractUser;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSComplex81;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSDualComplex;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSEnum81;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSNamingStrategyConstraints;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSUser;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChat;
Expand Down Expand Up @@ -141,4 +142,14 @@ public function enum()
public function discriminatorMapAction()
{
}

#[Route('/api/jms_enum_array', methods: ['GET'])]
#[OA\Response(
response: 200,
description: 'Success',
content: new Model(type: JMSEnum81::class))
]
public function enumArrayAction()
{
}
}
25 changes: 25 additions & 0 deletions tests/Functional/Entity/JMSEnum81.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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 JMS\Serializer\Annotation as Serializer;

class JMSEnum81
{
#[Serializer\Type('enum<'.ArticleType81::class.", 'value'>")]
#[Serializer\Expose]
public $enumValue;

#[Serializer\Type('array<enum<'.ArticleType81::class.", 'value'>>")]
#[Serializer\Expose]
public $enumValues;
}
30 changes: 30 additions & 0 deletions tests/Functional/JMSFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,36 @@ public function testEnumSupport(): void
'final',
],
], json_decode($this->getModel('ArticleType81')->toJson(), true));

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

if (TestKernel::isAnnotationsAvailable()) {
// Further tests have only been defined with attributes
return;
}

self::assertEquals([
'schema' => 'JMSEnum81',
'type' => 'object',
'properties' => [
'enum_value' => [
'$ref' => '#/components/schemas/ArticleType81'
],
'enum_values' => [
'type' => 'array',
'items' => [
'$ref' => '#/components/schemas/ArticleType81'
]
],
]
], json_decode($this->getModel('JMSEnum81')->toJson(), true));
}

public function testModeDiscriminatorMap(): void
Expand Down

0 comments on commit 1e283ef

Please sign in to comment.