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

api-platform / core / 13814792797

12 Mar 2025 03:09PM UTC coverage: 5.889% (-1.4%) from 7.289%
13814792797

Pull #7012

github

web-flow
Merge 199d44919 into 284937039
Pull Request #7012: doc: comment typo in ApiResource.php

10048 of 170615 relevant lines covered (5.89%)

5.17 hits per line

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

78.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
    }
363✔
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())) {
348✔
45
            throw new RuntimeException('Request attributes are not valid.');
×
46
        }
47

48
        if (!($operation = $attributes['operation'] ?? null)) {
348✔
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() ?? []);
348✔
57
        $context['operation_name'] = $operation->getName();
348✔
58
        $context['operation'] = $operation;
348✔
59
        $context['resource_class'] = $attributes['resource_class'];
348✔
60
        $context['skip_null_values'] ??= true;
348✔
61
        $context['iri_only'] ??= false;
348✔
62
        $context['request_uri'] = $request->getRequestUri();
348✔
63
        $context['uri'] = $request->getUri();
348✔
64
        $context['input'] = $operation->getInput();
348✔
65
        $context['output'] = $operation->getOutput();
348✔
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()) {
348✔
69
            $context['item_uri_template'] = $operation->getItemUriTemplate();
×
70
        }
71

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

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

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

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

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

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

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

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

106
            if ('csv' === (method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType())) {
21✔
107
                $context[CsvEncoder::AS_COLLECTION_KEY] = false;
×
108
            }
109
        }
110
        if ($operation->getCollectDenormalizationErrors() ?? false) {
348✔
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';
348✔
116
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'operation';
348✔
117
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'object';
348✔
118
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'data';
348✔
119
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'property_metadata';
348✔
120
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'circular_reference_limit_counters';
348✔
121
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'debug_trace_id';
348✔
122

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

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