From 8b8fab9af0d09d98ce05ca91c9d72389ee072fa0 Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Tue, 19 Mar 2024 17:45:17 +0100 Subject: [PATCH] add basic jms enum support array> it is not supported yet --- .../JmsSerializer/PropertiesExtractor.php | 2 + .../TypeHandler/JmsEnumHander.php | 39 ++++++++++++++++++ .../JmsSerializer/PropertiesExtractorTest.php | 41 +++++++++++++++++++ .../fixture/jmsExtractorTestExtract.json | 26 +++++++++++- 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 packages/open-api/Extraction/Extractor/JmsSerializer/TypeHandler/JmsEnumHander.php diff --git a/packages/open-api/Extraction/Extractor/JmsSerializer/PropertiesExtractor.php b/packages/open-api/Extraction/Extractor/JmsSerializer/PropertiesExtractor.php index 5c082075a..444888b0b 100644 --- a/packages/open-api/Extraction/Extractor/JmsSerializer/PropertiesExtractor.php +++ b/packages/open-api/Extraction/Extractor/JmsSerializer/PropertiesExtractor.php @@ -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; @@ -51,6 +52,7 @@ public static function getDefaultHandlers(): array new DynamicObjectHandler(), new ArrayHandler(), new GenericTemplateHandler(), + new JmsEnumHander(), ]; } diff --git a/packages/open-api/Extraction/Extractor/JmsSerializer/TypeHandler/JmsEnumHander.php b/packages/open-api/Extraction/Extractor/JmsSerializer/TypeHandler/JmsEnumHander.php new file mode 100644 index 000000000..22a2ccc77 --- /dev/null +++ b/packages/open-api/Extraction/Extractor/JmsSerializer/TypeHandler/JmsEnumHander.php @@ -0,0 +1,39 @@ +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]); + } +} diff --git a/packages/open-api/Tests/Extraction/Extractor/JmsSerializer/PropertiesExtractorTest.php b/packages/open-api/Tests/Extraction/Extractor/JmsSerializer/PropertiesExtractorTest.php index 67fa2b2fc..3394d3505 100644 --- a/packages/open-api/Tests/Extraction/Extractor/JmsSerializer/PropertiesExtractorTest.php +++ b/packages/open-api/Tests/Extraction/Extractor/JmsSerializer/PropertiesExtractorTest.php @@ -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. * @@ -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; +} diff --git a/packages/open-api/Tests/Extraction/Extractor/JmsSerializer/fixture/jmsExtractorTestExtract.json b/packages/open-api/Tests/Extraction/Extractor/JmsSerializer/fixture/jmsExtractorTestExtract.json index e0bed7871..317e3a267 100644 --- a/packages/open-api/Tests/Extraction/Extractor/JmsSerializer/fixture/jmsExtractorTestExtract.json +++ b/packages/open-api/Tests/Extraction/Extractor/JmsSerializer/fixture/jmsExtractorTestExtract.json @@ -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" @@ -38,4 +62,4 @@ } } } -} \ No newline at end of file +}