mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-09 19:19:29 +03:00
Add exntensible Property Describers
This commit is contained in:
parent
f596adfb4d
commit
f9eacee3fd
@ -17,6 +17,7 @@ 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\PropertyDescriber\PropertyDescriberInterface;
|
||||||
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
|
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
|
||||||
use Symfony\Component\PropertyInfo\Type;
|
use Symfony\Component\PropertyInfo\Type;
|
||||||
|
|
||||||
@ -24,16 +25,22 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar
|
|||||||
{
|
{
|
||||||
use ModelRegistryAwareTrait;
|
use ModelRegistryAwareTrait;
|
||||||
|
|
||||||
|
/** @var PropertyInfoExtractorInterface */
|
||||||
private $propertyInfo;
|
private $propertyInfo;
|
||||||
|
/** @var Reader */
|
||||||
private $doctrineReader;
|
private $doctrineReader;
|
||||||
|
/** @var PropertyDescriberInterface[] */
|
||||||
|
private $propertyDescribers;
|
||||||
|
|
||||||
private $swaggerDefinitionAnnotationReader;
|
private $swaggerDefinitionAnnotationReader;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
PropertyInfoExtractorInterface $propertyInfo,
|
PropertyInfoExtractorInterface $propertyInfo,
|
||||||
Reader $reader
|
Reader $reader,
|
||||||
|
array $propertyDescribers
|
||||||
) {
|
) {
|
||||||
$this->propertyInfo = $propertyInfo;
|
$this->propertyInfo = $propertyInfo;
|
||||||
|
$this->propertyDescribers = $propertyDescribers;
|
||||||
$this->doctrineReader = $reader;
|
$this->doctrineReader = $reader;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,39 +93,18 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar
|
|||||||
}
|
}
|
||||||
|
|
||||||
$type = $types[0];
|
$type = $types[0];
|
||||||
if ($type->isCollection()) {
|
foreach ($this->propertyDescribers as $propertyDescriber) {
|
||||||
$type = $type->getCollectionValueType();
|
if ($propertyDescriber instanceof ModelRegistryAwareInterface) {
|
||||||
if (null === $type) {
|
$propertyDescriber->setModelRegistry($this->modelRegistry);
|
||||||
throw new \LogicException(sprintf('Property "%s:%s" is an array, but its items type isn\'t specified. You can specify that by using the type `string[]` for instance or `@SWG\Property(type="array", @SWG\Items(type="string"))`.', $class, $propertyName));
|
|
||||||
}
|
}
|
||||||
|
if ($propertyDescriber->supports($type)) {
|
||||||
|
$propertyDescriber->describe($type, $property, $model->getGroups());
|
||||||
|
|
||||||
$property->setType('array');
|
break;
|
||||||
$property = $property->getItems();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Type::BUILTIN_TYPE_STRING === $type->getBuiltinType()) {
|
throw new \Exception(sprintf('Type "%s" is not supported in %s::$%s. You may use the `@SWG\Property(type="")` annotation to specify it manually.', $type->getBuiltinType(), $class, $propertyName));
|
||||||
$property->setType('string');
|
|
||||||
} elseif (Type::BUILTIN_TYPE_BOOL === $type->getBuiltinType()) {
|
|
||||||
$property->setType('boolean');
|
|
||||||
} elseif (Type::BUILTIN_TYPE_INT === $type->getBuiltinType()) {
|
|
||||||
$property->setType('integer');
|
|
||||||
} elseif (Type::BUILTIN_TYPE_FLOAT === $type->getBuiltinType()) {
|
|
||||||
$property->setType('number');
|
|
||||||
$property->setFormat('float');
|
|
||||||
} elseif (Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType()) {
|
|
||||||
if (is_a($type->getClassName(), \DateTimeInterface::class, true)) {
|
|
||||||
$property->setType('string');
|
|
||||||
$property->setFormat('date-time');
|
|
||||||
} else {
|
|
||||||
$type = new Type($type->getBuiltinType(), false, $type->getClassName(), $type->isCollection(), $type->getCollectionKeyType(), $type->getCollectionValueType()); // ignore nullable field
|
|
||||||
|
|
||||||
$property->setRef(
|
|
||||||
$this->modelRegistry->register(new Model($type, $model->getGroups()))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new \Exception(sprintf('Type "%s" is not supported in %s::$%s. You may use the `@SWG\Property(type="")` annotation to specify it manually.', $type->getBuiltinType(), $class, $propertyName));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
58
PropertyDescriber/ArrayPropertyDescriber.php
Normal file
58
PropertyDescriber/ArrayPropertyDescriber.php
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<?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\PropertyDescriber;
|
||||||
|
|
||||||
|
use EXSyst\Component\Swagger\Schema;
|
||||||
|
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
|
||||||
|
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
|
||||||
|
use Symfony\Component\PropertyInfo\Type;
|
||||||
|
|
||||||
|
class ArrayPropertyDescriber implements PropertyDescriberInterface, ModelRegistryAwareInterface
|
||||||
|
{
|
||||||
|
use ModelRegistryAwareTrait;
|
||||||
|
|
||||||
|
/** @var PropertyDescriberInterface[] */
|
||||||
|
private $propertyDescribers;
|
||||||
|
|
||||||
|
public function __construct(array $propertyDescribers)
|
||||||
|
{
|
||||||
|
$this->propertyDescribers = $propertyDescribers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function describe(Type $type, Schema $property, array $groups)
|
||||||
|
{
|
||||||
|
$type = $type->getCollectionValueType();
|
||||||
|
if (null === $type) {
|
||||||
|
throw new \LogicException(sprintf('Property "%s:%s" is an array, but its items type isn\'t specified. You can specify that by using the type `string[]` for instance or `@SWG\Property(type="array", @SWG\Items(type="string"))`.', $class, $propertyName));
|
||||||
|
}
|
||||||
|
|
||||||
|
$property->setType('array');
|
||||||
|
$property = $property->getItems();
|
||||||
|
|
||||||
|
foreach ($this->propertyDescribers as $propertyDescriber) {
|
||||||
|
if ($propertyDescriber instanceof ModelRegistryAwareInterface) {
|
||||||
|
$propertyDescriber->setModelRegistry($this->modelRegistry);
|
||||||
|
}
|
||||||
|
if ($propertyDescriber->supports($type)) {
|
||||||
|
$propertyDescriber->describe($type, $property);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supports(Type $type): bool
|
||||||
|
{
|
||||||
|
return $type->isCollection();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
29
PropertyDescriber/BooleanPropertyDescriber.php
Normal file
29
PropertyDescriber/BooleanPropertyDescriber.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?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\PropertyDescriber;
|
||||||
|
|
||||||
|
use EXSyst\Component\Swagger\Schema;
|
||||||
|
use Symfony\Component\PropertyInfo\Type;
|
||||||
|
|
||||||
|
class BooleanPropertyDescriber implements PropertyDescriberInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
public function describe(Type $type, Schema $property, array $groups)
|
||||||
|
{
|
||||||
|
$property->setType('boolean');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supports(Type $type): bool
|
||||||
|
{
|
||||||
|
return Type::BUILTIN_TYPE_BOOL === $type->getBuiltinType();
|
||||||
|
}
|
||||||
|
}
|
31
PropertyDescriber/DateTimePropertyDescriber.php
Normal file
31
PropertyDescriber/DateTimePropertyDescriber.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?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\PropertyDescriber;
|
||||||
|
|
||||||
|
use EXSyst\Component\Swagger\Schema;
|
||||||
|
use Symfony\Component\PropertyInfo\Type;
|
||||||
|
|
||||||
|
class DateTimePropertyDescriber implements PropertyDescriberInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
public function describe(Type $type, Schema $property, array $groups)
|
||||||
|
{
|
||||||
|
$property->setType('string');
|
||||||
|
$property->setFormat('date-time');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supports(Type $type): bool
|
||||||
|
{
|
||||||
|
return Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType()
|
||||||
|
&& is_a($type->getClassName(), \DateTimeInterface::class, true);
|
||||||
|
}
|
||||||
|
}
|
30
PropertyDescriber/FloatPropertyDescriber.php
Normal file
30
PropertyDescriber/FloatPropertyDescriber.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?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\PropertyDescriber;
|
||||||
|
|
||||||
|
use EXSyst\Component\Swagger\Schema;
|
||||||
|
use Symfony\Component\PropertyInfo\Type;
|
||||||
|
|
||||||
|
class FloatPropertyDescriber implements PropertyDescriberInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
public function describe(Type $type, Schema $property, array $groups)
|
||||||
|
{
|
||||||
|
$property->setType('number');
|
||||||
|
$property->setFormat('float');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supports(Type $type): bool
|
||||||
|
{
|
||||||
|
return Type::BUILTIN_TYPE_FLOAT === $type->getBuiltinType();
|
||||||
|
}
|
||||||
|
}
|
29
PropertyDescriber/IntegerPropertyDescriber.php
Normal file
29
PropertyDescriber/IntegerPropertyDescriber.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?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\PropertyDescriber;
|
||||||
|
|
||||||
|
use EXSyst\Component\Swagger\Schema;
|
||||||
|
use Symfony\Component\PropertyInfo\Type;
|
||||||
|
|
||||||
|
class IntegerPropertyDescriber implements PropertyDescriberInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
public function describe(Type $type, Schema $property, array $groups)
|
||||||
|
{
|
||||||
|
$property->setType('integer');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supports(Type $type): bool
|
||||||
|
{
|
||||||
|
return Type::BUILTIN_TYPE_INT === $type->getBuiltinType();
|
||||||
|
}
|
||||||
|
}
|
36
PropertyDescriber/ObjectPropertyDescriber.php
Normal file
36
PropertyDescriber/ObjectPropertyDescriber.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?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\PropertyDescriber;
|
||||||
|
|
||||||
|
use EXSyst\Component\Swagger\Schema;
|
||||||
|
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
|
||||||
|
use Nelmio\ApiDocBundle\Model\Model;
|
||||||
|
use Symfony\Component\PropertyInfo\Type;
|
||||||
|
|
||||||
|
class ObjectPropertyDescriber implements PropertyDescriberInterface
|
||||||
|
{
|
||||||
|
use ModelRegistryAwareTrait;
|
||||||
|
|
||||||
|
public function describe(Type $type, Schema $property, array $groups)
|
||||||
|
{
|
||||||
|
$type = new Type($type->getBuiltinType(), false, $type->getClassName(), $type->isCollection(), $type->getCollectionKeyType(), $type->getCollectionValueType()); // ignore nullable field
|
||||||
|
|
||||||
|
$property->setRef(
|
||||||
|
$this->modelRegistry->register(new Model($type, $groups))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supports(Type $type): bool
|
||||||
|
{
|
||||||
|
return Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType();
|
||||||
|
}
|
||||||
|
}
|
22
PropertyDescriber/PropertyDescriberInterface.php
Normal file
22
PropertyDescriber/PropertyDescriberInterface.php
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?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\PropertyDescriber;
|
||||||
|
|
||||||
|
use Symfony\Component\PropertyInfo\Type;
|
||||||
|
use EXSyst\Component\Swagger\Schema;
|
||||||
|
|
||||||
|
interface PropertyDescriberInterface
|
||||||
|
{
|
||||||
|
public function describe(Type $type, Schema $property, array $groups);
|
||||||
|
|
||||||
|
public function supports(Type $type): bool;
|
||||||
|
}
|
29
PropertyDescriber/StringPropertyDescriber.php
Normal file
29
PropertyDescriber/StringPropertyDescriber.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?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\PropertyDescriber;
|
||||||
|
|
||||||
|
use EXSyst\Component\Swagger\Schema;
|
||||||
|
use Symfony\Component\PropertyInfo\Type;
|
||||||
|
|
||||||
|
class StringPropertyDescriber implements PropertyDescriberInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
public function describe(Type $type, Schema $property, array $groups)
|
||||||
|
{
|
||||||
|
$property->setType('string');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supports(Type $type): bool
|
||||||
|
{
|
||||||
|
return Type::BUILTIN_TYPE_STRING === $type->getBuiltinType();
|
||||||
|
}
|
||||||
|
}
|
@ -41,6 +41,7 @@
|
|||||||
<service id="nelmio_api_doc.model_describers.object" class="Nelmio\ApiDocBundle\ModelDescriber\ObjectModelDescriber" public="false">
|
<service id="nelmio_api_doc.model_describers.object" class="Nelmio\ApiDocBundle\ModelDescriber\ObjectModelDescriber" public="false">
|
||||||
<argument type="service" id="property_info" />
|
<argument type="service" id="property_info" />
|
||||||
<argument type="service" id="annotation_reader" />
|
<argument type="service" id="annotation_reader" />
|
||||||
|
<argument type="tagged_iterator" id="nelmio_api_doc.property_describer" />
|
||||||
|
|
||||||
<tag name="nelmio_api_doc.model_describer" />
|
<tag name="nelmio_api_doc.model_describer" />
|
||||||
</service>
|
</service>
|
||||||
@ -49,6 +50,35 @@
|
|||||||
<tag name="nelmio_api_doc.model_describer" priority="-1000" />
|
<tag name="nelmio_api_doc.model_describer" priority="-1000" />
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
|
<!-- Property Describers -->
|
||||||
|
<service id="nelmio_api_doc.property_describers.array" class="Nelmio\ApiDocBundle\ModelDescriber\ArrayPropertyDescriber" public="false">
|
||||||
|
<tag name="nelmio_api_doc.property_describer" />
|
||||||
|
</service>
|
||||||
|
|
||||||
|
<service id="nelmio_api_doc.property_describers.boolean" class="Nelmio\ApiDocBundle\ModelDescriber\BooleanPropertyDescriber" public="false">
|
||||||
|
<tag name="nelmio_api_doc.property_describer" />
|
||||||
|
</service>
|
||||||
|
|
||||||
|
<service id="nelmio_api_doc.property_describers.float" class="Nelmio\ApiDocBundle\ModelDescriber\FloatPropertyDescriber" public="false">
|
||||||
|
<tag name="nelmio_api_doc.property_describer" />
|
||||||
|
</service>
|
||||||
|
|
||||||
|
<service id="nelmio_api_doc.property_describers.integer" class="Nelmio\ApiDocBundle\ModelDescriber\IntegerPropertyDescriber" public="false">
|
||||||
|
<tag name="nelmio_api_doc.property_describer" />
|
||||||
|
</service>
|
||||||
|
|
||||||
|
<service id="nelmio_api_doc.property_describers.string" class="Nelmio\ApiDocBundle\ModelDescriber\StringPropertyDescriber" public="false">
|
||||||
|
<tag name="nelmio_api_doc.property_describer" />
|
||||||
|
</service>
|
||||||
|
|
||||||
|
<service id="nelmio_api_doc.property_describers.date_time" class="Nelmio\ApiDocBundle\ModelDescriber\DateTimePropertyDescriber" public="false">
|
||||||
|
<tag name="nelmio_api_doc.property_describer" />
|
||||||
|
</service>
|
||||||
|
|
||||||
|
<service id="nelmio_api_doc.property_describers.object" class="Nelmio\ApiDocBundle\ModelDescriber\ObjectPropertyDescriber" public="false">
|
||||||
|
<tag name="nelmio_api_doc.property_describer" priority="-1000" />
|
||||||
|
</service>
|
||||||
|
|
||||||
<!-- Form Type Extensions -->
|
<!-- Form Type Extensions -->
|
||||||
|
|
||||||
<service id="nelmio_api_doc.form.documentation_extension" class="Nelmio\ApiDocBundle\Form\Extension\DocumentationExtension">
|
<service id="nelmio_api_doc.form.documentation_extension" class="Nelmio\ApiDocBundle\Form\Extension\DocumentationExtension">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user