【问题标题】:Removing some schemas/models from API-Platforms Swagger/OpenAPI documentation output从 API-Platforms Swagger/OpenAPI 文档输出中删除一些模式/模型
【发布时间】:2021-09-07 23:55:40
【问题描述】:

API-Platform 将生成 Swagger/OpenAPI 路由文档,然后生成架构(AKA 模型)的文档(docs 将它们显示为“模型”,但当前版本(例如 2.7)将它们显示为“架构”)。

为显示这些架构/模型而生成的内容在哪里?有的怎么能去掉?显示它们的功能是 Swagger-UI 的一部分,但 API-Platform 必须负责提供 JSON 配置,从而更改 API-Platform 而不是 Swagger-UI。请注意,this post 显示了如何添加模式,而不是如何删除模式。除了this 之外,还有没有关于这个主题的文档没有详细说明?

从下面的输出中可以看出,我公开了AbstractOrganization,但是,这个类由其他几个类扩展并且不打算公开,但应该只公开具体类的模式。请注意,我的 AbstractOrganization 实体类未使用 @ApiResource 标记,并且未显示在 Swagger/OpenAPI 路由文档中,而仅显示在架构/模型文档中。

谢谢

【问题讨论】:

    标签: symfony swagger openapi api-platform.com


    【解决方案1】:

    我很确定有更好的方法来实现这一点,但是,以下方法会起作用并且可能对其他人有所帮助。

    <?php
    declare(strict_types=1);
    namespace App\OpenApi;
    
    use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
    use ApiPlatform\Core\OpenApi\OpenApi;
    use ApiPlatform\Core\OpenApi\Model\Paths;
    use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
    
    class OpenApiRouteHider implements OpenApiFactoryInterface {
    
        public function __construct(private OpenApiFactoryInterface $decorated, private TokenStorageInterface $tokenStorage)
        {
        }
    
        public function __invoke(array $context = []): OpenApi 
        {
            $openApi = $this->decorated->__invoke($context);
    
            $removedPaths = $this->getRemovedPaths();
    
            $paths = new Paths;
            $pathArray = $openApi->getPaths()->getPaths();
            foreach($openApi->getPaths()->getPaths() as $path=>$pathItem) {
                if(!isset($removedPaths[$path])) {
                    // No restrictions
                    $paths->addPath($path, $pathItem);
                }
                elseif($removedPaths[$path]!=='*') {
                    // Remove one or more operation
                    foreach($removedPaths[$path] as $operation) {
                        $method = 'with'.ucFirst($operation);
                        $pathItem = $pathItem->$method(null);                    
                    }
                    $paths->addPath($path, $pathItem);
                }
                // else don't add this route to the documentation
            }
            $openApiTest = $openApi->withPaths($paths);
    
            return $openApi->withPaths($paths);
        }
    
        private function getRemovedPaths():array
        {
            // Use $user to determine which ones to remove.
            $user = $this->tokenStorage->getToken()->getUser();
            return [
                '/guids'=>'*',                  // Remove all operations
                '/guids/{guid}'=>'*',           // Remove all operations
                '/tenants'=>['post', 'get'],   // Remove only post and get operations
                '/tenants/{uuid}'=>['delete'], // Remove only delete operation
                '/chart_themes'=>'*',
                '/chart_themes/{id}'=>['put', 'delete', 'patch'],
            ];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-14
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 2020-09-06
      • 2021-11-20
      相关资源
      最近更新 更多