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

api-platform / core / 17487610263

05 Sep 2025 08:12AM UTC coverage: 22.608% (+0.3%) from 22.319%
17487610263

push

github

web-flow
chore: remove @experimental flag from parameters (#7357)

12049 of 53296 relevant lines covered (22.61%)

26.21 hits per line

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

82.35
/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\Metadata\CollectionOperationInterface;
17
use ApiPlatform\Metadata\Error as ErrorOperation;
18
use ApiPlatform\Metadata\Exception\RuntimeException;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Metadata\Util\AttributesExtractor;
21
use ApiPlatform\State\SerializerContextBuilderInterface;
22
use ApiPlatform\State\Util\StateOptionsTrait;
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
    use StateOptionsTrait;
36

37
    public function __construct(private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, private readonly bool $debug = false)
38
    {
39
    }
635✔
40

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

50
        if (!($operation = $attributes['operation'] ?? null)) {
616✔
51
            if (!$this->resourceMetadataFactory) {
×
52
                throw new RuntimeException('No operation');
×
53
            }
54

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

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

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

75
        if ($types = $operation->getTypes()) {
616✔
76
            $context['types'] = $types;
×
77
        }
78

79
        // TODO: remove this as uri variables are available in the SerializerProcessor but correctly parsed
80
        if ($operation->getUriVariables()) {
616✔
81
            $context['uri_variables'] = [];
220✔
82

83
            foreach (array_keys($operation->getUriVariables()) as $parameterName) {
220✔
84
                $context['uri_variables'][$parameterName] = $request->attributes->get($parameterName);
220✔
85
            }
86
        }
87

88
        if (null === $context['output'] && $this->getStateOptionsClass($operation)) {
616✔
89
            $context['force_resource_class'] = $operation->getClass();
12✔
90
        }
91

92
        if ($this->debug && isset($context['groups']) && $operation instanceof ErrorOperation) {
616✔
93
            if (!\is_array($context['groups'])) {
102✔
94
                $context['groups'] = (array) $context['groups'];
×
95
            }
96

97
            $context['groups'][] = 'trace';
102✔
98
        }
99

100
        if (!$normalization) {
616✔
101
            if (!isset($context['api_allow_update'])) {
30✔
102
                $context['api_allow_update'] = \in_array($method = $request->getMethod(), ['PUT', 'PATCH'], true);
30✔
103

104
                if ($context['api_allow_update'] && 'PATCH' === $method) {
30✔
105
                    $context['deep_object_to_populate'] ??= true;
4✔
106
                }
107
            }
108

109
            if ('csv' === (method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType())) {
30✔
110
                $context[CsvEncoder::AS_COLLECTION_KEY] = false;
×
111
            }
112
        }
113
        if ($operation->getCollectDenormalizationErrors() ?? false) {
616✔
114
            $context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS] = true;
2✔
115
        }
116

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

126
        // JSON API see JsonApiProvider
127
        if ($included = $request->attributes->get('_api_included')) {
616✔
128
            $context['api_included'] = $included;
×
129
        }
130

131
        return $context;
616✔
132
    }
133
}
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