API平台注解中使用参数

Ren*_*haf 5 php annotations symfony api-platform.com

我正在研究如何将动态变量添加到 API 平台@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;
Run Code Online (Sandbox Code Playgroud)

%app.auth.default.redirect%不被同名的容器参数替换。我应该怎么做 ?

Evg*_*ban 2

乍一看,我在这里只看到一种方法 - 在 中创建您自己的属性openapi_context,比方说my_redirect_example

像这样,例如:

"openapi_context"={
    "type"="string",
    "my_redirect_example"=true
}
Run Code Online (Sandbox Code Playgroud)

然后你需要像文档中那样进行装饰

嗯,就像这样:

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;
}
Run Code Online (Sandbox Code Playgroud)

它应该有效。无论如何 - 这只是一个例子(我没有测试它),只是为了了解如何处理它。

当然,您可以true用自己的值替换值,并在if语句中使用它来根据您自己的逻辑来获取它。