首页 > 解决方案 > 配置 api-platform 以在 OpenAPI (AKA Swagger) 文档中包含标头

问题描述

我正在将API 平台JWT一起使用,并创建了一个装饰器以允许 Swagger 显示https://api-platform.com/docs/core/jwt//authentication_token所描述的端点,并在较小程度上显示https://api- platform.com/docs/core/openapi/。除了提供电子邮件和密码之外,我还需要提供第三个参数,它是一个 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',
                    'in' => 'header',
                    'example' => '1234abcd-abcd-1234-abcd-123412341234',
                ],
            ],
        ]);

        $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, null, null, null, $post);

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

        return $openApi;
    }
}

标签: symfonyswaggeropenapiapi-platform.comlexikjwtauthbundle

解决方案


推荐阅读