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

api-platform / core / 17054069864

18 Aug 2025 10:27PM UTC coverage: 21.952% (+0.2%) from 21.769%
17054069864

Pull #7151

github

web-flow
Merge 0da010d8d into 6491bfc7a
Pull Request #7151: fix: 7119 parameter array shape uses invalid syntax

11524 of 52497 relevant lines covered (21.95%)

11.86 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
    }
286✔
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())) {
284✔
47
            throw new RuntimeException('Request attributes are not valid.');
×
48
        }
49

50
        if (!($operation = $attributes['operation'] ?? null)) {
284✔
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() ?? []);
284✔
59
        $context['operation_name'] = $operation->getName();
284✔
60
        $context['operation'] = $operation;
284✔
61
        $context['resource_class'] = $attributes['resource_class'];
284✔
62
        $context['skip_null_values'] ??= true;
284✔
63
        $context[AbstractItemNormalizer::SKIP_NULL_TO_ONE_RELATIONS] ??= true;
284✔
64
        $context['iri_only'] ??= false;
284✔
65
        $context['request_uri'] = $request->getRequestUri();
284✔
66
        $context['uri'] = $request->getUri();
284✔
67
        $context['input'] = $operation->getInput();
284✔
68
        $context['output'] = $operation->getOutput();
284✔
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()) {
284✔
72
            $context['item_uri_template'] = $operation->getItemUriTemplate();
×
73
        }
74

75
        if ($types = $operation->getTypes()) {
284✔
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()) {
284✔
81
            $context['uri_variables'] = [];
106✔
82

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

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

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

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

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

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

109
            if ('csv' === (method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType())) {
13✔
110
                $context[CsvEncoder::AS_COLLECTION_KEY] = false;
×
111
            }
112
        }
113
        if ($operation->getCollectDenormalizationErrors() ?? false) {
284✔
114
            $context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS] = true;
1✔
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';
284✔
119
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'operation';
284✔
120
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'object';
284✔
121
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'data';
284✔
122
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'property_metadata';
284✔
123
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'circular_reference_limit_counters';
284✔
124
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'debug_trace_id';
284✔
125

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

131
        return $context;
284✔
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