1
votes

We are having issues autoloading JMSSerializer annotations in a symfony app. We are getting: [Semantical Error] The annotation "@JMS\Serializer\Annotation\XMLRoot" in class Class\Namespace\ClassName does not exist, or could not be auto-loaded.

We are using the standard symfony/composer autoloader, have "jms/serializer-bundle": "~1.0" required in composer.json, and include the bundle in the AppKernel. Other annotations (e.g symfony route annotations) work correctly.

We tried forcing to load the jms serializer annotations by modifying app_dev.php to:

<?php

use Doctrine\Common\Annotations\AnnotationRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;

// If you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
//umask(0000);

$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
Debug::enable();

require_once __DIR__.'/../app/AppKernel.php';

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

AnnotationRegistry::registerFile("/srv/httpd/project/vendor/jms/serializer/src/JMS/Serializer/Annotation/XmlRoot.php");
AnnotationRegistry::registerAutoloadNamespace('JMS\Serializer', "/srv/httpd/project/vendor/jms/serializer/src/");

$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

We tried several variations of the AnnotationRegistery::... calls. AnnotationRegistry::registerFile("/srv/httpd/project/vendor/jms/serializer/src/JMS/Serializer/Annotation/XmlRoot.php"); seems to register correctly the XmlRoot annotations but other JMS annotations still fail.

Thanks.

4
The namespace is imported as use JMS\Serializer\Annotation as JMS and the annotation is @JMS\XMLRootroinir
Try to change the JMS alias to something else then JMS. Perhaps it's clashing with the JMS namespace. Also, I'm not sure why you've put additional calls to registerFile() and registerAutoloaderNamespace(). Call to registerLoader() should be sufficient.Jakub Zalas
Changed the alias to something else doesn't work. I've put the other call there to illustrate our attempts to fixing the issue (as far as I understand the annotations are loaded in bootstrap.php.cache so we don't need to add anything there?)roinir
Note that the call to registerFile() did register the XmlRoot annotation correctly but obv it then failed in the next JMS annotation so it seems like we could manually register all the annotations and that would work but it seems like the wrong solution.roinir
Remove the calls to registerFile() and registerAutoloaderNamespace(). You don't need them. Dump composer's autoload classmap and check if annotation classes can be found in there.Jakub Zalas

4 Answers

2
votes

you have to edit the vendor/autoload.php file,like below:

// autoload.php @generated by Composer
use Doctrine\Common\Annotations\AnnotationRegistry;

require_once __DIR__ . '/composer/autoload_real.php';

$loader = ComposerAutoloaderInitb6ddad78dfb081b4ad47d02feb034c25::getLoader();
AnnotationRegistry::registerLoader([$loader, 'loadClass']);

return $loader;
0
votes

Try to import namespace to Your Entity with an alias:

<?php

use JMS\Serializer\Annotation as JMS

class SomeClass
{
    /**
     * @JMS/XMLRoot
     */
    public $someProperty
}

Got same problem with Doctrine elsewhere.

0
votes

Another possible reason for the same error:-

[Semantical Error] The annotation "@JMS\Serializer\Annotation\XMLRoot" in
class Class\Namespace\ClassName does not exist, or could not be auto-loaded.

is a missing call to AnnotationRegistry::registerLoader.

0
votes

See documentation here - http://jmsyst.com/libs/serializer/master/configuration

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');