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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

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

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

96.0
/src/Serializer/Filter/GroupFilter.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\Filter;
15

16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
18

19
/**
20
 * The group filter allows you to filter by serialization groups.
21
 *
22
 * Syntax: `?groups[]=<group>`.
23
 *
24
 * You can add as many groups as you need.
25
 *
26
 * Three arguments are available to configure the filter:
27
 * - `parameterName` is the query parameter name (default: `groups`)
28
 * - `overrideDefaultGroups` allows to override the default serialization groups (default: `false`)
29
 * - `whitelist` groups whitelist to avoid uncontrolled data exposure (default: `null` to allow all groups)
30
 *
31
 * <div data-code-selector>
32
 *
33
 * ```php
34
 * <?php
35
 * // api/src/Entity/Book.php
36
 * use ApiPlatform\Metadata\ApiFilter;
37
 * use ApiPlatform\Metadata\ApiResource;
38
 * use ApiPlatform\Serializer\Filter\GroupFilter;
39
 *
40
 * #[ApiResource]
41
 * #[ApiFilter(GroupFilter::class, arguments: ['parameterName' => 'groups', 'overrideDefaultGroups' => false, 'whitelist' => ['allowed_group']])]
42
 * class Book
43
 * {
44
 *     // ...
45
 * }
46
 * ```
47
 *
48
 * ```yaml
49
 * # config/services.yaml
50
 * services:
51
 *     book.group_filter:
52
 *         parent: 'api_platform.serializer.group_filter'
53
 *         arguments: [ $parameterName: 'groups', $overrideDefaultGroups: false, $whitelist: ['allowed_group'] ]
54
 *         tags:  [ 'api_platform.filter' ]
55
 *         # The following are mandatory only if a _defaults section is defined with inverted values.
56
 *         # You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
57
 *         autowire: false
58
 *         autoconfigure: false
59
 *         public: false
60
 *
61
 * # api/config/api_platform/resources.yaml
62
 * resources:
63
 *     App\Entity\Book:
64
 *         - operations:
65
 *               ApiPlatform\Metadata\GetCollection:
66
 *                   filters: ['book.group_filter']
67
 * ```
68
 *
69
 * ```xml
70
 * <?xml version="1.0" encoding="UTF-8" ?>
71
 * <!-- api/config/services.xml -->
72
 * <?xml version="1.0" encoding="UTF-8" ?>
73
 * <container
74
 *         xmlns="http://symfony.com/schema/dic/services"
75
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
76
 *         xsi:schemaLocation="http://symfony.com/schema/dic/services
77
 *         https://symfony.com/schema/dic/services/services-1.0.xsd">
78
 *     <services>
79
 *         <service id="book.group_filter" parent="api_platform.serializer.group_filter">
80
 *             <argument key="parameterName">groups</argument>
81
 *             <argument key="overrideDefaultGroups">false</argument>
82
 *             <argument key="whitelist" type="collection">
83
 *                 <argument>allowed_group</argument>
84
 *             </argument>
85
 *             <tag name="api_platform.filter"/>
86
 *         </service>
87
 *     </services>
88
 * </container>
89
 * <!-- api/config/api_platform/resources.xml -->
90
 * <resources
91
 *         xmlns="https://api-platform.com/schema/metadata/resources-3.0"
92
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
93
 *         xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
94
 *         https://api-platform.com/schema/metadata/resources-3.0.xsd">
95
 *     <resource class="App\Entity\Book">
96
 *         <operations>
97
 *             <operation class="ApiPlatform\Metadata\GetCollection">
98
 *                 <filters>
99
 *                     <filter>book.group_filter</filter>
100
 *                 </filters>
101
 *             </operation>
102
 *         </operations>
103
 *     </resource>
104
 * </resources>
105
 * ```
106
 *
107
 * </div>
108
 *
109
 * Given that the collection endpoint is `/books`, you can filter books by serialization groups with the following query: `/books?groups[]=read&groups[]=write`.
110
 *
111
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
112
 */
113
final class GroupFilter implements FilterInterface
114
{
115
    public function __construct(private readonly string $parameterName = 'groups', private readonly bool $overrideDefaultGroups = false, private readonly ?array $whitelist = null)
116
    {
UNCOV
117
    }
174✔
118

119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function apply(Request $request, bool $normalization, array $attributes, array &$context): void
123
    {
UNCOV
124
        if (\array_key_exists($this->parameterName, $commonAttribute = $request->attributes->get('_api_filters', []))) {
29✔
125
            $groups = $commonAttribute[$this->parameterName];
×
126
        } else {
UNCOV
127
            $groups = $request->query->all()[$this->parameterName] ?? null;
29✔
128
        }
129

UNCOV
130
        if (!\is_array($groups)) {
29✔
131
            return;
28✔
132
        }
133

UNCOV
134
        if (null !== $this->whitelist) {
27✔
135
            $groups = array_intersect($this->whitelist, $groups);
6✔
136
        }
137

UNCOV
138
        if (!$this->overrideDefaultGroups && isset($context[AbstractNormalizer::GROUPS])) {
27✔
139
            $groups = array_merge((array) $context[AbstractNormalizer::GROUPS], $groups);
13✔
140
        }
141

UNCOV
142
        $context[AbstractNormalizer::GROUPS] = $groups;
27✔
143
    }
144

145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function getDescription(string $resourceClass): array
149
    {
UNCOV
150
        $description = [
156✔
UNCOV
151
            'type' => 'string',
156✔
UNCOV
152
            'is_collection' => true,
156✔
UNCOV
153
            'required' => false,
156✔
UNCOV
154
        ];
156✔
155

UNCOV
156
        if ($this->whitelist) {
156✔
157
            $description['schema'] = [
153✔
158
                'type' => 'array',
153✔
159
                'items' => [
153✔
160
                    'type' => 'string',
153✔
161
                    'enum' => $this->whitelist,
153✔
162
                ],
153✔
163
            ];
153✔
164
        }
165

UNCOV
166
        return ["$this->parameterName[]" => $description];
156✔
167
    }
168
}
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