HFinch
HFinch

Reputation: 626

Symfony 4 route annotations cause error with openapi annotations

Currently our project has Symfony 4.3 installed. We have grown since 2.7 so there may be some legacy garbage but overall our app works nicely. We have a flavoring system that gets prefixed to every route (if this is a good idea I dont know but it was like this when we started supporting it). So a link would look like this:

test.me/{flavor}/project/{UUID}

We are using annotations for the routing and therefore have an annotations.yaml:

controllers:
    resource: ../../src/App/Controller/
    type: annotation
    prefix:   /{flavor}/

Now since we want to introduce an API documentation we wanted to use this php package: https://github.com/zircote/swagger-php which lets us write annotations for our API as we do it with routes and generates a swagger.json that can be distributed to our connected projects. Problem is when writing the annotations this error pops up:

$:bin/console cache:clear -e dev                                

 // Clearing the cache for the dev environment with debug true                                           


In FileLoader.php line 166:

  [Semantical Error] The annotation "@OA\Schema" in class App\Controller\Api\Model\APIMessage  
   was never imported. Did you maybe forget to add a "use" statement for this annotation? in /config/routes/../../src/Catrobat/Controller/ (which is being imported from "/config/routes/annotations.yaml"). Make sure annotations are installed  
   and enabled. 

Any Idea how I can tell the framework to ignore this annotation in the file loader? swagger-php parses the files on its own so it dosent really care if this is imported or not. I also tried importing the Openapi Annotations but this did not work either. Any help into this is greatly appreciated!

Upvotes: 3

Views: 2734

Answers (1)

HFinch
HFinch

Reputation: 626

This is somewhat embarrasing. My initial thought was, that having the annotations.yaml would somehow try to load the Annotations and I was correct. To resolve this errer I simply had to add:

use OpenApi\Annotations as OA;

To the files using the annotations and it works.

Upvotes: 6

Related Questions