Swagger-php: Conflict with another library's annotations

Created on 25 Nov 2020  路  6Comments  路  Source: zircote/swagger-php

I'm using both zircote/php-swagger and jms/serializer to annotate my API. What I'm seeing is that the moment a php-swagger annotation is processed then all jms/serializer annotations are seemingly lost. Removing all php-swagger annotations fix this. Reordering the annotations results in anything after the first php-swagger annotation failing instead. All jms/serializer annotations before the first php-swagger annotation is fine. I must be doing something wrong as I've seen others use both but I'm unsure what the issue could be.

This is with PHP 7.2 and the latest of zircote/php-swagger and jms/serializer installed via composer. I tried earlier versions of both packages but the results were the same.

use OpenApi\Annotations as OA;
use JMS\Serializer\Annotation\AccessType;
use JMS\Serializer\Annotation\AccessorOrder;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\VirtualProperty;

/**
 * @AccessorOrder("alphabetical")
 * @AccessType("public_method")
 *
 * Class BaseResponse
 *
 * @OA\Schema(
 *     description="Base response",
 *     title="Base response",
 *     schema="BaseResponse"
 * )
 */
class BaseResponse implements IResponse {
        var $errors;

        /**
         * @VirtualProperty
         * @SerializedName("foo")
         *
         * @return string
         */
        public function getFoo() {
                return "foo";
        }

PHP Fatal error: Uncaught Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "@ JMS\Serializer\Annotation\VirtualProperty" in method [redacted]\BaseResponse::getFoo() was never imported. Did you maybe forget to add a "use" statement for this annotation? in [redacted]/releases/20200812082918/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php:39

Most helpful comment

I ran into the same issue while using jms/serializer and it seems that the error does not come from swagger-php but from a misconfiguration : when using jms as a standalone library, you actually have to initialize the annotation registry by yourself (source).

If you are using the standalone library and you want to use annotations, the annotation registry must be initialized:

Doctrine\Common\Annotations\AnnotationRegistry::registerLoader('class_exists');

Adding that simple line fixed my issue.

All 6 comments

This should work - so a few questions:

  • How do you actually run swagger-php? As part of a bundle or stand-alone?
  • What version of doctrine do you use?

It might be the setup of the doctrine autoloader or similar.

You might want to try to add the JMS/Serializer annotations to the swagger-php whitelist:

  Analyser::$whitelist = ['JMS\Serializer\\'];

When running swagger-php I use the stand-alone script so something like:

$ vendor/zircote/swagger-php/bin/openapi api/

That seems to work fine producing yaml output.

I'm using 1.11.1 doctrine/annotations according to composer.

I was able to resolve using your whitelist suggestion. Adding this to the top of my index.php file fixed it.

OpenApi\Analyser::$whitelist[] = 'JMS\Serializer\';

I have the same issue with another library, nelmio/api-doc-bundle

I had to add this to index.php, but it doesn't seem like a viable solution... why is this happening?

// fix for annotations not showing as imported
Analyser::$whitelist = [];
AnnotationRegistry::reset();

After deploying, I noticed it also had issues with symfony @Route...

@rydan313 @akalineskou is it possible for both of you to provide a github repo with a basic PoC showing the errors? please provide any instructions on installation and the commands you are running in the README.md, that would greatly help to hunt down this problem.

Might be related to #894 but I doubt it.

I agree with @Doqnach a small repo to reproduce the issue would be great.

Another question would be how are the JMS annotations lost? I guess you mean "the app breaks as if those annotations do not exist?" ..... hmmm thinking about it maybe its related to both using Doctrine\Common\Annotations\AnnotationRegistry in some incompatible static way?

I ran into the same issue while using jms/serializer and it seems that the error does not come from swagger-php but from a misconfiguration : when using jms as a standalone library, you actually have to initialize the annotation registry by yourself (source).

If you are using the standalone library and you want to use annotations, the annotation registry must be initialized:

Doctrine\Common\Annotations\AnnotationRegistry::registerLoader('class_exists');

Adding that simple line fixed my issue.

Was this page helpful?
0 / 5 - 0 ratings