From ac7b924129e57636ed533e2ef2075175b4e33dfd Mon Sep 17 00:00:00 2001 From: Myroslav Date: Sun, 17 Dec 2017 17:58:41 +0200 Subject: [PATCH] Read more properties from swagger property annotation (#1146) * read default property from swagger property annotation * read enum from swagger property annotation * a small fixes --- ModelDescriber/JMSModelDescriber.php | 4 +++- .../SwaggerPropertyAnnotationReader.php | 21 ++++++++++++------ Tests/Functional/Entity/JMSUser.php | 22 +++++++++++++++++-- Tests/Functional/Entity/User.php | 19 ++++++++++++---- Tests/Functional/FunctionalTest.php | 8 ++++++- Tests/Functional/JMSFunctionalTest.php | 11 +++++++++- 6 files changed, 69 insertions(+), 16 deletions(-) diff --git a/ModelDescriber/JMSModelDescriber.php b/ModelDescriber/JMSModelDescriber.php index 25e58d1..6e81f13 100644 --- a/ModelDescriber/JMSModelDescriber.php +++ b/ModelDescriber/JMSModelDescriber.php @@ -80,8 +80,10 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn $type = $item->type['name']; } - if (in_array($type, ['boolean', 'integer', 'string', 'array'])) { + if (in_array($type, ['boolean', 'string', 'array'])) { $property->setType($type); + } elseif (in_array($type, ['int', 'integer'])) { + $property->setType('integer'); } elseif (in_array($type, ['double', 'float'])) { $property->setType('number'); $property->setFormat($type); diff --git a/ModelDescriber/SwaggerPropertyAnnotationReader.php b/ModelDescriber/SwaggerPropertyAnnotationReader.php index 7572bc2..6d19853 100644 --- a/ModelDescriber/SwaggerPropertyAnnotationReader.php +++ b/ModelDescriber/SwaggerPropertyAnnotationReader.php @@ -15,6 +15,7 @@ use Doctrine\Common\Annotations\Reader; use EXSyst\Component\Swagger\Items; use EXSyst\Component\Swagger\Schema; use Swagger\Annotations\Property as SwgProperty; +use const Swagger\Annotations\UNDEFINED; /** * @internal @@ -36,21 +37,27 @@ class SwaggerPropertyAnnotationReader { $swgProperty = $this->annotationsReader->getPropertyAnnotation($reflectionProperty, SwgProperty::class); if ($swgProperty instanceof SwgProperty) { - if (null !== $swgProperty->type) { + if ($swgProperty->type !== null) { $property->setType($swgProperty->type); } - if (null !== $swgProperty->readOnly) { - $property->setReadOnly($swgProperty->readOnly); + if ($swgProperty->default !== UNDEFINED) { + $property->setDefault($swgProperty->default); + } + if ($swgProperty->enum !== null) { + $property->setEnum($swgProperty->enum); } if ($property instanceof Schema) { - if (null !== $swgProperty->description) { + if ($swgProperty->description !== null) { $property->setDescription($swgProperty->description); } - if (null !== $swgProperty->title) { + if ($swgProperty->title !== null) { $property->setTitle($swgProperty->title); } - if (null !== $swgProperty->example) { - $property->setExample((string) $swgProperty->example); + if ($swgProperty->example !== null) { + $property->setExample($swgProperty->example); + } + if ($swgProperty->readOnly !== null) { + $property->setReadOnly($swgProperty->readOnly); } } } diff --git a/Tests/Functional/Entity/JMSUser.php b/Tests/Functional/Entity/JMSUser.php index 6ad5199..acf96cb 100644 --- a/Tests/Functional/Entity/JMSUser.php +++ b/Tests/Functional/Entity/JMSUser.php @@ -25,10 +25,19 @@ class JMSUser * @Serializer\Type("integer") * @Serializer\Expose * - * @SWG\Property(description = "User id", required = true, readOnly = true, title = "userid", example=1) + * @SWG\Property(description = "User id", required = true, readOnly = true, title = "userid", example=1, default = null) */ private $id; + /** + * @Serializer\Type("int") + * @Serializer\Expose + * @Serializer\SerializedName("daysOnline") + * + * @SWG\Property(default = 0) + */ + private $daysOnline; + /** * @Serializer\Type("string") * @Serializer\Expose @@ -41,7 +50,8 @@ class JMSUser * @Serializer\Type("array") * @Serializer\Accessor(getter="getRoles", setter="setRoles") * @Serializer\Expose - * @SWG\Property(description = "User roles", required = true, title = "roles", example="[""ADMIN"",""SUPERUSER""]") + * + * @SWG\Property(default = {"user"}, description = "Roles list", example="[""ADMIN"",""SUPERUSER""]", title="roles") */ private $roles; @@ -78,6 +88,14 @@ class JMSUser */ private $bestFriend; + /** + * @Serializer\Type("string") + * @Serializer\Expose + * + * @SWG\Property(enum = {"disabled", "enabled"}) + */ + private $status; + public function setRoles($roles) { } diff --git a/Tests/Functional/Entity/User.php b/Tests/Functional/Entity/User.php index ab178ab..750b770 100644 --- a/Tests/Functional/Entity/User.php +++ b/Tests/Functional/Entity/User.php @@ -21,7 +21,7 @@ class User /** * @var int * - * @SWG\Property(description = "User id", required = true, readOnly = true, title = "userid", example=1) + * @SWG\Property(description = "User id", required = true, readOnly = true, title = "userid", example=1, default = null) */ private $id; @@ -37,11 +37,10 @@ class User * * @SWG\Property( * description = "User roles", - * type = "array", - * items=@SWG\Items(type="string"), * required = true, * title = "roles", - * example="[""ADMIN"",""SUPERUSER""]") + * example="[""ADMIN"",""SUPERUSER""]", + * default = {"user"}, * ) */ private $roles; @@ -55,6 +54,7 @@ class User /** * @var float + * @SWG\Property(default = 0.0) */ private $money; @@ -68,6 +68,13 @@ class User */ private $users; + /** + * @var string + * + * @SWG\Property(enum = {"disabled", "enabled"}) + */ + private $status; + /** * @param float $money */ @@ -119,4 +126,8 @@ class User public function setDummy(Dummy $dummy) { } + + public function setStatus(string $status) + { + } } diff --git a/Tests/Functional/FunctionalTest.php b/Tests/Functional/FunctionalTest.php index a804604..3a8d735 100644 --- a/Tests/Functional/FunctionalTest.php +++ b/Tests/Functional/FunctionalTest.php @@ -156,6 +156,7 @@ class FunctionalTest extends WebTestCase 'money' => [ 'type' => 'number', 'format' => 'float', + 'default' => 0.0 ], 'id' => [ 'type' => 'integer', @@ -169,11 +170,12 @@ class FunctionalTest extends WebTestCase 'readOnly' => false, ], 'roles' => [ + 'title' => 'roles', 'type' => 'array', 'description' => 'User roles', - 'title' => 'roles', 'example' => '["ADMIN","SUPERUSER"]', 'items' => ['type' => 'string'], + 'default' => ['user'], ], 'friendsNumber' => [ 'type' => 'string', @@ -191,6 +193,10 @@ class FunctionalTest extends WebTestCase 'dummy' => [ '$ref' => '#/definitions/Dummy2', ], + 'status' => [ + 'type' => 'string', + 'enum' => ["disabled", "enabled"], + ], ], ], $this->getModel('User')->toArray() diff --git a/Tests/Functional/JMSFunctionalTest.php b/Tests/Functional/JMSFunctionalTest.php index 922ae53..4fec31b 100644 --- a/Tests/Functional/JMSFunctionalTest.php +++ b/Tests/Functional/JMSFunctionalTest.php @@ -25,16 +25,21 @@ class JMSFunctionalTest extends WebTestCase 'title' => 'userid', 'example' => 1, ], + 'daysOnline' => [ + 'type' => 'integer', + 'default' => 0, + ], 'email' => [ 'type' => 'string', 'readOnly' => false, ], 'roles' => [ 'type' => 'array', - 'description' => 'User roles', 'title' => 'roles', 'example' => '["ADMIN","SUPERUSER"]', 'items' => ['type' => 'string'], + 'default' => ['user'], + 'description' => 'Roles list', ], 'friendsNumber' => [ 'type' => 'string', @@ -48,6 +53,10 @@ class JMSFunctionalTest extends WebTestCase 'best_friend' => [ '$ref' => '#/definitions/User', ], + 'status' => [ + 'type' => 'string', + 'enum' => ["disabled", "enabled"], + ], ], ], $this->getModel('JMSUser')->toArray()); }