Detect when a model is used for a form type (#1834)

* Detect when a model is used for a form type

* typo

* Add a test + Fix implementation

* CS
This commit is contained in:
Guilhem Niot 2022-06-10 22:41:24 +02:00 committed by GitHub
parent b2b6d0f798
commit a6f25da106
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 1 deletions

View File

@ -16,7 +16,9 @@ use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait; use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
use Nelmio\ApiDocBundle\Model\Model; use Nelmio\ApiDocBundle\Model\Model;
use Nelmio\ApiDocBundle\ModelDescriber\Annotations\AnnotationsReader; use Nelmio\ApiDocBundle\ModelDescriber\Annotations\AnnotationsReader;
use Nelmio\ApiDocBundle\OpenApiPhp\ModelRegister;
use Nelmio\ApiDocBundle\OpenApiPhp\Util; use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Analysis;
use OpenApi\Annotations as OA; use OpenApi\Annotations as OA;
use OpenApi\Generator; use OpenApi\Generator;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
@ -103,9 +105,13 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry
if ($config->hasOption('documentation')) { if ($config->hasOption('documentation')) {
$property->mergeProperties($config->getOption('documentation')); $property->mergeProperties($config->getOption('documentation'));
// Parse inner @Model annotations
$modelRegister = new ModelRegister($this->modelRegistry, $this->mediaTypes);
$modelRegister->__invoke(new Analysis([$property], Util::createContext()));
} }
if (Generator::UNDEFINED !== $property->type) { if (Generator::UNDEFINED !== $property->type || Generator::UNDEFINED !== $property->ref) {
continue; // Type manually defined continue; // Type manually defined
} }

View File

@ -25,6 +25,7 @@ use Nelmio\ApiDocBundle\Tests\Functional\Entity\SymfonyDiscriminator;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\User; use Nelmio\ApiDocBundle\Tests\Functional\Entity\User;
use Nelmio\ApiDocBundle\Tests\Functional\Form\DummyType; use Nelmio\ApiDocBundle\Tests\Functional\Form\DummyType;
use Nelmio\ApiDocBundle\Tests\Functional\Form\FormWithAlternateSchemaType; use Nelmio\ApiDocBundle\Tests\Functional\Form\FormWithAlternateSchemaType;
use Nelmio\ApiDocBundle\Tests\Functional\Form\FormWithModel;
use Nelmio\ApiDocBundle\Tests\Functional\Form\FormWithRefType; use Nelmio\ApiDocBundle\Tests\Functional\Form\FormWithRefType;
use Nelmio\ApiDocBundle\Tests\Functional\Form\UserType; use Nelmio\ApiDocBundle\Tests\Functional\Form\UserType;
use OpenApi\Annotations as OA; use OpenApi\Annotations as OA;
@ -158,6 +159,18 @@ class ApiController80
{ {
} }
/**
* @Route("/form-model", methods={"POST"})
* @OA\RequestBody(
* description="Request content",
* @Model(type=FormWithModel::class))
* )
* @OA\Response(response="201", description="")
*/
public function formWithModelAction()
{
}
/** /**
* @Route("/security") * @Route("/security")
* @OA\Response(response="201", description="") * @OA\Response(response="201", description="")

View File

@ -0,0 +1,32 @@
<?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\Form;
use Nelmio\ApiDocBundle\Annotation\Model;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class FormWithModel extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('quz', TextType::class, ['documentation' => ['ref' => new Model(['type' => User::class])]]);
}
public function configureOptions(OptionsResolver $resolver)
{
}
}

View File

@ -343,6 +343,17 @@ class FunctionalTest extends WebTestCase
'required' => ['foo', 'foz', 'password'], 'required' => ['foo', 'foz', 'password'],
'schema' => 'DummyType', 'schema' => 'DummyType',
], json_decode($this->getModel('DummyType')->toJson(), true)); ], json_decode($this->getModel('DummyType')->toJson(), true));
$this->assertEquals([
'type' => 'object',
'properties' => [
'quz' => [
'$ref' => '#/components/schemas/User',
],
],
'required' => ['quz'],
'schema' => 'FormWithModel',
], json_decode($this->getModel('FormWithModel')->toJson(), true));
} }
/** /**