DarkaOnLine / L5-Swagger

OpenApi or Swagger integration to Laravel
https://github.com/DarkaOnLine/L5-Swagger
MIT License
2.64k stars 394 forks source link

❓ Question - Class `...` is not annotated with `Attribute` #574

Closed Rikj000 closed 9 months ago

Rikj000 commented 9 months ago

Environment:

Software Version
L5-Swagger 8.5.2
PHP Version 8.1.27
OS + Kernel Manjaro Linux x86_64 + 6.6.11-x64v2-xanmod1-1

Description:

Annotating through PHP Attributes does not work correctly out of the box.
Annotating with PHP Doc blocks works correctly.

When trying to annotate through PHP Attributes:

  1. My IDE throws warnings like:

    Class 'Info' is not annotated with 'Attribute'
    Class 'Get' is not annotated with 'Attribute'

    is-not-annotated-with-attribute

  2. My CLI throws errors when I run php artisan l5-swagger:generate, like:

    Regenerating docs default
    
       Error 
    
      Attempting to use non-attribute class "OpenApi\Annotations\Info" as attribute
    
      at vendor/zircote/swagger-php/src/Analysers/AttributeAnnotationFactory.php:47
         43▕         $annotations = [];
         44▕         try {
         45▕             foreach ($reflector->getAttributes() as $attribute) {
         46▕                 if (class_exists($attribute->getName())) {
      ➜  47▕                     $instance = $attribute->newInstance();
         48▕                     if ($instance instanceof OA\AbstractAnnotation) {
         49▕                         $annotations[] = $instance;
         50▕                     }
         51▕                 } else {
    
          +21 vendor frames 
    
      22  artisan:35
          Illuminate\Foundation\Console\Kernel::handle()

    attempting-to-use-non-attribute

Steps To Reproduce:

  1. Install DarkaOnLine/L5-Swagger with php composer require darkaonline/l5-swagger
  2. Attempt to annotate a Class/Function with PHP Attributes

Note

Is there anything I'm missing here?

I saw in #514 that PHP Attributes should be supported by this library,
however the docs there do not mention anything about additional dependencies / configurations
which would be needed to make PHP Attributes work.

DerManoMann commented 9 months ago

I wonder if you have updated your import statement?

Annotations use:

use OpenApi\Annotations as OA;

Attributes use:

use OpenApi\Attributes as OA;

Personally I actually prefer

use OpenApi\Attributes as OAT;

to make things even clearer.

Rikj000 commented 9 months ago

I wonder if you have updated your import statement?

@DerManoMann that was indeed my issue, I did not update my import statement.. 🤦

Thank you for your quick reply!
I'll close the issue since it's resolved for me now 🙂