symfony - 配置 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;
}
}
解决方案
推荐阅读
- angular - Angular 6 + jsQR - 如何在第一次成功时捕获 QR 码?
- python - NLTK 和 Stanford Dependency Parser - 如何获取单词位置?
- java - Java 通配符有界泛型
- javascript - 有没有办法从节点红色转移到纯代码?
- jquery - 在 Nuget 管理器中安装 jQuery 后未定义“jQuery”
- julia - 重载已被导入模块使用的函数名称
- asp.net-web-api - 如何在 db first 方法的现有 mvc 项目中添加身份框架
- node.js - 来自 forEach 函数的多个承诺
- r - curl::curl_fetch_memory(url, handle = handle) 中的错误:操作被 RStudio 中的应用程序中止
- sql - 如何在 SQL 的单列中合并三个差异列