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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

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

88.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
    {
37
    }
1,008✔
38

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

48
        if (!($operation = $attributes['operation'] ?? null)) {
988✔
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

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

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

72
        if ($types = $operation->getTypes()) {
988✔
UNCOV
73
            $context['types'] = $types;
13✔
74
        }
75

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

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

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

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

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

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

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

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

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

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

128
        return $context;
988✔
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