Read more properties from swagger property annotation (#1146)

* read default property from swagger property annotation

* read enum from swagger property annotation

* a small fixes
This commit is contained in:
Myroslav 2017-12-17 17:58:41 +02:00 committed by Guilhem N
parent a6ff145195
commit ac7b924129
6 changed files with 69 additions and 16 deletions

View File

@ -80,8 +80,10 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
$type = $item->type['name']; $type = $item->type['name'];
} }
if (in_array($type, ['boolean', 'integer', 'string', 'array'])) { if (in_array($type, ['boolean', 'string', 'array'])) {
$property->setType($type); $property->setType($type);
} elseif (in_array($type, ['int', 'integer'])) {
$property->setType('integer');
} elseif (in_array($type, ['double', 'float'])) { } elseif (in_array($type, ['double', 'float'])) {
$property->setType('number'); $property->setType('number');
$property->setFormat($type); $property->setFormat($type);

View File

@ -15,6 +15,7 @@ use Doctrine\Common\Annotations\Reader;
use EXSyst\Component\Swagger\Items; use EXSyst\Component\Swagger\Items;
use EXSyst\Component\Swagger\Schema; use EXSyst\Component\Swagger\Schema;
use Swagger\Annotations\Property as SwgProperty; use Swagger\Annotations\Property as SwgProperty;
use const Swagger\Annotations\UNDEFINED;
/** /**
* @internal * @internal
@ -36,21 +37,27 @@ class SwaggerPropertyAnnotationReader
{ {
$swgProperty = $this->annotationsReader->getPropertyAnnotation($reflectionProperty, SwgProperty::class); $swgProperty = $this->annotationsReader->getPropertyAnnotation($reflectionProperty, SwgProperty::class);
if ($swgProperty instanceof SwgProperty) { if ($swgProperty instanceof SwgProperty) {
if (null !== $swgProperty->type) { if ($swgProperty->type !== null) {
$property->setType($swgProperty->type); $property->setType($swgProperty->type);
} }
if (null !== $swgProperty->readOnly) { if ($swgProperty->default !== UNDEFINED) {
$property->setReadOnly($swgProperty->readOnly); $property->setDefault($swgProperty->default);
}
if ($swgProperty->enum !== null) {
$property->setEnum($swgProperty->enum);
} }
if ($property instanceof Schema) { if ($property instanceof Schema) {
if (null !== $swgProperty->description) { if ($swgProperty->description !== null) {
$property->setDescription($swgProperty->description); $property->setDescription($swgProperty->description);
} }
if (null !== $swgProperty->title) { if ($swgProperty->title !== null) {
$property->setTitle($swgProperty->title); $property->setTitle($swgProperty->title);
} }
if (null !== $swgProperty->example) { if ($swgProperty->example !== null) {
$property->setExample((string) $swgProperty->example); $property->setExample($swgProperty->example);
}
if ($swgProperty->readOnly !== null) {
$property->setReadOnly($swgProperty->readOnly);
} }
} }
} }

View File

@ -25,10 +25,19 @@ class JMSUser
* @Serializer\Type("integer") * @Serializer\Type("integer")
* @Serializer\Expose * @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; private $id;
/**
* @Serializer\Type("int")
* @Serializer\Expose
* @Serializer\SerializedName("daysOnline")
*
* @SWG\Property(default = 0)
*/
private $daysOnline;
/** /**
* @Serializer\Type("string") * @Serializer\Type("string")
* @Serializer\Expose * @Serializer\Expose
@ -41,7 +50,8 @@ class JMSUser
* @Serializer\Type("array<string>") * @Serializer\Type("array<string>")
* @Serializer\Accessor(getter="getRoles", setter="setRoles") * @Serializer\Accessor(getter="getRoles", setter="setRoles")
* @Serializer\Expose * @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; private $roles;
@ -78,6 +88,14 @@ class JMSUser
*/ */
private $bestFriend; private $bestFriend;
/**
* @Serializer\Type("string")
* @Serializer\Expose
*
* @SWG\Property(enum = {"disabled", "enabled"})
*/
private $status;
public function setRoles($roles) public function setRoles($roles)
{ {
} }

View File

@ -21,7 +21,7 @@ class User
/** /**
* @var int * @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; private $id;
@ -37,11 +37,10 @@ class User
* *
* @SWG\Property( * @SWG\Property(
* description = "User roles", * description = "User roles",
* type = "array",
* items=@SWG\Items(type="string"),
* required = true, * required = true,
* title = "roles", * title = "roles",
* example="[""ADMIN"",""SUPERUSER""]") * example="[""ADMIN"",""SUPERUSER""]",
* default = {"user"},
* ) * )
*/ */
private $roles; private $roles;
@ -55,6 +54,7 @@ class User
/** /**
* @var float * @var float
* @SWG\Property(default = 0.0)
*/ */
private $money; private $money;
@ -68,6 +68,13 @@ class User
*/ */
private $users; private $users;
/**
* @var string
*
* @SWG\Property(enum = {"disabled", "enabled"})
*/
private $status;
/** /**
* @param float $money * @param float $money
*/ */
@ -119,4 +126,8 @@ class User
public function setDummy(Dummy $dummy) public function setDummy(Dummy $dummy)
{ {
} }
public function setStatus(string $status)
{
}
} }

