Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add basic jms enum support #240

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,39 @@
<?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
&& 'int' === $type->getBackingType()->getName() ? 'integer' : 'string';
$prop->enum = array_map(static fn (\ReflectionEnumBackedCase|\ReflectionEnumUnitCase $value) => $value instanceof \ReflectionEnumBackedCase ? $value->getBackingValue() : $value->getName(), $type->getCases());

return $prop;
}

private function getEnumFqcn(PropertyMetadata $item): ?\ReflectionEnum
{
if (!isset($item->type['name'])
|| 'enum' !== $item->type['name']
|| !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,20 @@ 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 @@
}
}
}
}
}
Loading