Guides
Tutorials
The property filter adds the possibility to select the properties to serialize (sparse fieldsets).
Note: We strongly recommend using Vulcain instead of this filter. Vulcain is faster, allows a better hit rate, and is supported out of the box in the API Platform distribution.
Syntax: ?properties[]=<property>&properties[<relation>][]=<property>
.
You can add as many properties as you need.
Three arguments are available to configure the filter:
parameterName
is the query parameter name (default: properties
)overrideDefaultProperties
allows to override the default serialization properties (default: false
)whitelist
properties whitelist to avoid uncontrolled data exposure (default: null
to allow all properties)<?php
// api/src/Entity/Book.php
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Serializer\Filter\PropertyFilter;
#[ApiResource]
#[ApiFilter(PropertyFilter::class, arguments: ['parameterName' => 'properties', 'overrideDefaultProperties' => false, 'whitelist' => ['allowed_property']])]
class Book
{
// ...
}
<?php
// api/src/Entity/Book.php
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Serializer\Filter\PropertyFilter;
#[ApiResource]
#[ApiFilter(PropertyFilter::class, arguments: ['parameterName' => 'properties', 'overrideDefaultProperties' => false, 'whitelist' => ['allowed_property']])]
class Book
{
// ...
}
# config/services.yaml
services:
book.property_filter:
parent: 'api_platform.serializer.property_filter'
arguments: [ $parameterName: 'properties', $overrideDefaultGroups: false, $whitelist: ['allowed_property'] ]
tags: [ 'api_platform.filter' ]
# The following are mandatory only if a _defaults section is defined with inverted values.
# You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
autowire: false
autoconfigure: false
public: false
# api/config/api_platform/resources.yaml
resources:
App\Entity\Book:
- operations:
ApiPlatform\Metadata\GetCollection:
filters: ['book.property_filter']
# config/services.yaml
services:
book.property_filter:
parent: 'api_platform.serializer.property_filter'
arguments: [ $parameterName: 'properties', $overrideDefaultGroups: false, $whitelist: ['allowed_property'] ]
tags: [ 'api_platform.filter' ]
# The following are mandatory only if a _defaults section is defined with inverted values.
# You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
autowire: false
autoconfigure: false
public: false
# api/config/api_platform/resources.yaml
resources:
App\Entity\Book:
- operations:
ApiPlatform\Metadata\GetCollection:
filters: ['book.property_filter']
<?xml version="1.0" encoding="UTF-8" ?>
<!-- api/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container
xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="book.property_filter" parent="api_platform.serializer.property_filter">
<argument key="parameterName">properties</argument>
<argument key="overrideDefaultGroups">false</argument>
<argument key="whitelist" type="collection">
<argument>allowed_property</argument>
</argument>
<tag name="api_platform.filter"/>
</service>
</services>
</container>
<!-- api/config/api_platform/resources.xml -->
<resources
xmlns="https://api-platform.com/schema/metadata/resources-3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
https://api-platform.com/schema/metadata/resources-3.0.xsd">
<resource class="App\Entity\Book">
<operations>
<operation class="ApiPlatform\Metadata\GetCollection">
<filters>
<filter>book.property_filter</filter>
</filters>
</operation>
</operations>
</resource>
</resources>
<?xml version="1.0" encoding="UTF-8" ?>
<!-- api/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container
xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="book.property_filter" parent="api_platform.serializer.property_filter">
<argument key="parameterName">properties</argument>
<argument key="overrideDefaultGroups">false</argument>
<argument key="whitelist" type="collection">
<argument>allowed_property</argument>
</argument>
<tag name="api_platform.filter"/>
</service>
</services>
</container>
<!-- api/config/api_platform/resources.xml -->
<resources
xmlns="https://api-platform.com/schema/metadata/resources-3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
https://api-platform.com/schema/metadata/resources-3.0.xsd">
<resource class="App\Entity\Book">
<operations>
<operation class="ApiPlatform\Metadata\GetCollection">
<filters>
<filter>book.property_filter</filter>
</filters>
</operation>
</operations>
</resource>
</resources>
Given that the collection endpoint is /books
, you can filter the serialization properties with the following query: /books?properties[]=title&properties[]=author
. If you want to include some properties of the nested "author" document, use: /books?properties[]=title&properties[author][]=name
.
class ApiPlatform\Serializer\Filter\PropertyFilter 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 $overrideDefaultProperties, null|array $whitelist, null|Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter)
public apply(Symfony\Component\HttpFoundation\Request $request, bool $normalization, array $attributes, array $context): null
public getDescription(string $resourceClass): array
}
class ApiPlatform\Serializer\Filter\PropertyFilter 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 $overrideDefaultProperties, null|array $whitelist, null|Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter)
public apply(Symfony\Component\HttpFoundation\Request $request, bool $normalization, array $attributes, array $context): null
public getDescription(string $resourceClass): array
}
public __construct(string $parameterName, bool $overrideDefaultProperties, null|array $whitelist, null|Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter)
public __construct(string $parameterName, bool $overrideDefaultProperties, null|array $whitelist, null|Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter)
parameterName | string | |
overrideDefaultProperties | bool | |
whitelist | array | |
nameConverter | Symfony\Component\Serializer\NameConverter\NameConverterInterface |
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
request | Symfony\Component\HttpFoundation\Request | |
normalization | bool | |
attributes | array | |
context | array |
null
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:
public getDescription(string $resourceClass): array
public getDescription(string $resourceClass): array
resourceClass | string |
array