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

api-platform / core / 13200284839

07 Feb 2025 12:56PM UTC coverage: 0.0% (-8.2%) from 8.164%
13200284839

Pull #6952

github

web-flow
Merge 519fbf8cc into 62377f880
Pull Request #6952: fix: errors retrieval and documentation

0 of 206 new or added lines in 17 files covered. (0.0%)

10757 existing lines in 366 files now uncovered.

0 of 47781 relevant lines covered (0.0%)

0.0 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
128
        return $context;
×
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