vendor/api-platform/core/src/Serializer/SerializerContextBuilder.php line 34

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the API Platform project.
  4. *
  5. * (c) Kévin Dunglas <dunglas@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Serializer;
  12. use ApiPlatform\Core\Api\OperationType;
  13. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  14. use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
  15. use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer;
  16. use ApiPlatform\Exception\RuntimeException;
  17. use ApiPlatform\Metadata\CollectionOperationInterface;
  18. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  19. use ApiPlatform\Util\RequestAttributesExtractor;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\Serializer\Encoder\CsvEncoder;
  22. /**
  23. * @author Kévin Dunglas <dunglas@gmail.com>
  24. */
  25. final class SerializerContextBuilder implements SerializerContextBuilderInterface
  26. {
  27. private $resourceMetadataFactory;
  28. public function __construct($resourceMetadataFactory)
  29. {
  30. $this->resourceMetadataFactory = $resourceMetadataFactory;
  31. if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  32. trigger_deprecation('api-platform/core', '2.7', sprintf('Use "%s" instead of "%s".', ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
  33. }
  34. }
  35. public function createFromRequest(Request $request, bool $normalization, array $attributes = null): array
  36. {
  37. if (null === $attributes && !$attributes = RequestAttributesExtractor::extractAttributes($request)) {
  38. throw new RuntimeException('Request attributes are not valid.');
  39. }
  40. // TODO remove call to getContentType() when requiring symfony/http-foundation ≥ 6.2
  41. $contentTypeFormat = method_exists($request, 'getContentTypeFormat')
  42. ? $request->getContentTypeFormat()
  43. : $request->getContentType();
  44. // TODO: 3.0 change the condition to remove the ResourceMetadataFactorym only used to skip null values
  45. if (
  46. $this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface
  47. && (isset($attributes['operation_name']) || isset($attributes['operation']))
  48. ) {
  49. $operation = $attributes['operation'] ?? $this->resourceMetadataFactory->create($attributes['resource_class'])->getOperation($attributes['operation_name']);
  50. $context = $normalization ? ($operation->getNormalizationContext() ?? []) : ($operation->getDenormalizationContext() ?? []);
  51. $context['operation_name'] = $operation->getName();
  52. $context['operation'] = $operation;
  53. $context['resource_class'] = $attributes['resource_class'];
  54. // TODO: 3.0 becomes true by default
  55. $context['skip_null_values'] = $context['skip_null_values'] ?? $this->shouldSkipNullValues($attributes['resource_class'], $context['operation_name']);
  56. // TODO: remove in 3.0, operation type will not exist anymore
  57. $context['operation_type'] = $operation instanceof CollectionOperationInterface ? OperationType::COLLECTION : OperationType::ITEM;
  58. $context['iri_only'] = $context['iri_only'] ?? false;
  59. $context['request_uri'] = $request->getRequestUri();
  60. $context['uri'] = $request->getUri();
  61. $context['input'] = $operation->getInput();
  62. $context['output'] = $operation->getOutput();
  63. // Special case as this is usually handled by our OperationContextTrait, here we want to force the IRI in the response
  64. if (!$operation instanceof CollectionOperationInterface && method_exists($operation, 'getItemUriTemplate') && $operation->getItemUriTemplate()) {
  65. $context['item_uri_template'] = $operation->getItemUriTemplate();
  66. }
  67. $context['types'] = $operation->getTypes();
  68. $context['uri_variables'] = [];
  69. foreach (array_keys($operation->getUriVariables() ?? []) as $parameterName) {
  70. $context['uri_variables'][$parameterName] = $request->attributes->get($parameterName);
  71. }
  72. if (!$normalization) {
  73. if (!isset($context['api_allow_update'])) {
  74. $context['api_allow_update'] = \in_array($method = $request->getMethod(), ['PUT', 'PATCH'], true);
  75. if ($context['api_allow_update'] && 'PATCH' === $method) {
  76. $context['deep_object_to_populate'] = $context['deep_object_to_populate'] ?? true;
  77. }
  78. }
  79. if ('csv' === $contentTypeFormat) {
  80. $context[CsvEncoder::AS_COLLECTION_KEY] = false;
  81. }
  82. }
  83. return $context;
  84. }
  85. /** @var ResourceMetadata $resourceMetadata */
  86. $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
  87. $key = $normalization ? 'normalization_context' : 'denormalization_context';
  88. if (isset($attributes['collection_operation_name'])) {
  89. $operationKey = 'collection_operation_name';
  90. $operationType = OperationType::COLLECTION;
  91. } elseif (isset($attributes['item_operation_name'])) {
  92. $operationKey = 'item_operation_name';
  93. $operationType = OperationType::ITEM;
  94. } else {
  95. $operationKey = 'subresource_operation_name';
  96. $operationType = OperationType::SUBRESOURCE;
  97. }
  98. $context = $resourceMetadata->getTypedOperationAttribute($operationType, $attributes[$operationKey], $key, [], true);
  99. $context['operation_type'] = $operationType;
  100. $context[$operationKey] = $attributes[$operationKey];
  101. $context['iri_only'] = $resourceMetadata->getAttribute('normalization_context')['iri_only'] ?? false;
  102. $context['input'] = $resourceMetadata->getTypedOperationAttribute($operationType, $attributes[$operationKey], 'input', null, true);
  103. $context['output'] = $resourceMetadata->getTypedOperationAttribute($operationType, $attributes[$operationKey], 'output', null, true);
  104. if (!$normalization) {
  105. if (!isset($context['api_allow_update'])) {
  106. $context['api_allow_update'] = \in_array($method = $request->getMethod(), ['PUT', 'PATCH'], true);
  107. if ($context['api_allow_update'] && 'PATCH' === $method) {
  108. $context['deep_object_to_populate'] = $context['deep_object_to_populate'] ?? true;
  109. }
  110. }
  111. if ('csv' === $contentTypeFormat) {
  112. $context[CsvEncoder::AS_COLLECTION_KEY] = false;
  113. }
  114. }
  115. $context['resource_class'] = $attributes['resource_class'];
  116. $context['request_uri'] = $request->getRequestUri();
  117. $context['uri'] = $request->getUri();
  118. if (isset($attributes['subresource_context'])) {
  119. $context['subresource_identifiers'] = [];
  120. foreach ($attributes['subresource_context']['identifiers'] as $parameterName => [$resourceClass]) {
  121. if (!isset($context['subresource_resources'][$resourceClass])) {
  122. $context['subresource_resources'][$resourceClass] = [];
  123. }
  124. $context['subresource_identifiers'][$parameterName] = $context['subresource_resources'][$resourceClass][$parameterName] = $request->attributes->get($parameterName);
  125. }
  126. }
  127. if (isset($attributes['subresource_property'])) {
  128. $context['subresource_property'] = $attributes['subresource_property'];
  129. $context['subresource_resource_class'] = $attributes['subresource_resource_class'] ?? null;
  130. }
  131. unset($context[DocumentationNormalizer::SWAGGER_DEFINITION_NAME]);
  132. if (isset($context['skip_null_values'])) {
  133. return $context;
  134. }
  135. // TODO: We should always use `skip_null_values` but changing this would be a BC break, for now use it only when `merge-patch+json` is activated on a Resource
  136. if (!$this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  137. foreach ($resourceMetadata->getItemOperations() as $operation) {
  138. if ('PATCH' === ($operation['method'] ?? '') && \in_array('application/merge-patch+json', $operation['input_formats']['json'] ?? [], true)) {
  139. $context['skip_null_values'] = true;
  140. break;
  141. }
  142. }
  143. } else {
  144. $context['skip_null_values'] = $this->shouldSkipNullValues($attributes['resource_class'], $attributes['operation_name']);
  145. }
  146. return $context;
  147. }
  148. /**
  149. * TODO: remove in 3.0, this will have no impact and skip_null_values will be default, no more resourceMetadataFactory call in this class.
  150. */
  151. private function shouldSkipNullValues(string $class, string $operationName): bool
  152. {
  153. if (!$this->resourceMetadataFactory) {
  154. return false;
  155. }
  156. $collection = $this->resourceMetadataFactory->create($class);
  157. foreach ($collection as $metadata) {
  158. foreach ($metadata->getOperations() as $operation) {
  159. if ('PATCH' === ($operation->getMethod() ?? '') && \in_array('application/merge-patch+json', $operation->getInputFormats()['json'] ?? [], true)) {
  160. return true;
  161. }
  162. }
  163. }
  164. return false;
  165. }
  166. }
  167. class_alias(SerializerContextBuilder::class, \ApiPlatform\Core\Serializer\SerializerContextBuilder::class);