Skip to content

Commit

Permalink
Merge pull request #2039 from nelmio/GuilhemN-patch-1
Browse files Browse the repository at this point in the history
Make ClassMetadataFactory optional
  • Loading branch information
GuilhemN committed Sep 28, 2022
2 parents a46458e + 40ad9c9 commit b42ac43
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DependencyInjection/NelmioApiDocExtension.php
Expand Up @@ -144,7 +144,7 @@ public function load(array $configs, ContainerBuilder $container)
));

$container->getDefinition('nelmio_api_doc.model_describers.object')
->setArgument(4, $config['media_types']);
->setArgument(3, $config['media_types']);

// Add autoconfiguration for model describer
$container->registerForAutoconfiguration(ModelDescriberInterface::class)
Expand Down
19 changes: 11 additions & 8 deletions ModelDescriber/ObjectModelDescriber.php
Expand Up @@ -33,35 +33,35 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar

/** @var PropertyInfoExtractorInterface */
private $propertyInfo;
/** @var ClassMetadataFactoryInterface */
/** @var ClassMetadataFactoryInterface|null */
private $classMetadataFactory;
/** @var Reader */
private $doctrineReader;
/** @var PropertyDescriberInterface[] */
private $propertyDescribers;
/** @var string[] */
private $mediaTypes;
/** @var NameConverterInterface[] */
/** @var NameConverterInterface|null */
private $nameConverter;
/** @var bool */
private $useValidationGroups;

public function __construct(
PropertyInfoExtractorInterface $propertyInfo,
ClassMetadataFactoryInterface $classMetadataFactory,
Reader $reader,
iterable $propertyDescribers,
array $mediaTypes,
NameConverterInterface $nameConverter = null,
bool $useValidationGroups = false
bool $useValidationGroups = false,
ClassMetadataFactoryInterface $classMetadataFactory = null
) {
$this->propertyInfo = $propertyInfo;
$this->classMetadataFactory = $classMetadataFactory;
$this->doctrineReader = $reader;
$this->propertyDescribers = $propertyDescribers;
$this->mediaTypes = $mediaTypes;
$this->nameConverter = $nameConverter;
$this->useValidationGroups = $useValidationGroups;
$this->classMetadataFactory = $classMetadataFactory;
}

public function describe(Model $model, OA\Schema $schema)
Expand Down Expand Up @@ -89,9 +89,12 @@ public function describe(Model $model, OA\Schema $schema)

$schema->type = 'object';

$mapping = $this->classMetadataFactory
->getMetadataFor($class)
->getClassDiscriminatorMapping();
$mapping = false;
if (null !== $this->classMetadataFactory) {
$mapping = $this->classMetadataFactory
->getMetadataFor($class)
->getClassDiscriminatorMapping();
}

if ($mapping && Generator::UNDEFINED === $schema->discriminator) {
$this->applyOpenApiDiscriminator(
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/services.xml
Expand Up @@ -71,12 +71,12 @@
<!-- Model Describers -->
<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="serializer.mapping.class_metadata_factory" />
<argument type="service" id="annotations.reader" />
<argument type="tagged" tag="nelmio_api_doc.object_model.property_describer" />
<argument />
<argument type="service" id="serializer.name_converter.metadata_aware" on-invalid="ignore" />
<argument>%nelmio_api_doc.use_validation_groups%</argument>
<argument type="service" id="serializer.mapping.class_metadata_factory" on-invalid="ignore" />

<tag name="nelmio_api_doc.model_describer" />
</service>
Expand Down

0 comments on commit b42ac43

Please sign in to comment.