• 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

75.61
/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\AdvancedNameConverterInterface;
21
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
22
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
23
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
24

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

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

40
        if (isset($resolverContext['fields'])) {
27✔
41
            $context['no_resolver_data'] = true;
×
42
        }
43

44
        $context['operation'] = $operation;
27✔
45
        if ($operation->getInput()) {
27✔
46
            $context['input'] = $operation->getInput();
×
47
        }
48
        if ($operation->getOutput()) {
27✔
49
            $context['output'] = $operation->getOutput();
×
50
        }
51
        $context = $normalization ? array_merge($operation->getNormalizationContext() ?? [], $context) : array_merge($operation->getDenormalizationContext() ?? [], $context);
27✔
52

53
        if ($normalization) {
27✔
54
            $context['attributes'] = $this->fieldsToAttributes($resourceClass, $operation, $resolverContext, $context);
27✔
55
        }
56

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

66
        return $context;
27✔
67
    }
68

69
    /**
70
     * 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).
71
     */
72
    private function fieldsToAttributes(?string $resourceClass, Operation $operation, array $resolverContext, array $context): array
73
    {
74
        if (isset($resolverContext['fields'])) {
27✔
75
            $fields = $resolverContext['fields'];
×
76
        } else {
77
            /** @var ResolveInfo $info */
78
            $info = $resolverContext['info'];
27✔
79
            $fields = $info->getFieldSelection(\PHP_INT_MAX);
27✔
80
        }
81

82
        $attributes = $this->replaceIdKeys($fields['edges']['node'] ?? $fields['collection'] ?? $fields, $resourceClass, $context);
27✔
83

84
        if ($operation instanceof Subscription || $operation instanceof Mutation) {
27✔
85
            $wrapFieldName = lcfirst($operation->getShortName());
×
86

87
            return $attributes[$wrapFieldName] ?? [];
×
88
        }
89

90
        return $attributes;
27✔
91
    }
92

93
    private function replaceIdKeys(array $fields, ?string $resourceClass, array $context): array
94
    {
95
        $denormalizedFields = [];
27✔
96

97
        foreach ($fields as $key => $value) {
27✔
98
            if ('_id' === $key) {
27✔
99
                $denormalizedFields['id'] = $fields['_id'];
×
100

101
                continue;
×
102
            }
103

104
            $denormalizedFields[$this->denormalizePropertyName((string) $key, $resourceClass, $context)] = \is_array($value) ? $this->replaceIdKeys($value, $resourceClass, $context) : $value;
27✔
105
        }
106

107
        return $denormalizedFields;
27✔
108
    }
109

110
    private function denormalizePropertyName(string $property, ?string $resourceClass, array $context): string
111
    {
112
        if (null === $this->nameConverter) {
27✔
113
            return $property;
×
114
        }
115
        if ($this->nameConverter instanceof AdvancedNameConverterInterface || $this->nameConverter instanceof MetadataAwareNameConverter) {
27✔
116
            return $this->nameConverter->denormalize($property, $resourceClass, null, $context);
27✔
117
        }
118

119
        return $this->nameConverter->denormalize($property);
×
120
    }
121
}
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