View File

@ -156,6 +156,7 @@ class FunctionalTest extends WebTestCase
'money' => [ 'money' => [
'type' => 'number', 'type' => 'number',
'format' => 'float', 'format' => 'float',
'default' => 0.0
], ],
'id' => [ 'id' => [
'type' => 'integer', 'type' => 'integer',
@ -169,11 +170,12 @@ class FunctionalTest extends WebTestCase
'readOnly' => false, 'readOnly' => false,
], ],
'roles' => [ 'roles' => [
'title' => 'roles',
'type' => 'array', 'type' => 'array',
'description' => 'User roles', 'description' => 'User roles',
'title' => 'roles',
'example' => '["ADMIN","SUPERUSER"]', 'example' => '["ADMIN","SUPERUSER"]',
'items' => ['type' => 'string'], 'items' => ['type' => 'string'],
'default' => ['user'],
], ],
'friendsNumber' => [ 'friendsNumber' => [
'type' => 'string', 'type' => 'string',
@ -191,6 +193,10 @@ class FunctionalTest extends WebTestCase
'dummy' => [ 'dummy' => [
'$ref' => '#/definitions/Dummy2', '$ref' => '#/definitions/Dummy2',
], ],
'status' => [
'type' => 'string',
'enum' => ["disabled", "enabled"],
],
], ],
], ],
$this->getModel('User')->toArray() $this->getModel('User')->toArray()

View File

@ -25,16 +25,21 @@ class JMSFunctionalTest extends WebTestCase
'title' => 'userid', 'title' => 'userid',
'example' => 1, 'example' => 1,
], ],
'daysOnline' => [
'type' => 'integer',
'default' => 0,
],
'email' => [ 'email' => [
'type' => 'string', 'type' => 'string',
'readOnly' => false, 'readOnly' => false,
], ],
'roles' => [ 'roles' => [
'type' => 'array', 'type' => 'array',
'description' => 'User roles',
'title' => 'roles', 'title' => 'roles',
'example' => '["ADMIN","SUPERUSER"]', 'example' => '["ADMIN","SUPERUSER"]',
'items' => ['type' => 'string'], 'items' => ['type' => 'string'],
'default' => ['user'],
'description' => 'Roles list',
], ],
'friendsNumber' => [ 'friendsNumber' => [
'type' => 'string', 'type' => 'string',
@ -48,6 +53,10 @@ class JMSFunctionalTest extends WebTestCase
'best_friend' => [ 'best_friend' => [
'$ref' => '#/definitions/User', '$ref' => '#/definitions/User',
], ],
'status' => [
'type' => 'string',
'enum' => ["disabled", "enabled"],
],
], ],
], $this->getModel('JMSUser')->toArray()); ], $this->getModel('JMSUser')->toArray());
} }