微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Symfony 4.4 / NelmioApiDocBundle 4.0.1 注释问题

如何解决Symfony 4.4 / NelmioApiDocBundle 4.0.1 注释问题

一个类似的问题已经出现,但在 Symfony 和 NelmioApiDoc 上有不同的版本。而且我不确定是这种组合还是我在顶部使用 FesRestBundle 的事实。

我的问题如下:运行单元测试时出现以下错误

[语义错误] 从未导入方法 App\RestController\DnsEndpointsController::getDnsEndpointsAction() 中的注释“@OpenAPI\Annotations\Tag”。您是否忘记为此注释添加“use”语句?

这是我导致此错误的相关代码

DnsEndpointsController.PHP:
<?PHP

declare(strict_types=1);

namespace App\RestController;

(...)
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Request\ParamFetcherInterface;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenAPI\Annotations\Get;
use OpenAPI\Annotations\Items;
use OpenAPI\Annotations\JsonContent;
use OpenAPI\Annotations\Parameter;
use OpenAPI\Annotations\Response as OAResponse;
use OpenAPI\Annotations\Schema;
use OpenAPI\Annotations\Tag;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validation;

class DnsEndpointsController extends AbstractFOSRestController
{
(...)
    /**
     * The endpoint to get all DNS Endpoints listed in the database
     *
     * This call will return all DNS Endpoints that are matching the filter criteria handed in as query parameter.
     *
     * @Rest\Route("/subjects/dnsendpoints",name="rest_api_subjects_get_dns_endpoints",methods={"GET"})
     *
     * @Tag("Subjects - DnsEndpoints")
     *
     * @Get(
     *     route="/subjects/dnsendpoints",*     @OAResponse(
     *         response=200,*         description="Returned when successful",*
     *         @JsonContent(
     *             type="array",*             @Items(ref=@Model(type=NsdPrdplDnsEndpoint::class))
     *         )
     *     )
     * )
     *
     * @Parameter(
     *     name="page",in="query",required=false,*     description="number of page requested",*     @Schema(type="integer")
     * )
     * @Parameter(
     *     name="perPage",*     description="number entries per page",*     @Schema(type="integer")
     * )
(...)
     *
     * @param ParamFetcherInterface $paramFetcher
     *
     * @return Response
     *
     * @throws \LogicException
     */
    public function getDnsEndpointsAction(ParamFetcherInterface $paramFetcher): Response
(...)

相关配置是这样的:

config/routes/annotations.yaml:
(...)
rest-controllers:
    resource: ../../src/RestController/
    prefix: /api
    type: annotation

和:

composer.json:
(...)
    "require": {
        "PHP": ">=7.1.0","ext-json": "*","beberlei/doctrineextensions": "^1.2","composer/package-versions-deprecated": "^1.11","doctrine/annotations": "^1.0","doctrine/doctrine-bundle": "^2.1","doctrine/doctrine-migrations-bundle": "^2.2.0","doctrine/orm": "^2.6","friendsofsymfony/rest-bundle": "^2.8","incenteev/composer-parameter-handler": "^2.1","jms/serializer-bundle": "^2.4","lexik/jwt-authentication-bundle": "^2.8","nelmio/api-doc-bundle": "^4.0","PHPdocumentor/reflection-docblock": "^4.3","sensio/framework-extra-bundle": "^5.5","symfony/asset": "^4.4","symfony/flex": "^1.9","symfony/form": "^4.4","symfony/mailer": "^4.4","symfony/monolog-bundle": "^3.6","symfony/polyfill-apcu": "^1.0","symfony/property-access": "4.4.0","symfony/property-info": "4.4.0","symfony/security": "^4.4","symfony/security-bundle": "^4.4","symfony/serializer": "4.4.0","symfony/swiftmailer-bundle": "^3.1","symfony/templating": "4.4.0","symfony/translation": "^4.4","symfony/twig-bundle": "^4.4","symfony/validator": "^4.4","twig/extra-bundle": "^2.12|^3.0","twig/twig": "^2.12|^3.0"
    },(...)

请注意我围绕这些注释存在的三个问题:

  1. 尽管我明确声明 use OpenAPI\Annotations\Tag; 它不被识别。这个注释甚至完全类似于来自 Zircote 的显式示例:Swagger-PHP v3.x。另请注意,前面的 @Rest\Route 已传递并正确解析。

  2. 我是否使用像 use OpenAPI\Annotations as OA; 这样的缩写导入并因此在后面使用 @OA\Tag 或示例中的显式类导入对结果没有影响。

  3. 有没有办法显式验证注释而不是等待 PHPunit 运行测试?在这个阶段,我花了数百分钟盯着管道进度,这让我对这个问题更加沮丧。

对这个有什么想法吗?我错过了什么?

解决方法

在 Windows 上使用 PHP Storm,当测试管道在基于 linux 的主机上运行时,Jul 25,2021 @ 16:10:04.183 GET "/store/points",parameters={} Jul 25,2021 @ 16:11:07.226 Writing [[PointDto(id=91384750,etc OpenAPI 之间的大小写不匹配没有被注意到,因此它有所不同。感谢@GuilhemN 发现这一点。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。