swagger-php and doctrine annotation not playing nice.

Created on 12 Dec 2013  路  14Comments  路  Source: zircote/swagger-php

Hi,

I currently have swagger-php generating JSON formatted documentation through the commandline util swagger.phar.

I am trying to do the same now for the models/OM but I am having issues with this. Our project currently uses Doctrine's @ ORM annotation for DB mapping and the two seem to be clashing.

My main question is (as I am not experienced with this) is @ ORM and @ SWG model annotations compatible? And if they are, do you have an example of them working in harmony?

If I include both in my annotation, the @ ORM annotation fails, causing my application to error. E.g:

https://gist.github.com/scoch/fd3efc5bb1e11a2f7fb1

I've tried swapping their order various other ways but to no avail, it usually results in a "message": "An exception was raised while creating \"classnamehere\"; no instance returned", error or the @ SWG model annotation above the class being ignored/ commandline error.

Thanks for any assistance you can provide.

Regards,
Steven

Most helpful comment

Now that "SWG" has been replaced by "OA" for OpenAPI 3, the command becomes

AnnotationReader::addGlobalIgnoredNamespace("OA");

This solved the issue for me in working with both swagger-php and Doctrine ORM.

All 14 comments

The deeper error of them clashing:

exception 'Doctrine\Common\Annotations\AnnotationException' with message '[Semantical Error] The annotation "@Swagger\Annotations\Model" in class CLASSNAME does not exist, or could not be auto-loaded.' in PROJECT/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php:52

Is the doctrine script able to autoload Swagger\Annotations\Model ?

$aQuickTest = new \Swagger\Annotations\Model();
/**
 * @SWG\Model(...

Hi, the above code did not get ride of the swagger error. Although I have been making progress this morning, I got Doctrine to ignore the SWG annotations after much experimenting:

AnnotationReader::addGlobalIgnoredName('SWG\Model');

This however has me faced with a new and confusing error :
Fatal error: Class 'Swagger\Annotations\Logger' not found in phar:///STUFF/swagger.phar/library/Swagger/Annotations/Items.php on line 78

More prodding revealed:

 // Validate if items are inside a container type.
        if ($annotation->items !== null) {
            if ($annotation->type !== 'array') {
                Logger::warning('Unexcepted items for type "'.$annotation->type.'" in '.$annotation->identity().', expecting "array"');
                $annotation->items = null;
            } else {

This occurs when I add the annotation * items="$ref:ClassName", to my GET operation on the controller function annotation.

Ah, i copied validation logic into Items.php but forgot to add use Swagger\Logger

Okay, no problem. Any idea what would typically cause the underlying error of 'Unexcepted items for type'?

The swagger configures the parser with DocParser->setIgnoreNotImportedAnnotations(true); maybe you can set this option for doctrine too?

Is this still in relation to Doctrine trying to read the swagger @SWG annotations? If so I sorted it with adding 'AnnotationReader::addGlobalIgnoredName('SWG\Model');' to the top of the class.

If it's related to the new error I am getting in regards to the logger / 'Unexcepted items for type', I am getting it when running the swagger.phar cmd line tool and I don't think Doctrine is related to the error.

@SWG\Propery(type="array",items="$ref:Model") // Correct: items are allowed for arrays.

@SWG\Propery(type="string",items="$ref:Model") // Wrong: a `string` doesn't allow items 

You sir, are my saviour. It has been a difficult journey getting it working, but I am stepping out a humbled man. And I can't believe I didn't think of the type reflecting the model instead of the attributes. I feel slightly disappointed in myself.

Thanks again for your assistance, it is good to finally see the model being outputted in all its glory.

Glad to help!

I just wanted to mention I also was seeing this problem, but fixed it by adding this into my Doctrine bootstrap:

\Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace("Swagger", $CONFIG['directories']['vendor'] . '/zircote/swagger-php/library');

There's a decent explanation of how Doctrine looks for Annotation classes here: http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/annotations.html#introduction

Edit: I'm referring to this problem: The annotation "@Swagger\Annotations\Model" in class CLASSNAME does not exist, or could not be auto-loaded.

you can AnnotationReader::addGlobalIgnoredNamespace("SWG");

Now that "SWG" has been replaced by "OA" for OpenAPI 3, the command becomes

AnnotationReader::addGlobalIgnoredNamespace("OA");

This solved the issue for me in working with both swagger-php and Doctrine ORM.

@dennisameling thank you!

My error in symfony:

HTTP 500 Internal Server Error [Semantical Error] The annotation "@OA\Post" in method

My solve the problem - add into the file config/bootstrap.php next string:
```
\Doctrine\Common\AnnotationsAnnotationReader::addGlobalIgnoredNamespace("OA");
````
Result:
image

Was this page helpful?
0 / 5 - 0 ratings