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

api-platform / core / 6067528200

04 Sep 2023 12:12AM UTC coverage: 36.875% (-21.9%) from 58.794%
6067528200

Pull #5791

github

web-flow
Merge 64157e578 into d09cfc9d2
Pull Request #5791: fix: strip down any sql function name

3096 of 3096 new or added lines in 205 files covered. (100.0%)

9926 of 26918 relevant lines covered (36.87%)

6.5 hits per line

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

2.94
/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\NameConverterInterface;
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
    {
32
    }
18✔
33

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

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

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

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

55
        return $context;
×
56
    }
57

58
    /**
59
     * 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).
60
     */
61
    private function fieldsToAttributes(?string $resourceClass, Operation $operation, array $resolverContext, array $context): array
62
    {
63
        if (isset($resolverContext['fields'])) {
×
64
            $fields = $resolverContext['fields'];
×
65
        } else {
66
            /** @var ResolveInfo $info */
67
            $info = $resolverContext['info'];
×
68
            $fields = $info->getFieldSelection(\PHP_INT_MAX);
×
69
        }
70

71
        $attributes = $this->replaceIdKeys($fields['edges']['node'] ?? $fields['collection'] ?? $fields, $resourceClass, $context);
×
72

73
        if ($operation instanceof Subscription || $operation instanceof Mutation) {
×
74
            $wrapFieldName = lcfirst($operation->getShortName());
×
75

76
            return $attributes[$wrapFieldName] ?? [];
×
77
        }
78

79
        return $attributes;
×
80
    }
81

82
    private function replaceIdKeys(array $fields, ?string $resourceClass, array $context): array
83
    {
84
        $denormalizedFields = [];
×
85

86
        foreach ($fields as $key => $value) {
×
87
            if ('_id' === $key) {
×
88
                $denormalizedFields['id'] = $fields['_id'];
×
89

90
                continue;
×
91
            }
92

93
            $denormalizedFields[$this->denormalizePropertyName((string) $key, $resourceClass, $context)] = \is_array($value) ? $this->replaceIdKeys($value, $resourceClass, $context) : $value;
×
94
        }
95

96
        return $denormalizedFields;
×
97
    }
98

99
    private function denormalizePropertyName(string $property, ?string $resourceClass, array $context): string
100
    {
101
        if (null === $this->nameConverter) {
×
102
            return $property;
×
103
        }
104
        if ($this->nameConverter instanceof AdvancedNameConverterInterface) {
×
105
            return $this->nameConverter->denormalize($property, $resourceClass, null, $context);
×
106
        }
107

108
        return $this->nameConverter->denormalize($property);
×
109
    }
110
}
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