Reference
C
Class

ApiPlatform\Serializer\Filter\GroupFilter

The group filter allows you to filter by serialization groups.

Syntax: ?groups[]=<group>.

You can add as many groups as you need.

Three arguments are available to configure the filter:

  • parameterName is the query parameter name (default: groups)
  • overrideDefaultGroups allows to override the default serialization groups (default: false)
  • whitelist groups whitelist to avoid uncontrolled data exposure (default: null to allow all groups)
<?php
// api/src/Entity/Book.php
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Serializer\Filter\GroupFilter;
 
#[ApiResource]
#[ApiFilter(GroupFilter::class, arguments: ['parameterName' => 'groups', 'overrideDefaultGroups' => false, 'whitelist' => ['allowed_group']])]
class Book
{
    // ...
}
<?php
// api/src/Entity/Book.php
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Serializer\Filter\GroupFilter;
 
#[ApiResource]
#[ApiFilter(GroupFilter::class, arguments: ['parameterName' => 'groups', 'overrideDefaultGroups' => false, 'whitelist' => ['allowed_group']])]
class Book
{
    // ...
}

Given that the collection endpoint is /books, you can filter books by serialization groups with the following query: /books?groups[]=read&groups[]=write.

class ApiPlatform\Serializer\Filter\GroupFilter implements `<a href="/docs/reference/Serializer/Filter/FilterInterface">ApiPlatform\Serializer\Filter\FilterInterface</a>`, `<a href="/docs/reference/Metadata/FilterInterface">ApiPlatform\Metadata\FilterInterface</a>`
{
    public __construct(string $parameterName, bool $overrideDefaultGroups, null|array $whitelist)
    public apply(Symfony\Component\HttpFoundation\Request $request, bool $normalization, array $attributes, array $context): null
    public getDescription(string $resourceClass): array
}
class ApiPlatform\Serializer\Filter\GroupFilter implements `<a href="/docs/reference/Serializer/Filter/FilterInterface">ApiPlatform\Serializer\Filter\FilterInterface</a>`, `<a href="/docs/reference/Metadata/FilterInterface">ApiPlatform\Metadata\FilterInterface</a>`
{
    public __construct(string $parameterName, bool $overrideDefaultGroups, null|array $whitelist)
    public apply(Symfony\Component\HttpFoundation\Request $request, bool $normalization, array $attributes, array $context): null
    public getDescription(string $resourceClass): array
}

Methods

__construct

public __construct(string $parameterName, bool $overrideDefaultGroups, null|array $whitelist)
public __construct(string $parameterName, bool $overrideDefaultGroups, null|array $whitelist)

Parameters

parameterNamestring
overrideDefaultGroupsbool
whitelistarray

apply

Apply a filter to the serializer context.

public apply(Symfony\Component\HttpFoundation\Request $request, bool $normalization, array $attributes, array $context): null
public apply(Symfony\Component\HttpFoundation\Request $request, bool $normalization, array $attributes, array $context): null

Parameters

requestSymfony\Component\HttpFoundation\Request
normalizationbool
attributesarray
contextarray

Returns

null

getDescription

Gets the description of this filter for the given resource.Returns an array with the filter parameter names as keys and array with the following data as values:

  • property: the property where the filter is applied
  • type: the type of the filter
  • required: if this filter is required
  • strategy (optional): the used strategy
  • is_collection (optional): if this filter is for collection
  • swagger (optional): additional parameters for the path operation, e.g. 'swagger' => [ 'description' => 'My Description', 'name' => 'My Name', 'type' => 'integer', ]
  • openapi (optional): additional parameters for the path operation in the version 3 spec, e.g. 'openapi' => [ 'description' => 'My Description', 'name' => 'My Name', 'schema' => [ 'type' => 'integer', ] ]
  • schema (optional): schema definition, e.g. 'schema' => [ 'type' => 'string', 'enum' => ['value_1', 'value_2'], ] The description can contain additional data specific to a filter.Gets the description of this filter for the given resource.Returns an array with the filter parameter names as keys and array with the following data as values:
  • property: the property where the filter is applied
  • type: the type of the filter
  • required: if this filter is required
  • strategy (optional): the used strategy
  • is_collection (optional): if this filter is for collection
  • swagger (optional): additional parameters for the path operation, e.g. 'swagger' => [ 'description' => 'My Description', 'name' => 'My Name', 'type' => 'integer', ]
  • openapi (optional): additional parameters for the path operation in the version 3 spec, e.g. 'openapi' => [ 'description' => 'My Description', 'name' => 'My Name', 'schema' => [ 'type' => 'integer', ] ]
  • schema (optional): schema definition, e.g. 'schema' => [ 'type' => 'string', 'enum' => ['value_1', 'value_2'], ] The description can contain additional data specific to a filter.
public getDescription(string $resourceClass): array
public getDescription(string $resourceClass): array

Parameters

resourceClassstring

Returns

array

Copyright © 2023 Kévin Dunglas

Sponsored by Les-Tilleuls.coop