Add annotations support at methods level when using the JMS serializer (#1682)

This commit is contained in:
Guilhem Niot 2020-07-18 13:31:41 +02:00 committed by GitHub
parent 8948d5418b
commit 523d28e955
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 19 deletions

View File

@ -85,12 +85,19 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
$context->pushPropertyMetadata($item); $context->pushPropertyMetadata($item);
$name = true === $isJmsV1 ? $this->namingStrategy->translateName($item) : $item->serializedName; $name = true === $isJmsV1 ? $this->namingStrategy->translateName($item) : $item->serializedName;
// read property options from Swagger Property annotation if it exists
try { $reflections = [];
if (true === $isJmsV1 && property_exists($item, 'reflection') && null !== $item->reflection) { if (true === $isJmsV1 && property_exists($item, 'reflection') && null !== $item->reflection) {
$reflection = $item->reflection; $reflections[] = $item->reflection;
} else { } elseif (\property_exists($item->class, $item->name)) {
$reflection = new \ReflectionProperty($item->class, $item->name); $reflections[] = new \ReflectionProperty($item->class, $item->name);
}
if (null !== $item->getter) {
$reflections[] = new \ReflectionMethod($item->class, $item->getter);
}
if (null !== $item->setter) {
$reflections[] = new \ReflectionMethod($item->class, $item->setter);
} }
$groups = $this->computeGroups($context, $item->type); $groups = $this->computeGroups($context, $item->type);
@ -106,10 +113,14 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
continue; continue;
} }
$property = $properties->get($annotationsReader->getPropertyName($reflection, $name)); foreach ($reflections as $reflection) {
$annotationsReader->updateProperty($reflection, $property, $groups); $name = $annotationsReader->getPropertyName($reflection, $name);
} catch (\ReflectionException $e) { }
$property = $properties->get($name); $property = $properties->get($name);
foreach ($reflections as $reflection) {
$annotationsReader->updateProperty($reflection, $property, $groups);
} }
if (null !== $property->getType() || null !== $property->getRef()) { if (null !== $property->getType() || null !== $property->getRef()) {

View File

@ -45,4 +45,14 @@ class JMSComplex
* @Serializer\Groups({"list"}) * @Serializer\Groups({"list"})
*/ */
private $name; private $name;
/**
* @Serializer\VirtualProperty
* @Serializer\Expose
* @Serializer\Groups({"list"})
* @SWG\Property(ref=@Model(type=JMSUser::class))
*/
public function getVirtualFriend()
{
}
} }

View File

@ -260,6 +260,7 @@ class JMSFunctionalTest extends WebTestCase
'user' => ['$ref' => '#/definitions/JMSUser'], 'user' => ['$ref' => '#/definitions/JMSUser'],
'name' => ['type' => 'string'], 'name' => ['type' => 'string'],
'virtual' => ['$ref' => '#/definitions/JMSUser'], 'virtual' => ['$ref' => '#/definitions/JMSUser'],
'virtual_friend' => ['$ref' => '#/definitions/JMSUser'],
], ],
'required' => [ 'required' => [
'id', 'id',