Merge pull request #1236 from nelmio/properties

Allow to change a property name
This commit is contained in:
Guilhem N 2018-02-20 09:56:53 +01:00 committed by GitHub
commit d4b0382fa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 27 additions and 10 deletions

View File

@ -40,6 +40,11 @@ class AnnotationsReader
$this->symfonyConstraintAnnotationReader->setSchema($schema); $this->symfonyConstraintAnnotationReader->setSchema($schema);
} }
public function getPropertyName(\ReflectionProperty $reflectionProperty, string $default): string
{
return $this->swgAnnotationsReader->getPropertyName($reflectionProperty, $default);
}
public function updateProperty(\ReflectionProperty $reflectionProperty, Schema $property) public function updateProperty(\ReflectionProperty $reflectionProperty, Schema $property)
{ {
$this->phpDocReader->updateProperty($reflectionProperty, $property); $this->phpDocReader->updateProperty($reflectionProperty, $property);

View File

@ -40,6 +40,16 @@ class SwgAnnotationsReader
} }
} }
public function getPropertyName(\ReflectionProperty $reflectionProperty, string $default): string
{
/** @var SwgProperty $swgProperty */
if (!$swgProperty = $this->annotationsReader->getPropertyAnnotation($reflectionProperty, SwgProperty::class)) {
return $default;
}
return $swgProperty->property ?? $default;
}
public function updateProperty(\ReflectionProperty $reflectionProperty, Schema $property) public function updateProperty(\ReflectionProperty $reflectionProperty, Schema $property)
{ {
/** @var SwgProperty $swgProperty */ /** @var SwgProperty $swgProperty */

View File

@ -69,11 +69,13 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
} }
$name = $this->namingStrategy->translateName($item); $name = $this->namingStrategy->translateName($item);
$property = $properties->get($name);
// read property options from Swagger Property annotation if it exists // read property options from Swagger Property annotation if it exists
if (null !== $item->reflection) { if (null !== $item->reflection) {
$property = $properties->get($this->annotationsReader->getPropertyName($item->reflection, $name));
$this->annotationsReader->updateProperty($item->reflection, $property); $this->annotationsReader->updateProperty($item->reflection, $property);
} else {
$property = $properties->get($name);
} }
if (null !== $property->getType()) { if (null !== $property->getType()) {

View File

@ -55,14 +55,13 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar
} }
foreach ($propertyInfoProperties as $propertyName) { foreach ($propertyInfoProperties as $propertyName) {
$property = $properties->get($propertyName);
// read property options from Swagger Property annotation if it exists // read property options from Swagger Property annotation if it exists
if (property_exists($class, $propertyName)) { if (property_exists($class, $propertyName)) {
$this->annotationsReader->updateProperty( $reflectionProperty = new \ReflectionProperty($class, $propertyName);
new \ReflectionProperty($class, $propertyName), $property = $properties->get($this->annotationsReader->getPropertyName($reflectionProperty, $propertyName));
$property $this->annotationsReader->updateProperty($reflectionProperty, $property);
); } else {
$property = $properties->get($propertyName);
} }
// If type manually defined // If type manually defined

View File

@ -62,7 +62,7 @@ class JMSUser
private $password; private $password;
/** /**
* @SWG\Property(type="date") * @SWG\Property(property="last_update", type="date")
* @Serializer\Expose * @Serializer\Expose
*/ */
private $updatedAt; private $updatedAt;

View File

@ -57,6 +57,7 @@ class User
/** /**
* @var \DateTime * @var \DateTime
* @SWG\Property(property="creationDate")
*/ */
private $createdAt; private $createdAt;

View File

@ -179,7 +179,7 @@ class FunctionalTest extends WebTestCase
'friendsNumber' => [ 'friendsNumber' => [
'type' => 'string', 'type' => 'string',
], ],
'createdAt' => [ 'creationDate' => [
'type' => 'string', 'type' => 'string',
'format' => 'date-time', 'format' => 'date-time',
], ],

View File

@ -63,7 +63,7 @@ class JMSFunctionalTest extends WebTestCase
'description' => 'Only enabled users may be used in actions.', 'description' => 'Only enabled users may be used in actions.',
'enum' => ['disabled', 'enabled'], 'enum' => ['disabled', 'enabled'],
], ],
'updated_at' => [ 'last_update' => [
'type' => 'date', 'type' => 'date',
], ],
], ],