为了在实体中使用@MaxDepth批注,必须在序列化程序上下文中(例如在@ApiPlatform批注的配置中)显式设置enable_max_depth
属性,因此在实体级别,因此对于每个实体< / p>
是否可以为项目的所有实体定义此属性enable_max_depth=true
?我们可以在api-platform.yaml
中找到的东西看起来像这样:
api-platform:
serializer:
enable_max_depth: true
答案 0 :(得分:1)
目前尚无此类全局选项(值得添加,欢迎PR)。
但是,you can register a SerializerContextBuilder
会为所有资源自动添加此上下文条目:
<?php
namespace App\Serializer;
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
use Symfony\Component\HttpFoundation\Request;
final class MaxDepthContextBuilder implements SerializerContextBuilderInterface
{
private $decorated;
public function __construct(SerializerContextBuilderInterface $decorated)
{
$this->decorated = $decorated;
}
public function createFromRequest(Request $request, bool $normalization, ?array $extractedAttributes = null): array
{
$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
$context['enable_max_depth'] = true;
return $context;
}
}
然后将这个新类注册为服务装饰器:
# api/config/services.yaml
services:
# ...
'App\Serializer\MaxDepthContextBuilder':
decorates: 'api_platform.serializer.context_builder'
autoconfigure: false
autowire: true