配置 api-platform 以在 OpenAPI (AKA Swagger) 文档中包含标头

问题描述

我将 API-PlatformJWTs 一起使用,并创建了一个装饰器以允许 Swagger 显示 https://api-platform.com/docs/core/jwt/ 所描述的 /authentication_token 端点,并在较小程度上显示 {{3 }}。除了提供电子邮件和密码之外,我还需要提供第三个参数,它是一个 UUID,用于标识用户所属的帐户。我可以通过在正文中包含 UUID 来实现这一点,但我宁愿将其作为标头包含,例如 X-Account-UUID

如何修改,以便 Swagger-UI 将此 uuid 属性包含为标头而不包含在正文的 JSON 中?

<?PHP
// api/src/OpenApi/JwtDecorator.PHP

declare(strict_types=1);

namespace App\OpenApi;

use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
use ApiPlatform\Core\OpenApi\OpenApi;
use ApiPlatform\Core\OpenApi\Model;

final class JwtDecorator implements OpenApiFactoryInterface
{
    private $decorated;

    public function __construct(OpenApiFactoryInterface $decorated)
    {
        $this->decorated = $decorated;
    }

    public function __invoke(array $context = []): OpenApi
    {
        $openApi = ($this->decorated)($context);
        $schemas = $openApi->getComponents()->getSchemas();
        /*
        echo(json_encode(['class'=>get_class($this),'methods'=>get_class_methods($this)]));
        echo(json_encode(['class'=>get_class($this->decorated),'methods'=>get_class_methods($this->decorated)]));
        echo(json_encode(['class'=>get_class($openApi),'methods'=>get_class_methods($openApi)]));
        echo(json_encode(['class'=>get_class($openApi->getComponents()),'methods'=>get_class_methods($openApi->getComponents())]));
        echo(json_encode(['class'=>get_class($schemas),'properties'=>$schemas]));
        $securitySchemas = $openApi->getComponents()->getSecuritySchemes();
        echo(json_encode(['class'=>get_class($securitySchemas),'properties'=>$securitySchemas]));
        $headers = $openApi->getComponents()->getHeaders();
        exit(json_encode(['class'=>get_class($headers),'properties'=>$headers]));
        */
        $schemas['Token'] = new \ArrayObject([
            'type' => 'object','properties' => [
                'token' => [
                    'type' => 'string','readOnly' => true,],]);
        $schemas['Credentials'] = new \ArrayObject([
            'type' => 'object','properties' => [
                'email' => [
                    'type' => 'string','example' => 'john.doe@bla.com','password' => [
                    'type' => 'string','example' => 'secret','uuid' => [
                    'type' => 'string','in' => 'header','example' => '1234abcd-abcd-1234-abcd-123412341234','headers' => [
                'uuid' => [
                    'type' => 'string',]);

        $responses = [
            '200' => [
                'description' => 'Get JWT token','content' => [
                    'application/json' => [
                        'schema' => [
                            '$ref' => '#/components/schemas/Token',]
            ]
        ];

        $content = new \ArrayObject([
            'application/json' => [
                'schema' => [
                    '$ref' => '#/components/schemas/Credentials',]);

        $requestBody = new Model\RequestBody('Generate new JWT Token',$content);
        $post = new Model\Operation('postCredentialsItem',[],$responses,'Get JWT token to login.','',new Model\ExternalDocumentation,$requestBody);
        $pathItem = new Model\PathItem('JWT Token',null,$post);

        $openApi->getPaths()->addpath('/authentication_token',$pathItem);

        return $openApi;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)