1

API-Platform will generate Swagger/OpenAPI route documentation and then below documentation for the Schemas (AKA Models) (the docs show them as "Models" but current versions such as 2.7 show them as "Schemas").

Where is the content generated to show these schemas/models? How can some be removed? The functionality to display them is part of Swagger-UI, but API-Platform must be responsible for providing the JSON configuration and thus which to change API-Platform and not Swagger-UI. Note that this post shows how to add a schema but not how to remove one. Is there any documentation on the subject other than this which doesn't go into detail?

As seen by the output below, I am exposing AbstractOrganization, however, this class is extended by a couple other classes and is not meant to be exposed, but only schemas for the concrete classes should be exposed. Note that my AbstractOrganization entity class is not tagged with @ApiResource and is not shown in the Swagger/OpenAPI routing documentation but only the schema/model documentation.

Thank you

enter image description here

user1032531
  • 24,767
  • 68
  • 217
  • 387

1 Answers1

1

I am pretty certain there are better ways to implement this, however, the following will work and might be helpful for others.

<?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'],
        ];
    }
}
user1032531
  • 24,767
  • 68
  • 217
  • 387