Merge pull request #1145 from discordier/hotfix/override-values-correctly

[3.0] Pass real property to swagger annotation reader
This commit is contained in:
Guilhem N 2017-12-17 09:54:02 +01:00 committed by GitHub
commit 785a09fd49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 47 additions and 12 deletions

View File

@ -72,7 +72,7 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
} }
$name = $this->namingStrategy->translateName($item); $name = $this->namingStrategy->translateName($item);
$property = $properties->get($name); $realProp = $property = $properties->get($name);
if ($type = $this->getNestedTypeInArray($item)) { if ($type = $this->getNestedTypeInArray($item)) {
$property->setType('array'); $property->setType('array');
@ -102,7 +102,7 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
// read property options from Swagger Property annotation if it exists // read property options from Swagger Property annotation if it exists
if($item->reflection !== null) if($item->reflection !== null)
$this->swaggerPropertyAnnotationReader->updateWithSwaggerPropertyAnnotation($item->reflection, $property); $this->swaggerPropertyAnnotationReader->updateWithSwaggerPropertyAnnotation($item->reflection, $realProp);
} }
} }

View File

@ -61,7 +61,7 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar
} }
$type = $types[0]; $type = $types[0];
$property = $properties->get($propertyName); $realProp = $property = $properties->get($propertyName);
if (Type::BUILTIN_TYPE_ARRAY === $type->getBuiltinType()) { if (Type::BUILTIN_TYPE_ARRAY === $type->getBuiltinType()) {
$type = $type->getCollectionValueType(); $type = $type->getCollectionValueType();
@ -95,7 +95,7 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar
if (property_exists($class, $propertyName)) { if (property_exists($class, $propertyName)) {
$this->swaggerPropertyAnnotationReader->updateWithSwaggerPropertyAnnotation( $this->swaggerPropertyAnnotationReader->updateWithSwaggerPropertyAnnotation(
new \ReflectionProperty($class, $propertyName), new \ReflectionProperty($class, $propertyName),
$property $realProp
); );
} }
} }

View File

@ -36,20 +36,22 @@ 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 ($swgProperty->description !== null) {
$property->setDescription($swgProperty->description);
}
if ($swgProperty->type !== null) { if ($swgProperty->type !== null) {
$property->setType($swgProperty->type); $property->setType($swgProperty->type);
} }
if ($swgProperty->readOnly !== null) { if ($swgProperty->readOnly !== null) {
$property->setReadOnly($swgProperty->readOnly); $property->setReadOnly($swgProperty->readOnly);
} }
if ($swgProperty->title !== null) { if ($property instanceof Schema) {
$property->setTitle($swgProperty->title); if ($swgProperty->description !== null) {
} $property->setDescription($swgProperty->description);
if ($swgProperty->example !== null) { }
$property->setExample((string) $swgProperty->example); if ($swgProperty->title !== null) {
$property->setTitle($swgProperty->title);
}
if ($swgProperty->example !== null) {
$property->setExample((string) $swgProperty->example);
}
} }
} }
} }

View File

@ -41,6 +41,7 @@ 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""]")
*/ */
private $roles; private $roles;

View File

@ -32,6 +32,20 @@ class User
*/ */
private $email; private $email;
/**
* @var string[]
*
* @SWG\Property(
* description = "User roles",
* type = "array",
* items=@SWG\Items(type="string"),
* required = true,
* title = "roles",
* example="[""ADMIN"",""SUPERUSER""]")
* )
*/
private $roles;
/** /**
* @var int * @var int
* *
@ -78,6 +92,14 @@ class User
$this->email = $email; $this->email = $email;
} }
/**
* @param string[] $roles
*/
public function setRoles(array $roles)
{
$this->roles = $roles;
}
/** /**
* @param int $friendsNumber * @param int $friendsNumber
*/ */

View File

@ -168,6 +168,13 @@ class FunctionalTest extends WebTestCase
'type' => 'string', 'type' => 'string',
'readOnly' => false, 'readOnly' => false,
], ],
'roles' => [
'type' => 'array',
'description' => 'User roles',
'title' => 'roles',
'example' => '["ADMIN","SUPERUSER"]',
'items' => ['type' => 'string'],
],
'friendsNumber' => [ 'friendsNumber' => [
'type' => 'string', 'type' => 'string',
], ],

View File

@ -31,6 +31,9 @@ class JMSFunctionalTest extends WebTestCase
], ],
'roles' => [ 'roles' => [
'type' => 'array', 'type' => 'array',
'description' => 'User roles',
'title' => "roles",
'example' => '["ADMIN","SUPERUSER"]',
'items' => ['type' => 'string'], 'items' => ['type' => 'string'],
], ],
'friendsNumber' => [ 'friendsNumber' => [