Fix complex groups for jms serializer

This commit is contained in:
Yi Ping 2017-12-07 14:00:40 +08:00
parent 01f691c456
commit d4bf23bcfb
6 changed files with 94 additions and 4 deletions

View File

@ -95,8 +95,14 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
continue;
}
if(empty($model->getGroups()) || !array_key_exists($name, $model->getGroups()) || !is_array($model->getGroups()[$name]))
$groups = $model->getGroups();
else
$groups = $model->getGroups()[$name];
$property->setRef(
$this->modelRegistry->register(new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, $type), $model->getGroups()))
$this->modelRegistry->register(new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, $type), $groups))
);
}

View File

@ -13,6 +13,7 @@ namespace Nelmio\ApiDocBundle\Tests\Functional\Controller;
use Nelmio\ApiDocBundle\Annotation\Model;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSUser;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSComplex;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Swagger\Annotations as SWG;
@ -29,4 +30,17 @@ class JMSController
public function userAction()
{
}
/**
* @Route("/api/jms_complex", methods={"GET"})
* @SWG\Response(
* response=200,
* description="Success",
* @Model(type=JMSComplex::class, groups={"list", "details", "user" : {"list"}})
* )
*/
public function complexAction()
{
}
}

View File

@ -0,0 +1,43 @@
<?php
/**
* Created by PhpStorm.
* User: Frisks
* Date: 2/12/2017
* Time: 1:18 PM
*/
namespace Nelmio\ApiDocBundle\Tests\Functional\Entity;
use JMS\Serializer\Annotation as Serializer;
/**
* Class JMSComplex
* @package Nelmio\ApiDocBundle\Tests\Functional\Entity
* @Serializer\ExclusionPolicy("all")
*/
class JMSComplex
{
/**
* @Serializer\Type("integer")
* @Serializer\Expose
* @Serializer\Groups({"list"})
*/
private $id;
/**
* @Serializer\Type("Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSUser")
* @Serializer\Expose
* @Serializer\Groups({"details"})
*/
private $user;
/**
* @Serializer\Type("string")
* @Serializer\Expose
* @Serializer\Groups({"list"})
*/
private $name;
}

View File

@ -24,7 +24,7 @@ class JMSUser
/**
* @Serializer\Type("integer")
* @Serializer\Expose
*
* @Serializer\Groups({"list"})
* @SWG\Property(description = "User id", required = true, readOnly = true, title = "userid", example=1)
*/
private $id;
@ -32,8 +32,8 @@ class JMSUser
/**
* @Serializer\Type("string")
* @Serializer\Expose
*
* @SWG\Property(readOnly = false)
* @Serializer\Groups({"details"})
*/
private $email;

View File

@ -49,6 +49,32 @@ class JMSFunctionalTest extends WebTestCase
], $this->getModel('JMSUser')->toArray());
}
public function testModelComplexDocumentation()
{
$this->assertEquals([
'type' => 'object',
'properties' => [
'id' => ['type' => 'integer'],
'user' => ['$ref' => '#/definitions/JMSUser2'],
'name' => ['type' => 'string']
],
], $this->getModel('JMSComplex')->toArray());
$this->assertEquals([
'type' => 'object',
'properties' => [
'id' => [
'type' => 'integer',
'title' => 'userid',
'description' => 'User id',
'readOnly' => true,
'example' => '1',
],
],
], $this->getModel('JMSUser2')->toArray());
}
protected static function createKernel(array $options = array())
{
return new TestKernel(true);

View File

@ -37,13 +37,14 @@
"symfony/browser-kit": "^2.8|^3.0|^4.0",
"symfony/cache": "^3.1|^4.0",
"symfony/phpunit-bridge": "^3.3",
"symfony/stopwatch": "^2.8|^3.0|^4.0",
"sensio/framework-extra-bundle": "^3.0",
"doctrine/annotations": "^1.2",
"phpdocumentor/reflection-docblock": "^3.1",
"api-platform/core": "^2.0.3",
"friendsofsymfony/rest-bundle": "^2.0",
"jms/serializer-bundle": "^1.0|^2.0"
"jms/serializer-bundle": "^2.0"
},
"suggest": {
"phpdocumentor/reflection-docblock": "For parsing php docs.",