Php 在API平台注释中使用参数
我正在研究如何将动态变量添加到API平台Php 在API平台注释中使用参数,php,symfony,annotations,api-platform.com,Php,Symfony,Annotations,Api Platform.com,我正在研究如何将动态变量添加到API平台@ApiProperty注释中。 我发现Symfony允许这样做,但它在API平台注释中似乎不起作用。 例如: /** * Redirection URL. * * @Groups({"authorization_code_login_write", "authorization_code_logout_write"}) * @ApiProperty( * attributes={ *
@ApiProperty
注释中。
我发现Symfony允许这样做,但它在API平台注释中似乎不起作用。
例如:
/**
* Redirection URL.
*
* @Groups({"authorization_code_login_write", "authorization_code_logout_write"})
* @ApiProperty(
* attributes={
* "openapi_context"={
* "type"="string",
* "example"="%app.auth.default.redirect%"
* }
* }
* )
*/
protected ?string $redirectionUrl = null;
%app.auth.default.redirect%
未替换为同名的容器参数。
我该怎么做?乍一看,我在这里只看到一种方法——在
openapi\u上下文中创建自己的属性,比如my\u redirect\u示例
例如,Smth如下所示:
"openapi_context"={
"type"="string",
"my_redirect_example"=true
}
那你需要装饰一下
Smth,就像这样:
public function normalize($object, $format = null, array $context = [])
{
$docs = $this->decorated->normalize($object, $format, $context);
$redirectUrl = .... # your own logic to get this dynamical value
foreach ($docs['paths'] as $pathName => $path) {
foreach ($path as $operationName => $operation) {
if ($operation['my_redirect_example'] ?? false) {
$docs['paths'][$pathName][$operationName]['example'] = $redirectUrl;
}
}
}
return $docs;
}
它应该会起作用。无论如何,这只是一个例子(我没有测试它),只是为了了解如何处理它
当然,您可以将true
值替换为您自己的值,并在if
语句中使用它,以根据您自己的逻辑获得它。方法是按照文档来装饰Swagger Open API generator服务()
添加您自己的服务:
# api/config/services.yaml
services:
'App\Swagger\SwaggerDecorator':
decorates: 'api_platform.swagger.normalizer.api_gateway'
arguments: [ '@App\Swagger\SwaggerDecorator.inner' ]
autoconfigure: false
然后创建您的服务类:
<?php
namespace App\Swagger;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Custom Swagger decorator to remove/edit some API documentation information.
*/
final class SwaggerDecorator implements NormalizerInterface
{
/**
* Decorated base Swagger normalizer.
*
* @var NormalizerInterface
*/
protected NormalizerInterface $decorated;
/**
* SwaggerDecorator constructor.
*
* @param NormalizerInterface $decorated
*/
public function __construct(NormalizerInterface $decorated)
{
$this->decorated = $decorated;
}
/**
* {@inheritDoc}
*/
public function normalize($object, string $format = null, array $context = [])
{
$docs = $this->decorated->normalize($object, $format, $context);
$docs['components']['schemas']['authorization-authorization_code_login_write']['properties']['redirectionUrl']['example'] = 'https://example.com/my-dynamic-redirection';
$docs['components']['schemas']['authorization:jsonld-authorization_code_login_write']['properties']['redirectionUrl']['example'] = 'https://example.com/my-dynamic-redirection';
return $docs;
}
/**
* {@inheritDoc}
*/
public function supportsNormalization($data, string $format = null)
{
return $this->decorated->supportsNormalization($data, $format);
}
}
你说得对,装饰api\u平台。招摇过市。规范化器。文档服务是一条出路。我已经设法用这个方法完成了,但是在我的例子中,我必须重写模式,而不是直接重写路径。我将用下面的代码写在其他答案上。非常感谢你。