vendor/nelmio/api-doc-bundle/src/ModelDescriber/ObjectModelDescriber.php line 51

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the NelmioApiDocBundle package.
  4. *
  5. * (c) Nelmio
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Nelmio\ApiDocBundle\ModelDescriber;
  11. use Doctrine\Common\Annotations\Reader;
  12. use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
  13. use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
  14. use Nelmio\ApiDocBundle\Model\Model;
  15. use Nelmio\ApiDocBundle\ModelDescriber\Annotations\AnnotationsReader;
  16. use Nelmio\ApiDocBundle\OpenApiPhp\Util;
  17. use Nelmio\ApiDocBundle\PropertyDescriber\PropertyDescriberInterface;
  18. use OpenApi\Annotations as OA;
  19. use OpenApi\Generator;
  20. use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
  21. use Symfony\Component\PropertyInfo\Type;
  22. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
  23. use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
  24. class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwareInterface
  25. {
  26. use ModelRegistryAwareTrait;
  27. use ApplyOpenApiDiscriminatorTrait;
  28. /** @var PropertyInfoExtractorInterface */
  29. private $propertyInfo;
  30. /** @var ClassMetadataFactoryInterface|null */
  31. private $classMetadataFactory;
  32. /** @var Reader|null */
  33. private $doctrineReader;
  34. /** @var PropertyDescriberInterface|PropertyDescriberInterface[] */
  35. private $propertyDescriber;
  36. /** @var string[] */
  37. private $mediaTypes;
  38. /** @var NameConverterInterface|null */
  39. private $nameConverter;
  40. /** @var bool */
  41. private $useValidationGroups;
  42. /**
  43. * @param PropertyDescriberInterface|PropertyDescriberInterface[] $propertyDescribers
  44. */
  45. public function __construct(
  46. PropertyInfoExtractorInterface $propertyInfo,
  47. ?Reader $reader,
  48. $propertyDescribers,
  49. array $mediaTypes,
  50. NameConverterInterface $nameConverter = null,
  51. bool $useValidationGroups = false,
  52. ClassMetadataFactoryInterface $classMetadataFactory = null
  53. ) {
  54. if (is_iterable($propertyDescribers)) {
  55. trigger_deprecation('nelmio/api-doc-bundle', '4.17', 'Passing an array of PropertyDescriberInterface to %s() is deprecated. Pass a single PropertyDescriberInterface instead.', __METHOD__);
  56. } else {
  57. if (!$propertyDescribers instanceof PropertyDescriberInterface) {
  58. throw new \InvalidArgumentException(sprintf('Argument 3 passed to %s() must be an array of %s or a single %s.', __METHOD__, PropertyDescriberInterface::class, PropertyDescriberInterface::class));
  59. }
  60. }
  61. $this->propertyInfo = $propertyInfo;
  62. $this->doctrineReader = $reader;
  63. $this->propertyDescriber = $propertyDescribers;
  64. $this->mediaTypes = $mediaTypes;
  65. $this->nameConverter = $nameConverter;
  66. $this->useValidationGroups = $useValidationGroups;
  67. $this->classMetadataFactory = $classMetadataFactory;
  68. }
  69. public function describe(Model $model, OA\Schema $schema)
  70. {
  71. $class = $model->getType()->getClassName();
  72. $schema->_context->class = $class;
  73. $context = ['serializer_groups' => null];
  74. if (null !== $model->getGroups()) {
  75. $context['serializer_groups'] = array_filter($model->getGroups(), 'is_string');
  76. }
  77. $reflClass = new \ReflectionClass($class);
  78. $annotationsReader = new AnnotationsReader(
  79. $this->doctrineReader,
  80. $this->modelRegistry,
  81. $this->mediaTypes,
  82. $this->useValidationGroups
  83. );
  84. $classResult = $annotationsReader->updateDefinition($reflClass, $schema);
  85. if (!$classResult->shouldDescribeModelProperties()) {
  86. return;
  87. }
  88. $schema->type = 'object';
  89. $mapping = false;
  90. if (null !== $this->classMetadataFactory) {
  91. $mapping = $this->classMetadataFactory
  92. ->getMetadataFor($class)
  93. ->getClassDiscriminatorMapping();
  94. }
  95. if ($mapping && Generator::UNDEFINED === $schema->discriminator) {
  96. $this->applyOpenApiDiscriminator(
  97. $model,
  98. $schema,
  99. $this->modelRegistry,
  100. $mapping->getTypeProperty(),
  101. $mapping->getTypesMapping()
  102. );
  103. }
  104. $propertyInfoProperties = $this->propertyInfo->getProperties($class, $context);
  105. if (null === $propertyInfoProperties) {
  106. return;
  107. }
  108. // Fix for https://github.com/nelmio/NelmioApiDocBundle/issues/1756
  109. // The SerializerExtractor does expose private/protected properties for some reason, so we eliminate them here
  110. $propertyInfoProperties = array_intersect($propertyInfoProperties, $this->propertyInfo->getProperties($class, []) ?? []);
  111. $defaultValues = array_filter($reflClass->getDefaultProperties(), static function ($value) {
  112. return null !== $value;
  113. });
  114. foreach ($propertyInfoProperties as $propertyName) {
  115. $serializedName = null !== $this->nameConverter ? $this->nameConverter->normalize($propertyName, $class, null, $model->getSerializationContext()) : $propertyName;
  116. $reflections = $this->getReflections($reflClass, $propertyName);
  117. // Check if a custom name is set
  118. foreach ($reflections as $reflection) {
  119. $serializedName = $annotationsReader->getPropertyName($reflection, $serializedName);
  120. }
  121. $property = Util::getProperty($schema, $serializedName);
  122. // Interpret additional options
  123. $groups = $model->getGroups();
  124. if (isset($groups[$propertyName]) && is_array($groups[$propertyName])) {
  125. $groups = $model->getGroups()[$propertyName];
  126. }
  127. foreach ($reflections as $reflection) {
  128. $annotationsReader->updateProperty($reflection, $property, $groups);
  129. }
  130. // If type manually defined
  131. if (Generator::UNDEFINED !== $property->type || Generator::UNDEFINED !== $property->ref) {
  132. continue;
  133. }
  134. if (Generator::UNDEFINED === $property->default && array_key_exists($propertyName, $defaultValues)) {
  135. $property->default = $defaultValues[$propertyName];
  136. }
  137. $types = $this->propertyInfo->getTypes($class, $propertyName);
  138. if (null === $types || 0 === count($types)) {
  139. throw new \LogicException(sprintf('The PropertyInfo component was not able to guess the type of %s::$%s. You may need to add a `@var` annotation or use `@OA\Property(type="")` to make its type explicit.', $class, $propertyName));
  140. }
  141. $this->describeProperty($types, $model, $property, $propertyName, $schema);
  142. }
  143. }
  144. /**
  145. * @return \ReflectionProperty[]|\ReflectionMethod[]
  146. */
  147. private function getReflections(\ReflectionClass $reflClass, string $propertyName): array
  148. {
  149. $reflections = [];
  150. if ($reflClass->hasProperty($propertyName)) {
  151. $reflections[] = $reflClass->getProperty($propertyName);
  152. }
  153. $camelProp = $this->camelize($propertyName);
  154. foreach (['', 'get', 'is', 'has', 'can', 'add', 'remove', 'set'] as $prefix) {
  155. if ($reflClass->hasMethod($prefix.$camelProp)) {
  156. $reflections[] = $reflClass->getMethod($prefix.$camelProp);
  157. }
  158. }
  159. return $reflections;
  160. }
  161. /**
  162. * Camelizes a given string.
  163. */
  164. private function camelize(string $string): string
  165. {
  166. return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
  167. }
  168. /**
  169. * @param Type[] $types
  170. */
  171. private function describeProperty(array $types, Model $model, OA\Schema $property, string $propertyName, OA\Schema $schema)
  172. {
  173. $propertyDescribers = is_iterable($this->propertyDescriber) ? $this->propertyDescriber : [$this->propertyDescriber];
  174. foreach ($propertyDescribers as $propertyDescriber) {
  175. if ($propertyDescriber instanceof ModelRegistryAwareInterface) {
  176. $propertyDescriber->setModelRegistry($this->modelRegistry);
  177. }
  178. if ($propertyDescriber->supports($types)) {
  179. $propertyDescriber->describe($types, $property, $model->getGroups(), $schema, $model->getSerializationContext());
  180. return;
  181. }
  182. }
  183. throw new \Exception(sprintf('Type "%s" is not supported in %s::$%s. You may use the `@OA\Property(type="")` annotation to specify it manually.', $types[0]->getBuiltinType(), $model->getType()->getClassName(), $propertyName));
  184. }
  185. public function supports(Model $model): bool
  186. {
  187. return Type::BUILTIN_TYPE_OBJECT === $model->getType()->getBuiltinType()
  188. && (class_exists($model->getType()->getClassName()) || interface_exists($model->getType()->getClassName()));
  189. }
  190. }