• 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

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

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

128
        if (!\is_array($groups)) {
×
129
            return;
×
130
        }
131

132
        if (null !== $this->whitelist) {
×
133
            $groups = array_intersect($this->whitelist, $groups);
×
134
        }
135

136
        if (!$this->overrideDefaultGroups && isset($context[AbstractNormalizer::GROUPS])) {
×
137
            $groups = array_merge((array) $context[AbstractNormalizer::GROUPS], $groups);
×
138
        }
139

140
        $context[AbstractNormalizer::GROUPS] = $groups;
×
141
    }
142

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

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

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