Skip to content

Commit

Permalink
fix test and coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Apr 9, 2024
1 parent 382b854 commit 6dfac45
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Draw\Component\OpenApi\Extraction\Extractor\JmsSerializer\TypeHandler\ArrayHandler;
use Draw\Component\OpenApi\Extraction\Extractor\JmsSerializer\TypeHandler\DoctrineObjectReferenceSchemaHandler;
use Draw\Component\OpenApi\Extraction\Extractor\JmsSerializer\TypeHandler\DynamicObjectHandler;
use Draw\Component\OpenApi\Extraction\Extractor\JmsSerializer\TypeHandler\EnumHandler;
use Draw\Component\OpenApi\Extraction\Extractor\JmsSerializer\TypeHandler\GenericTemplateHandler;
use Draw\Component\OpenApi\Extraction\Extractor\OpenApi\HeaderAttributeExtractor;
use Draw\Component\OpenApi\Extraction\Extractor\OpenApi\JsonRootSchemaExtractor;
Expand Down Expand Up @@ -267,6 +268,10 @@ public static function provideTestLoad(): iterable
'draw.open_api.extractor.jms_serializer.type_handler.dynamic_object_handler',
[DynamicObjectHandler::class]
),
new ServiceConfiguration(
'draw.open_api.extractor.jms_serializer.type_handler.enum_handler',
[EnumHandler::class]
),
new ServiceConfiguration(
'draw.open_api.extractor.jms_serializer.type_handler.generic_template_handler',
[GenericTemplateHandler::class]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Draw\Component\OpenApi\Extraction\Extractor\JmsSerializer\Event\PropertyExtractedEvent;
use Draw\Component\OpenApi\Extraction\Extractor\JmsSerializer\TypeHandler\ArrayHandler;
use Draw\Component\OpenApi\Extraction\Extractor\JmsSerializer\TypeHandler\DynamicObjectHandler;
use Draw\Component\OpenApi\Extraction\Extractor\JmsSerializer\TypeHandler\EnumHandler;
use Draw\Component\OpenApi\Extraction\Extractor\JmsSerializer\TypeHandler\GenericTemplateHandler;
use Draw\Component\OpenApi\Extraction\Extractor\JmsSerializer\TypeHandler\JmsEnumHander;
use Draw\Component\OpenApi\Extraction\Extractor\JmsSerializer\TypeHandler\TypeToSchemaHandlerInterface;
use Draw\Component\OpenApi\Extraction\ExtractorInterface;
use Draw\Component\OpenApi\Schema\Schema;
Expand Down Expand Up @@ -52,7 +52,7 @@ public static function getDefaultHandlers(): array
new DynamicObjectHandler(),
new ArrayHandler(),
new GenericTemplateHandler(),
new JmsEnumHander(),
new EnumHandler(),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,36 @@
use Draw\Component\OpenApi\Schema\Schema;
use JMS\Serializer\Metadata\PropertyMetadata;

class JmsEnumHander implements TypeToSchemaHandlerInterface
class EnumHandler implements TypeToSchemaHandlerInterface
{
public function extractSchemaFromType(
PropertyMetadata $propertyMetadata,
ExtractionContextInterface $extractionContext
): ?Schema {
if (null === ($type = $this->getEnumFqcn($propertyMetadata))) {
if (null === ($type = $this->getEnumClassName($propertyMetadata))) {
return null;
}

$backingType = $type->getBackingType();

$prop = new Schema();
$prop->type = $type->getBackingType() instanceof \ReflectionNamedType
&& 'int' === $type->getBackingType()->getName() ? 'integer' : 'string';
$prop->enum = array_map(static fn (\ReflectionEnumBackedCase|\ReflectionEnumUnitCase $value) => $value instanceof \ReflectionEnumBackedCase ? $value->getBackingValue() : $value->getName(), $type->getCases());
$prop->type = $backingType instanceof \ReflectionNamedType && 'int' === $backingType->getName()
? 'integer'
: 'string';

$prop->enum = array_map(
static function (\ReflectionEnumBackedCase|\ReflectionEnumUnitCase $value) {
return $value instanceof \ReflectionEnumBackedCase
? $value->getBackingValue()
: $value->getName();
},
$type->getCases()
);

return $prop;
}

private function getEnumFqcn(PropertyMetadata $item): ?\ReflectionEnum
private function getEnumClassName(PropertyMetadata $item): ?\ReflectionEnum
{
if (!isset($item->type['name'])
|| 'enum' !== $item->type['name']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@
"readOnly": true
},
"backed_enum_int": {
"description": "The backed enum int",
"description": "The backed enum int.",
"enum": [
1,
2
],
"type": "integer"
},
"backed_enum": {
"description": "The backed enum",
"description": "The backed enum.",
"enum": [
"abc",
"def"
],
"type": "string"
},
"generic_enum": {
"description": "The simple enum",
"description": "The simple enum.",
"enum": [
"FOO",
"BAR"
Expand Down

0 comments on commit 6dfac45

Please sign in to comment.