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

api-platform / core / 11271860720

10 Oct 2024 10:04AM UTC coverage: 7.84% (+0.007%) from 7.833%
11271860720

push

github

soyuka
Merge 3.4

0 of 34 new or added lines in 5 files covered. (0.0%)

11160 existing lines in 381 files now uncovered.

12955 of 165237 relevant lines covered (7.84%)

27.06 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 ApiPlatform\State\SerializerContextBuilderInterface;
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
    public function __construct(private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, private readonly bool $debug = false)
36
    {
UNCOV
37
    }
2,190✔
38

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

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

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

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

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

UNCOV
72
        if ($types = $operation->getTypes()) {
2,166✔
UNCOV
73
            $context['types'] = $types;
44✔
74
        }
75

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

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

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

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

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

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

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

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

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

123
        // JSON API see JsonApiProvider
UNCOV
124
        if ($included = $request->attributes->get('_api_included')) {
2,166✔
UNCOV
125
            $context['api_included'] = $included;
48✔
126
        }
127

UNCOV
128
        return $context;
2,166✔
129
    }
130
}
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