Merge pull request #1132 from 7thcubic/complex_groups

Fix complex groups context for jms models
This commit is contained in:
Guilhem N 2018-01-03 12:01:06 +01:00 committed by GitHub
commit 8a53891ee9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 2 deletions

View File

@ -100,8 +100,15 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
continue; continue;
} }
if(!array_key_exists($name, $model->getGroups()) || !is_array($model->getGroups()[$name])) {
$groups = $model->getGroups();
} else {
$groups = $model->getGroups()[$name];
}
$property->setRef( $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\Annotation\Model;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSUser; use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSUser;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSComplex;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\VirtualProperty; use Nelmio\ApiDocBundle\Tests\Functional\Entity\VirtualProperty;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Swagger\Annotations as SWG; use Swagger\Annotations as SWG;
@ -42,4 +43,16 @@ class JMSController
public function yamlAction() public function yamlAction()
{ {
} }
/**
* @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,6 +24,7 @@ class JMSUser
/** /**
* @Serializer\Type("integer") * @Serializer\Type("integer")
* @Serializer\Expose * @Serializer\Expose
* @Serializer\Groups({"list"})
* *
* @SWG\Property(description = "User id", required = true, readOnly = true, title = "userid", example=1, default = null) * @SWG\Property(description = "User id", required = true, readOnly = true, title = "userid", example=1, default = null)
*/ */
@ -41,8 +42,8 @@ class JMSUser
/** /**
* @Serializer\Type("string") * @Serializer\Type("string")
* @Serializer\Expose * @Serializer\Expose
*
* @SWG\Property(readOnly = false) * @SWG\Property(readOnly = false)
* @Serializer\Groups({"details"})
*/ */
private $email; private $email;

View File

@ -63,6 +63,31 @@ class JMSFunctionalTest extends WebTestCase
], $this->getModel('JMSUser')->toArray()); ], $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());
}
public function testYamlConfig() public function testYamlConfig()
{ {
$this->assertEquals([ $this->assertEquals([

View File

@ -34,6 +34,7 @@
"symfony/browser-kit": "^2.8|^3.0|^4.0", "symfony/browser-kit": "^2.8|^3.0|^4.0",
"symfony/cache": "^3.1|^4.0", "symfony/cache": "^3.1|^4.0",
"symfony/phpunit-bridge": "^3.3", "symfony/phpunit-bridge": "^3.3",
"symfony/stopwatch": "^2.8|^3.0|^4.0",
"sensio/framework-extra-bundle": "^3.0", "sensio/framework-extra-bundle": "^3.0",
"symfony/stopwatch": "^2.8|^3.0|^4.0", "symfony/stopwatch": "^2.8|^3.0|^4.0",
"doctrine/annotations": "^1.2", "doctrine/annotations": "^1.2",