respect jms serializer nested group exclusion

This commit is contained in:
Asmir Mustafic 2018-05-02 16:56:20 +02:00 committed by Guilhem Niot
parent 31ae375ab5
commit bd26696f8f
6 changed files with 241 additions and 4 deletions

View File

@ -33,6 +33,12 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
private $factory;
private $namingStrategy;
private $doctrineReader;
private $previousGroups = [];
/**
* @var array
*/
private $propertyTypeUseGroupsCache = [];
public function __construct(
MetadataFactoryInterface $factory,
@ -70,8 +76,14 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
$name = $this->namingStrategy->translateName($item);
$groups = $model->getGroups();
$previousGroups = null;
if (isset($groups[$name]) && is_array($groups[$name])) {
$previousGroups = $groups;
$groups = $model->getGroups()[$name];
} elseif (!isset($groups[$name]) && !empty($this->previousGroups[spl_object_hash($model)])) {
// $groups = $this->previousGroups[spl_object_hash($model)]; use this for jms/serializer 2.0
$groups = false === $this->propertyTypeUsesGroups($item->type) ? null : [GroupsExclusionStrategy::DEFAULT_GROUP];
} elseif (is_array($groups)) {
$groups = array_filter($groups, 'is_scalar');
}
@ -97,7 +109,7 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
continue;
}
$this->describeItem($item->type, $property, $groups);
$this->describeItem($item->type, $property, $groups, $previousGroups);
}
}
@ -118,9 +130,11 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
return false;
}
private function describeItem(array $type, Schema $property, array $groups = null)
private function describeItem(array $type, $property, array $groups = null, array $previousGroups = null)
{
if (list($nestedType, $isHash) = $this->getNestedTypeInArray($type)) { // @ todo update a bit getNestedTypeInArray and describe ($type = $item->type)
$nestedTypeInfo = $this->getNestedTypeInArray($type);
if (null !== $nestedTypeInfo) {
list($nestedType, $isHash) = $nestedTypeInfo;
if ($isHash) {
$property->setType('object');
// in the case of a virtual property, set it as free object type
@ -131,7 +145,7 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
return;
}
$this->describeItem($nestedType, $property->getAdditionalProperties(), $groups);
$this->describeItem($nestedType, $property->getAdditionalProperties(), $groups, $previousGroups);
return;
}
@ -160,6 +174,10 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
$property->setRef($this->modelRegistry->register(
new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, $type['name']), $groups)
));
if ($previousGroups) {
$this->previousGroups[spl_object_hash($model)] = $previousGroups;
}
}
}
@ -179,4 +197,35 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
return null;
}
/**
* @param array $type
*
* @return bool|null
*/
private function propertyTypeUsesGroups(array $type)
{
if (!array_key_exists($type['name'], $this->propertyTypeUseGroupsCache)) {
return $this->propertyTypeUseGroupsCache[$type['name']];
}
try {
$metadata = $this->factory->getMetadataForClass($type['name']);
foreach ($metadata->propertyMetadata as $item) {
if (null !== $item->groups && $item->groups != [GroupsExclusionStrategy::DEFAULT_GROUP]) {
$this->propertyTypeUseGroupsCache[$type['name']] = true;
return true;
}
}
$this->propertyTypeUseGroupsCache[$type['name']] = false;
return false;
} catch (\ReflectionException $e) {
$this->propertyTypeUseGroupsCache[$type['name']] = null;
return null;
}
}
}

View File

@ -15,6 +15,9 @@ use Nelmio\ApiDocBundle\Annotation\Model;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSComplex;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSDualComplex;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSUser;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChat;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChatUser;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSPicture;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\VirtualProperty;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Swagger\Annotations as SWG;
@ -71,4 +74,40 @@ class JMSController
public function complexDualAction()
{
}
/**
* @Route("/api/jms_chat", methods={"GET"})
* @SWG\Response(
* response=200,
* description="Success",
* @Model(type=JMSChat::class, groups={"Default", "members" : {"mini"}})
* )
*/
public function chatAction()
{
}
/**
* @Route("/api/jms_picture", methods={"GET"})
* @SWG\Response(
* response=200,
* description="Success",
* @Model(type=JMSPicture::class, groups={"mini"})
* )
*/
public function pictureAction()
{
}
/**
* @Route("/api/jms_mini_user", methods={"GET"})
* @SWG\Response(
* response=200,
* description="Success",
* @Model(type=JMSChatUser::class, groups={"mini"})
* )
*/
public function minUserAction()
{
}
}

View File

@ -0,0 +1,34 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup;
use JMS\Serializer\Annotation as Serializer;
/**
* User.
*
* @Serializer\ExclusionPolicy("all")
*/
class JMSChat
{
/**
* @Serializer\Type("integer")
* @Serializer\Expose
*/
private $id;
/**
* @Serializer\Type("array<Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChatUser>")
* @Serializer\Expose
*/
private $members;
}

View File

@ -0,0 +1,41 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup;
use JMS\Serializer\Annotation as Serializer;
/**
* User.
*
* @Serializer\ExclusionPolicy("all")
*/
class JMSChatUser
{
/**
* @Serializer\Type("integer")
* @Serializer\Expose
*/
private $id;
/**
* @Serializer\Type("Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSPicture")
* @Serializer\Groups({"Default", "mini"})
* @Serializer\Expose
*/
private $picture;
/**
* @Serializer\Type("array<Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSPicture>")
* @Serializer\Expose
*/
private $allPictures;
}

View File

@ -0,0 +1,35 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup;
use JMS\Serializer\Annotation as Serializer;
/**
* User.
*
* @Serializer\ExclusionPolicy("all")
*/
class JMSPicture
{
/**
* @Serializer\Type("integer")
* @Serializer\Expose
*/
private $id;
/**
* @Serializer\Type("integer")
* @Serializer\Expose
* @Serializer\Groups({"mini"})
*/
private $onlyDirectPictureMini;
}

View File

@ -13,6 +13,45 @@ namespace Nelmio\ApiDocBundle\Tests\Functional;
class JMSFunctionalTest extends WebTestCase
{
public function testModelPictureDocumentation()
{
$this->assertEquals([
'type' => 'object',
'properties' => [
'only_direct_picture_mini' => [
'type' => 'integer',
],
],
], $this->getModel('JMSPicture')->toArray());
}
public function testModeChatDocumentation()
{
$this->assertEquals([
'type' => 'object',
'properties' => [
'id' => [
'type' => 'integer',
],
'members' => [
'items' => [
'$ref' => '#/definitions/JMSChatUser',
],
'type' => 'array',
],
],
], $this->getModel('JMSChat')->toArray());
$this->assertEquals([
'type' => 'object',
'properties' => [
'picture' => [
'$ref' => '#/definitions/JMSPicture',
],
],
], $this->getModel('JMSChatUser')->toArray());
}
public function testModelDocumentation()
{
$this->assertEquals([