Api-Platform + Swagger:按 Doctrine 或 ApiPlatform 映射分组 operations/entities
Api-Platform + Swagger: group operations/entities by Doctrine or ApiPlatform mapping
我有 config/packages/api_platform.yaml
:
api_platform:
mapping:
paths:
- '%kernel.project_dir%/src/Entity'
- '%kernel.project_dir%/old/Entity'
在两个名称空间中,我都有同名的实体。我需要做些什么才能让它们显示在 /docs
中分组到不同的映射中,而不是单独按实体 class 名称显示。
如何为每个命名空间“全局”配置前缀?
API 平台目前不提供此配置,您将不得不像 this
一样装饰 OpenApiFactory
Swagger 文档按 tags
对它们进行分组
<?php
// src/OpenApi/OpenApiFactory.php
namespace App\OpenApi;
use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
use ApiPlatform\Core\OpenApi\OpenApi;
use ApiPlatform\Core\OpenApi\Model;
final class OpenApiFactory implements OpenApiFactoryInterface
{
private $decorated;
public function __construct(OpenApiFactoryInterface $decorated)
{
$this->decorated = $decorated;
}
public function __invoke(array $context = []): OpenApi
{
$openApi = $this->decorated->__invoke($context);
$paths = $openApi->getPaths()->getPaths();
$filteredPaths = new Model\Paths();
foreach ($paths as $path => $pathItem) {
// Here you can check the tags for each Operations
// Add custom logic with the snippet below
}
return $openApi->withPaths($filteredPaths);
}
}
肮脏的部分来了,标签将是 EntityName
而不是完整的命名空间名称
我会列出所有旧实体,过滤当前标签并更改为旧的或新的
(以下为伪代码)
$OldEntityList = ['entity1', 'entity2', ...]
foreach ($Operations as $op) {
if (in_array($op->getTags(), $OldEntityList) {
$op->withTags('old');
} else {
$op->withTags('new');
}
}
我有 config/packages/api_platform.yaml
:
api_platform:
mapping:
paths:
- '%kernel.project_dir%/src/Entity'
- '%kernel.project_dir%/old/Entity'
在两个名称空间中,我都有同名的实体。我需要做些什么才能让它们显示在 /docs
中分组到不同的映射中,而不是单独按实体 class 名称显示。
如何为每个命名空间“全局”配置前缀?
API 平台目前不提供此配置,您将不得不像 this
一样装饰OpenApiFactory
Swagger 文档按 tags
<?php
// src/OpenApi/OpenApiFactory.php
namespace App\OpenApi;
use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
use ApiPlatform\Core\OpenApi\OpenApi;
use ApiPlatform\Core\OpenApi\Model;
final class OpenApiFactory implements OpenApiFactoryInterface
{
private $decorated;
public function __construct(OpenApiFactoryInterface $decorated)
{
$this->decorated = $decorated;
}
public function __invoke(array $context = []): OpenApi
{
$openApi = $this->decorated->__invoke($context);
$paths = $openApi->getPaths()->getPaths();
$filteredPaths = new Model\Paths();
foreach ($paths as $path => $pathItem) {
// Here you can check the tags for each Operations
// Add custom logic with the snippet below
}
return $openApi->withPaths($filteredPaths);
}
}
肮脏的部分来了,标签将是 EntityName
而不是完整的命名空间名称
我会列出所有旧实体,过滤当前标签并更改为旧的或新的
(以下为伪代码)
$OldEntityList = ['entity1', 'entity2', ...]
foreach ($Operations as $op) {
if (in_array($op->getTags(), $OldEntityList) {
$op->withTags('old');
} else {
$op->withTags('new');
}
}