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

api-platform / core / 17585586101

09 Sep 2025 02:16PM UTC coverage: 0.0% (-22.6%) from 22.592%
17585586101

push

github

web-flow
fix(state): transform uri variable using ReadLinkParameterProvider (#7375)

0 of 4 new or added lines in 1 file covered. (0.0%)

12247 existing lines in 411 files now uncovered.

0 of 52798 relevant lines covered (0.0%)

0.0 hits per line

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

0.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\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
    {
UNCOV
39
    }
×
40

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

UNCOV
50
        if (!($operation = $attributes['operation'] ?? null)) {
×
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

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

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

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

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

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

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

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

UNCOV
97
            $context['groups'][] = 'trace';
×
98
        }
99

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

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

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

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

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

UNCOV
131
        return $context;
×
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