• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 14008635868

22 Mar 2025 12:39PM UTC coverage: 8.52% (+0.005%) from 8.515%
14008635868

Pull #7042

github

web-flow
Merge fdd88ef56 into 47a6dffbb
Pull Request #7042: Purge parent collections in inheritance cases

4 of 9 new or added lines in 1 file covered. (44.44%)

540 existing lines in 57 files now uncovered.

13394 of 157210 relevant lines covered (8.52%)

22.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/src/Serializer/SerializerContextBuilder.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Serializer;
15

16
use ApiPlatform\Doctrine\Orm\State\Options;
17
use ApiPlatform\Metadata\CollectionOperationInterface;
18
use ApiPlatform\Metadata\Error as ErrorOperation;
19
use ApiPlatform\Metadata\Exception\RuntimeException;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Util\AttributesExtractor;
22
use ApiPlatform\State\SerializerContextBuilderInterface;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\Serializer\Encoder\CsvEncoder;
25
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
26
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
27

28
/**
29
 * {@inheritdoc}
30
 *
31
 * @author Kévin Dunglas <dunglas@gmail.com>
32
 */
33
final class SerializerContextBuilder implements SerializerContextBuilderInterface
34
{
35
    public function __construct(private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, private readonly bool $debug = false)
36
    {
37
    }
1,715✔
38

39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function createFromRequest(Request $request, bool $normalization, ?array $attributes = null): array
43
    {
44
        if (null === $attributes && !$attributes = AttributesExtractor::extractAttributes($request->attributes->all())) {
1,696✔
45
            throw new RuntimeException('Request attributes are not valid.');
×
46
        }
47

48
        if (!($operation = $attributes['operation'] ?? null)) {
1,696✔
49
            if (!$this->resourceMetadataFactory) {
×
50
                throw new RuntimeException('No operation');
×
51
            }
52

53
            $operation = $this->resourceMetadataFactory->create($attributes['resource_class'])->getOperation($attributes['operation_name'] ?? null);
×
54
        }
55

56
        $context = $normalization ? ($operation->getNormalizationContext() ?? []) : ($operation->getDenormalizationContext() ?? []);
1,696✔
57
        $context['operation_name'] = $operation->getName();
1,696✔
58
        $context['operation'] = $operation;
1,696✔
59
        $context['resource_class'] = $attributes['resource_class'];
1,696✔
60
        $context['skip_null_values'] ??= true;
1,696✔
61
        $context['iri_only'] ??= false;
1,696✔
62
        $context['request_uri'] = $request->getRequestUri();
1,696✔
63
        $context['uri'] = $request->getUri();
1,696✔
64
        $context['input'] = $operation->getInput();
1,696✔
65
        $context['output'] = $operation->getOutput();
1,696✔
66

67
        // Special case as this is usually handled by our OperationContextTrait, here we want to force the IRI in the response
68
        if (!$operation instanceof CollectionOperationInterface && method_exists($operation, 'getItemUriTemplate') && $operation->getItemUriTemplate()) {
1,696✔
69
            $context['item_uri_template'] = $operation->getItemUriTemplate();
4✔
70
        }
71

72
        if ($types = $operation->getTypes()) {
1,696✔
73
            $context['types'] = $types;
31✔
74
        }
75

76
        // TODO: remove this as uri variables are available in the SerializerProcessor but correctly parsed
77
        if ($operation->getUriVariables()) {
1,696✔
78
            $context['uri_variables'] = [];
679✔
79

80
            foreach (array_keys($operation->getUriVariables()) as $parameterName) {
679✔
81
                $context['uri_variables'][$parameterName] = $request->attributes->get($parameterName);
679✔
82
            }
83
        }
84

85
        if (null === $context['output'] && ($options = $operation?->getStateOptions()) && class_exists(Options::class) && $options instanceof Options && $options->getEntityClass()) {
1,696✔
86
            $context['force_resource_class'] = $operation->getClass();
13✔
87
        }
88

89
        if ($this->debug && isset($context['groups']) && $operation instanceof ErrorOperation) {
1,696✔
90
            if (!\is_array($context['groups'])) {
214✔
91
                $context['groups'] = (array) $context['groups'];
×
92
            }
93

94
            $context['groups'][] = 'trace';
214✔
95
        }
96

97
        if (!$normalization) {
1,696✔
98
            if (!isset($context['api_allow_update'])) {
428✔
99
                $context['api_allow_update'] = \in_array($method = $request->getMethod(), ['PUT', 'PATCH'], true);
428✔
100

101
                if ($context['api_allow_update'] && 'PATCH' === $method) {
428✔
102
                    $context['deep_object_to_populate'] ??= true;
21✔
103
                }
104
            }
105

106
            if ('csv' === (method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType())) {
428✔
107
                $context[CsvEncoder::AS_COLLECTION_KEY] = false;
2✔
108
            }
109
        }
110
        if ($operation->getCollectDenormalizationErrors() ?? false) {
1,696✔
UNCOV
111
            $context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS] = true;
1✔
112
        }
113

114
        // to keep the cache computation smaller, we have "operation_name" and "iri" anyways
115
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'root_operation';
1,696✔
116
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'operation';
1,696✔
117
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'object';
1,696✔
118
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'data';
1,696✔
119
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'property_metadata';
1,696✔
120
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'circular_reference_limit_counters';
1,696✔
121
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'debug_trace_id';
1,696✔
122

123
        // JSON API see JsonApiProvider
124
        if ($included = $request->attributes->get('_api_included')) {
1,696✔
125
            $context['api_included'] = $included;
32✔
126
        }
127

128
        return $context;
1,696✔
129
    }
130
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc