2

A similar question is already out, but with a different versioning on Symfony and NelmioApiDoc. And I am not sure it is this combination or that fact that also I am using FesRestBundle on top.

My issue is the following: I do get the following error when running the unit tests

[Semantical Error] The annotation "@OpenAPI\Annotations\Tag" in method App\RestController\DnsEndpointsController::getDnsEndpointsAction() was never imported. Did you maybe forget to add a "use" statement for this annotation?

Here is my relevant code leading to this error:

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
(...)

The relevant configuration is this:

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

and:

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"
    },
(...)

Note three issues that I have around these annotations:

  1. Despite the fact that I do explicitly state use OpenAPI\Annotations\Tag; it is not recognized. This annotation is even in full analogy to the explicit example from Zircote here: Swagger-PHP v3.x. Note also, that the preceding @Rest\Route is passed and properly parsed.

  2. Whether I use the abbreviated import like use OpenAPI\Annotations as OA; and consequently @OA\Tag later or the explicit class import as in the example makes no difference in the result.

  3. Is there a way to validate the annotations explicitly rather than waiting for phpunit to run the tests? I am waisting hundreds of minutes at this stage staring at the pipeline progress which increases my frustration about this issue even more.

Any idea on this one? What am I missing?

Carsten
  • 21
  • 2
  • About debugging (question 3): does your IDE recognise the class? And can't you just perform a request in you local environment? Or run just a small part of the test? – Stephan Vierkant Jul 23 '21 at 18:32
  • IDE is PHPStorm and recognizes the annotation class w/o complaints. Yes, I limited the scope to run only through a single controller class. So faster now, thanks. – Carsten Jul 23 '21 at 21:25
  • 2
    Could it be related to case sensitivity? https://dotjoeblog.wordpress.com/2019/07/03/php-namespaces-are-case-insensitive-but/ The actual namespace in the library is `OpenApi` and not `OpenAPI`. – Guilhem N Jul 24 '21 at 08:49
  • Thanks @GuilhemN that was definitely the (first) issue here - moving to a different error now where the Tag annoation claims an unknown parameter. But that indicates the situation I addressed here is unblocked. Thanks ! – Carsten Jul 25 '21 at 13:10

1 Answers1

0

Using PHP Storm on Windows, the case-mismatch between OpenAPI and OpenApi went unnoticed while the testing pipeline runs on a linux-based host and hence it makes the difference. Thanks @GuilhemN for spotting that.

Carsten
  • 21
  • 2