jms 2.0 groups tested

This commit is contained in:
Asmir Mustafic 2019-01-26 14:52:13 +01:00
parent e738102514
commit 0fb5d7afa2
No known key found for this signature in database
GPG Key ID: 5408354D09CC64FC
8 changed files with 223 additions and 5 deletions

View File

@ -23,7 +23,6 @@ use Nelmio\ApiDocBundle\Routing\FilteredRouteCollectionBuilder;
use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

View File

@ -68,6 +68,8 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
$annotationsReader = new AnnotationsReader($this->doctrineReader, $this->modelRegistry); $annotationsReader = new AnnotationsReader($this->doctrineReader, $this->modelRegistry);
$annotationsReader->updateDefinition(new \ReflectionClass($className), $schema); $annotationsReader->updateDefinition(new \ReflectionClass($className), $schema);
$isJmsV1 = null !== $this->namingStrategy;
$properties = $schema->getProperties(); $properties = $schema->getProperties();
foreach ($metadata->propertyMetadata as $item) { foreach ($metadata->propertyMetadata as $item) {
// filter groups // filter groups
@ -84,8 +86,10 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
} elseif (!isset($groups[$item->name]) && !empty($this->previousGroups[$model->getHash()])) { } elseif (!isset($groups[$item->name]) && !empty($this->previousGroups[$model->getHash()])) {
$groups = false === $this->propertyTypeUsesGroups($item->type) $groups = false === $this->propertyTypeUsesGroups($item->type)
? null ? null
: ($this->namingStrategy ? [GroupsExclusionStrategy::DEFAULT_GROUP] : $this->previousGroups[$model->getHash()]); : ($isJmsV1 ? [GroupsExclusionStrategy::DEFAULT_GROUP] : $this->previousGroups[$model->getHash()]);
} elseif (is_array($groups)) { }
if (is_array($groups)) {
$groups = array_filter($groups, 'is_scalar'); $groups = array_filter($groups, 'is_scalar');
} }
@ -93,10 +97,10 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
$groups = null; $groups = null;
} }
$name = $this->namingStrategy ? $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 // read property options from Swagger Property annotation if it exists
try { try {
if (property_exists($item, 'reflection') && null !== $item->reflection) { if (true === $isJmsV1 && property_exists($item, 'reflection') && null !== $item->reflection) {
$reflection = $item->reflection; $reflection = $item->reflection;
} elseif ($item instanceof VirtualProperty) { } elseif ($item instanceof VirtualProperty) {
$reflection = new \ReflectionProperty($item->class, $item->name); $reflection = new \ReflectionProperty($item->class, $item->name);

View File

@ -17,6 +17,7 @@ use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSDualComplex;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSNamingStrategyConstraints; use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSNamingStrategyConstraints;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSUser; use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSUser;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChat; use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChat;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChatRoomUser;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChatUser; use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChatUser;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSPicture; use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSPicture;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\VirtualProperty; use Nelmio\ApiDocBundle\Tests\Functional\Entity\VirtualProperty;
@ -123,4 +124,16 @@ class JMSController
public function minUserAction() public function minUserAction()
{ {
} }
/**
* @Route("/api/jms_mini_user_nested", methods={"GET"})
* @SWG\Response(
* response=200,
* description="Success",
* @Model(type=JMSChatRoomUser::class, groups={"mini", "friend": {"living":{"Default"}}})
* )
*/
public function minUserNestedAction()
{
}
} }

View File

@ -0,0 +1,42 @@
<?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 JMSChatFriend
{
/**
* @Serializer\Type("Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChatRoom")
* @Serializer\Expose
* @Serializer\Groups({"mini"})
*/
private $room;
/**
* @Serializer\Type("Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChatLivingRoom")
* @Serializer\Expose
* @Serializer\Groups({"Default"})
*/
private $living;
/**
* @Serializer\Type("Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChatRoom")
* @Serializer\Expose
*/
private $dining;
}

View File

@ -0,0 +1,28 @@
<?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 JMSChatLivingRoom
{
/**
* @Serializer\Type("integer")
* @Serializer\Expose
*/
private $id;
}

View File

@ -0,0 +1,42 @@
<?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 JMSChatRoom
{
/**
* @Serializer\Type("integer")
* @Serializer\Expose
* @Serializer\Groups({"Default"})
*/
private $id1;
/**
* @Serializer\Type("integer")
* @Serializer\Expose
* @Serializer\Groups({"mini"})
*/
private $id2;
/**
* @Serializer\Type("integer")
* @Serializer\Expose
*/
private $id3;
}

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 JMSChatRoomUser
{
/**
* @Serializer\Type("integer")
* @Serializer\Expose
*/
private $id;
/**
* @Serializer\Type("Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChatFriend")
* @Serializer\Expose
* @Serializer\Groups({"mini"})
*/
private $friend;
}

View File

@ -11,6 +11,8 @@
namespace Nelmio\ApiDocBundle\Tests\Functional; namespace Nelmio\ApiDocBundle\Tests\Functional;
use JMS\Serializer\Visitor\SerializationVisitorInterface;
class JMSFunctionalTest extends WebTestCase class JMSFunctionalTest extends WebTestCase
{ {
public function testModelPictureDocumentation() public function testModelPictureDocumentation()
@ -192,6 +194,59 @@ class JMSFunctionalTest extends WebTestCase
], $this->getModel('JMSDualComplex')->toArray()); ], $this->getModel('JMSDualComplex')->toArray());
} }
public function testNestedGroupsV1()
{
if (interface_exists(SerializationVisitorInterface::class)){
$this->markTestSkipped('This applies only for jms/serializer v1.x');
}
$this->assertEquals([
'type' => 'object',
'properties' => [
'living' => ['$ref' => '#/definitions/JMSChatLivingRoom'],
'dining' => ['$ref' => '#/definitions/JMSChatRoom'],
],
], $this->getModel('JMSChatFriend')->toArray());
$this->assertEquals([
'type' => 'object',
'properties' => [
'id1' => ['type' => 'integer'],
'id2' => ['type' => 'integer'],
'id3' => ['type' => 'integer'],
],
], $this->getModel('JMSChatRoom')->toArray());
}
public function testNestedGroupsV2()
{
if (!interface_exists(SerializationVisitorInterface::class)){
$this->markTestSkipped('This applies only for jms/serializer v2.x');
}
$this->assertEquals([
'type' => 'object',
'properties' => [
'living' => ['$ref' => '#/definitions/JMSChatLivingRoom'],
'dining' => ['$ref' => '#/definitions/JMSChatRoom'],
],
], $this->getModel('JMSChatFriend')->toArray());
$this->assertEquals([
'type' => 'object',
'properties' => [
'id2' => ['type' => 'integer'],
],
], $this->getModel('JMSChatRoom')->toArray());
$this->assertEquals([
'type' => 'object',
'properties' => [
'id' => ['type' => 'integer'],
],
], $this->getModel('JMSChatLivingRoom')->toArray());
}
public function testModelComplexDocumentation() public function testModelComplexDocumentation()
{ {
$this->assertEquals([ $this->assertEquals([