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

api-platform / core / 10884379752

16 Sep 2024 01:01PM UTC coverage: 7.281% (-0.4%) from 7.672%
10884379752

push

github

soyuka
Merge 3.4

0 of 100 new or added lines in 7 files covered. (0.0%)

5332 existing lines in 181 files now uncovered.

11994 of 164725 relevant lines covered (7.28%)

9.52 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 Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\Serializer\Encoder\CsvEncoder;
24
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
25
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
26

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

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

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

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

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

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

UNCOV
71
        if ($types = $operation->getTypes()) {
706✔
UNCOV
72
            $context['types'] = $types;
87✔
73
        }
74

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

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

UNCOV
84
        if (null === $context['output'] && ($options = $operation?->getStateOptions()) && class_exists(Options::class) && $options instanceof Options && $options->getEntityClass()) {
706✔
85
            $context['force_resource_class'] = $operation->getClass();
9✔
86
        }
87

UNCOV
88
        if ($this->debug && isset($context['groups']) && $operation instanceof ErrorOperation) {
706✔
UNCOV
89
            if (!\is_array($context['groups'])) {
96✔
90
                $context['groups'] = (array) $context['groups'];
×
91
            }
92

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

UNCOV
96
        if (!$normalization) {
706✔
UNCOV
97
            if (!isset($context['api_allow_update'])) {
235✔
UNCOV
98
                $context['api_allow_update'] = \in_array($method = $request->getMethod(), ['PUT', 'PATCH'], true);
235✔
99

UNCOV
100
                if ($context['api_allow_update'] && 'PATCH' === $method) {
235✔
UNCOV
101
                    $context['deep_object_to_populate'] ??= true;
12✔
102
                }
103
            }
104

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

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

122
        // JSON API see JsonApiProvider
UNCOV
123
        if ($included = $request->attributes->get('_api_included')) {
706✔
UNCOV
124
            $context['api_included'] = $included;
16✔
125
        }
126

UNCOV
127
        return $context;
706✔
128
    }
129
}
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