Skip to content

Commit

Permalink
add basic jms enum support
Browse files Browse the repository at this point in the history
array<enum<'Foo'>> it is not supported yet
  • Loading branch information
goetas committed Mar 19, 2024
1 parent 4c15ee5 commit 5a5485d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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\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 @@ -51,6 +52,7 @@ public static function getDefaultHandlers(): array
new DynamicObjectHandler(),
new ArrayHandler(),
new GenericTemplateHandler(),
new JmsEnumHander(),
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Draw\Component\OpenApi\Extraction\Extractor\JmsSerializer\TypeHandler;

use Draw\Component\OpenApi\Extraction\ExtractionContextInterface;
use Draw\Component\OpenApi\Schema\Schema;
use JMS\Serializer\Metadata\PropertyMetadata;

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

$prop = new Schema();
$prop->type = $type->getBackingType() instanceof \ReflectionNamedType
&& $type->getBackingType()->getName() === 'int' ? '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
{
if (!isset($item->type['name'])
|| $item->type['name'] !== 'enum'
|| !isset($item->type['params'][0])
|| !enum_exists($item->type['params'][0])
) {
return null;
}
return new \ReflectionEnum($item->type['params'][0]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,30 @@ class JmsExtractorStubModel
#[Serializer\ReadOnlyProperty]
public $generic;

/**
* The simple enum
* @var JmsExtractorStubEnum
*/
#[Serializer\Type('enum<\''.JmsExtractorStubEnum::class.'\'>')]
#[Serializer\Groups(['test'])]
public $genericEnum;

/**
* The backed enum
* @var JmsExtractorStubEnum
*/
#[Serializer\Type('enum<\''.JmsExtractorStubBackedEnum::class.'\'>')]
#[Serializer\Groups(['test'])]
public $backedEnum;

/**
* The backed enum int
* @var JmsExtractorStubEnum
*/
#[Serializer\Type('enum<\''.JmsExtractorStubBackedEnumInt::class.'\'>')]
#[Serializer\Groups(['test'])]
public $backedEnumInt;

/**
* Serialized property.
*
Expand Down Expand Up @@ -177,3 +201,17 @@ class JmsExtractorStubGeneric
#[Serializer\ReadOnlyProperty]
public $name;
}

enum JmsExtractorStubEnum{
case FOO;
case BAR;
}
enum JmsExtractorStubBackedEnum: string{
case ABC = 'abc';
case DEF = 'def';
}

enum JmsExtractorStubBackedEnumInt: int{
case ABC = 1;
case DEF = 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@
},
"readOnly": true
},
"backed_enum_int": {
"description": "The backed enum int",
"enum": [
1,
2
],
"type": "integer"
},
"backed_enum": {
"description": "The backed enum",
"enum": [
"abc",
"def"
],
"type": "string"
},
"generic_enum": {
"description": "The simple enum",
"enum": [
"FOO",
"BAR"
],
"type": "string"
},
"serializeProperty": {
"description": "Serialized property.",
"type": "string"
Expand All @@ -38,4 +62,4 @@
}
}
}
}
}

0 comments on commit 5a5485d

Please sign in to comment.