首页 > 解决方案 > Symfony 4.4 / NelmioApiDocBundle 4.0.1 注释问题

问题描述

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

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

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

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

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", 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 OpenAPI\Annotations\Tag;它不被认可。这个注释甚至完全类似于 Zircote 这里的显式示例:Swagger-PHP v3.x。另请注意,前面@Rest\Route的内容已通过并正确解析。

  2. 无论我是否使用缩写导入(如use OpenAPI\Annotations as OA;随后@OA\Tag)或示例中的显式类导入,结果都没有区别。

  3. 有没有办法明确地验证注释而不是等待 phpunit 运行测试?在这个阶段,我正在等待数百分钟盯着管道进度,这更加增加了我对这个问题的挫败感。

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

标签: phpsymfonyannotationsfosrestbundlenelmioapidocbundle

解决方案


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


推荐阅读