Symfony 4.4 / NelmioApiDocBundle 4.0.1注释问题



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

我的问题如下:在运行单元测试时,我确实得到了以下错误

[语义错误]注释"@OpenAPIAnnotationsTag"在方法AppRestControllerDnsEndpointsController::getDnsEndpointsAction()从未导入。你是不是忘了加上"use"这个词了?注释的语句?

下面是导致这个错误的相关代码:

DnsEndpointsController.php:
<?php
declare(strict_types=1);
namespace AppRestController;
(...)
use FOSRestBundleControllerAbstractFOSRestController;
use FOSRestBundleControllerAnnotations as Rest;
use FOSRestBundleRequestParamFetcherInterface;
use NelmioApiDocBundleAnnotationModel;
use OpenAPIAnnotationsGet;
use OpenAPIAnnotationsItems;
use OpenAPIAnnotationsJsonContent;
use OpenAPIAnnotationsParameter;
use OpenAPIAnnotationsResponse as OAResponse;
use OpenAPIAnnotationsSchema;
use OpenAPIAnnotationsTag;
use SensioBundleFrameworkExtraBundleConfigurationParamConverter;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentValidatorConstraintViolationInterface;
use SymfonyComponentValidatorConstraintViolationListInterface;
use SymfonyComponentValidatorValidation;
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.
     *
     * @RestRoute("/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", in="query", required=false,
     *     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 OpenAPIAnnotationsTag;,但它不被识别。这个注释甚至完全类似于Zircote的显式示例:Swagger-PHP v3.x。还要注意,前面的@RestRoute被传递并正确解析。

  2. 无论我是使用use OpenAPIAnnotations as OA;这样的缩写导入,随后使用@OATag,还是像示例中那样使用显式类导入,结果都没有区别。

  3. 是否有一种方法可以显式验证注释,而不是等待phpunit运行测试?我在这个阶段浪费了数百分钟盯着管道进度,这更增加了我对这个问题的沮丧感。

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

在Windows上使用PHP Storm,当测试管道在基于linux的主机上运行时,OpenAPIOpenApi之间的大小写不匹配没有被注意到,因此它产生了差异。谢谢@GuilhemN发现了这一点。

最新更新