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,31 +85,42 @@ 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);
if (true === $item->inline && isset($item->type['name'])) {
// currently array types can not be documented :-/
if (!in_array($item->type['name'], ['array', 'ArrayCollection'], true)) {
$inlineModel = new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, $item->type['name']), $groups);
$this->describe($inlineModel, $schema);
} }
$context->popPropertyMetadata();
$groups = $this->computeGroups($context, $item->type); continue;
}
if (true === $item->inline && isset($item->type['name'])) { foreach ($reflections as $reflection) {
// currently array types can not be documented :-/ $name = $annotationsReader->getPropertyName($reflection, $name);
if (!in_array($item->type['name'], ['array', 'ArrayCollection'], true)) { }
$inlineModel = new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, $item->type['name']), $groups);
$this->describe($inlineModel, $schema);
}
$context->popPropertyMetadata();
continue; $property = $properties->get($name);
}
$property = $properties->get($annotationsReader->getPropertyName($reflection, $name)); foreach ($reflections as $reflection) {
$annotationsReader->updateProperty($reflection, $property, $groups); $annotationsReader->updateProperty($reflection, $property, $groups);
} catch (\ReflectionException $e) {
$property = $properties->get($name);
} }
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',