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

api-platform / core / 20847864477

09 Jan 2026 09:47AM UTC coverage: 29.1% (+0.005%) from 29.095%
20847864477

Pull #7649

github

web-flow
Merge b342dd5db into d640d106b
Pull Request #7649: feat(validator): uuid/ulid parameter validation

0 of 4 new or added lines in 1 file covered. (0.0%)

15050 existing lines in 491 files now uncovered.

16996 of 58406 relevant lines covered (29.1%)

81.8 hits per line

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

97.44
/src/GraphQl/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\GraphQl\Serializer;
15

16
use ApiPlatform\Metadata\GraphQl\Mutation;
17
use ApiPlatform\Metadata\GraphQl\Operation;
18
use ApiPlatform\Metadata\GraphQl\Subscription;
19
use GraphQL\Type\Definition\ResolveInfo;
20
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
21
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
22

23
/**
24
 * Builds the context used by the Symfony Serializer.
25
 *
26
 * @author Alan Poulain <contact@alanpoulain.eu>
27
 */
28
final class SerializerContextBuilder implements SerializerContextBuilderInterface
29
{
30
    public function __construct(private readonly ?NameConverterInterface $nameConverter)
31
    {
UNCOV
32
    }
1,337✔
33

34
    public function create(?string $resourceClass, Operation $operation, array $resolverContext, bool $normalization): array
35
    {
UNCOV
36
        $context = ['resource_class' => $resourceClass, 'operation_name' => $operation->getName(), 'graphql_operation_name' => $operation->getName()];
257✔
37

UNCOV
38
        if (isset($resolverContext['fields'])) {
257✔
39
            $context['no_resolver_data'] = true;
2✔
40
        }
41

UNCOV
42
        $context['operation'] = $operation;
257✔
UNCOV
43
        if ($operation->getInput()) {
257✔
44
            $context['input'] = $operation->getInput();
10✔
45
        }
UNCOV
46
        if ($operation->getOutput()) {
257✔
47
            $context['output'] = $operation->getOutput();
8✔
48
        }
UNCOV
49
        $context = $normalization ? array_merge($operation->getNormalizationContext() ?? [], $context) : array_merge($operation->getDenormalizationContext() ?? [], $context);
257✔
50

UNCOV
51
        if ($normalization) {
257✔
UNCOV
52
            $context['attributes'] = $this->fieldsToAttributes($resourceClass, $operation, $resolverContext, $context);
257✔
53
        }
54

55
        // to keep the cache computation smaller, we have "operation_name" and "iri" anyways
UNCOV
56
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'root_operation';
257✔
UNCOV
57
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'operation';
257✔
UNCOV
58
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'object';
257✔
UNCOV
59
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'data';
257✔
UNCOV
60
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'property_metadata';
257✔
UNCOV
61
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'circular_reference_limit_counters';
257✔
UNCOV
62
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'debug_trace_id';
257✔
63

UNCOV
64
        return $context;
257✔
65
    }
66

67
    /**
68
     * Retrieves fields, recursively replaces the "_id" key (the raw id) by "id" (the name of the property expected by the Serializer) and flattens edge and node structures (pagination).
69
     */
70
    private function fieldsToAttributes(?string $resourceClass, Operation $operation, array $resolverContext, array $context): array
71
    {
UNCOV
72
        if (isset($resolverContext['fields'])) {
257✔
73
            $fields = $resolverContext['fields'];
2✔
74
        } else {
75
            /** @var ResolveInfo $info */
UNCOV
76
            $info = $resolverContext['info'];
255✔
UNCOV
77
            $fields = $info->getFieldSelection(\PHP_INT_MAX);
255✔
78
        }
79

UNCOV
80
        $attributes = $this->replaceIdKeys($fields['edges']['node'] ?? $fields['collection'] ?? $fields, $resourceClass, $context);
257✔
81

UNCOV
82
        if ($operation instanceof Subscription || $operation instanceof Mutation) {
257✔
UNCOV
83
            $wrapFieldName = lcfirst($operation->getShortName());
75✔
84

UNCOV
85
            return $attributes[$wrapFieldName] ?? [];
75✔
86
        }
87

UNCOV
88
        return $attributes;
195✔
89
    }
90

91
    private function replaceIdKeys(array $fields, ?string $resourceClass, array $context): array
92
    {
UNCOV
93
        $denormalizedFields = [];
257✔
94

UNCOV
95
        foreach ($fields as $key => $value) {
257✔
UNCOV
96
            if ('_id' === $key) {
257✔
97
                $denormalizedFields['id'] = $fields['_id'];
9✔
98

99
                continue;
9✔
100
            }
101

UNCOV
102
            $denormalizedFields[$this->denormalizePropertyName((string) $key, $resourceClass, $context)] = \is_array($value) ? $this->replaceIdKeys($value, $resourceClass, $context) : $value;
255✔
103
        }
104

UNCOV
105
        return $denormalizedFields;
257✔
106
    }
107

108
    private function denormalizePropertyName(string $property, ?string $resourceClass, array $context): string
109
    {
UNCOV
110
        if (null === $this->nameConverter) {
255✔
111
            return $property;
×
112
        }
113

UNCOV
114
        return $this->nameConverter->denormalize($property, $resourceClass, null, $context);
255✔
115
    }
116
}
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

© 2026 Coveralls, Inc