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'];
}
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);

View File

@ -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);
}
}
}

View File

@ -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<string>")
* @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)
{
}

View File

@ -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)
{
}
}

View File

@ -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()

View File

@ -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());
}