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

api-platform / core / 14855288206

06 May 2025 08:38AM UTC coverage: 8.457% (-0.002%) from 8.459%
14855288206

push

github

soyuka
fix: type info deprecations to baseline

bc layer covered on main

13397 of 158416 relevant lines covered (8.46%)

22.88 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\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
    }
1,723✔
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())) {
1,700✔
47
            throw new RuntimeException('Request attributes are not valid.');
×
48
        }
49

50
        if (!($operation = $attributes['operation'] ?? null)) {
1,700✔
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() ?? []);
1,700✔
59
        $context['operation_name'] = $operation->getName();
1,700✔
60
        $context['operation'] = $operation;
1,700✔
61
        $context['resource_class'] = $attributes['resource_class'];
1,700✔
62
        $context['skip_null_values'] ??= true;
1,700✔
63
        $context['iri_only'] ??= false;
1,700✔
64
        $context['request_uri'] = $request->getRequestUri();
1,700✔
65
        $context['uri'] = $request->getUri();
1,700✔
66
        $context['input'] = $operation->getInput();
1,700✔
67
        $context['output'] = $operation->getOutput();
1,700✔
68

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

74
        if ($types = $operation->getTypes()) {
1,700✔
75
            $context['types'] = $types;
31✔
76
        }
77

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

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

87
        if (null === $context['output'] && $this->getStateOptionsClass($operation)) {
1,700✔
88
            $context['force_resource_class'] = $operation->getClass();
16✔
89
        }
90

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

96
            $context['groups'][] = 'trace';
216✔
97
        }
98

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

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

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

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

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

130
        return $context;
1,700✔
131
    }
132
}
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