restsymfonydoctrine-ormparse-platformsymfony-2.5

Doctrine annotation exception when using parse query in Symfony2


I'm trying to make an API Rest in Symfony2 using Parse as cloud database.

If I try to retrieve the Parse users it works fine and returns the expected data.

Local url example: http://www.foo.local/app_dev.php/getUsers/

Here is the code I use in the Users controller (I use annotations in order to set the routes in the controller):

namespace Foo\ApiBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations\View;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Parse\ParseClient;
use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseUser;

class UsersController extends Controller
{
    /**
    * @return array
    * @View()
    * @Route("/getUsers/")
    */
    public function getUsersAction(Request $request) {
        ParseClient::initialize(<my Parse keys>);

        $query = ParseUser::query();     
        $results = $query->find();

        return array('users' => $results);
    } 
}

However if I try the same with my Products ParseObjects, I get the following error message:

error code="500" message="Internal Server Error" exception class="Doctrine\Common\Annotations\AnnotationException" message="[Semantical Error] The annotation "@returns" in method Parse\ParseFile::getData() was never imported. Did you maybe forget to add a "use" statement for this annotation?"

Local url example: http://www.foo.local/app_dev.php/getProducts/

The Products controller code:

namespace Foo\ApiBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations\View;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Parse\ParseClient;
use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseUser;
use Parse\ParseFile;

class ProductsController extends Controller 
{
    /**
     * @return array
     * @View()
     * @Route("/getProducts/")
     */
    public function getProductsAction(Request $request) {
        ParseClient::initialize(<my Parse keys>);

        $query = new ParseQuery("Products");
        $results = $query->find();

        return array('products' => $results);    
    }
}

If instead of returning $results I return other dummy data, like return array('products' => 'fooProducts'), I no longer get the error message.

Also if I make a var_dump of the $results variable, I get the expected array of ParseObjects.

Here is my routing.yml file in case there is something wrong with it:

api:
    resource: "@FooApiBundle/Controller/"
    type:     annotation
    prefix:   /

users:
    type: rest
    resource: Foo\ApiBundle\Controller\UsersController

products:
    type: rest
    resource: Foo\ApiBundle\Controller\ProductsController

By the error message it seems that the problem is related to Doctrine, but since I'm not using it, I don't know exactly how there can be a conflict or how to fix it. Any suggestions?


Solution

  • There are a few DocBlock typos of @returns in the Parse\ParseFile class that is causing Doctrine's Annotations class to attempt to identify them as a class. This is not your fault but a bug in the Parse PHP SDK library.

    I've made a fix in this commit and submitted a pull request back to the original devs, so it should be a simple matter of eventually running composer update to bring your Parse library to the latest correct version.

    You can read more about DocBlock and the part specifically on Annotations here

    Here is a copy/paste of the resulting diff for src/Parse/ParseFile.php:

    @@ -31,7 +31,7 @@ class ParseFile implements \Parse\Internal\Encodable
       /**
        * Return the data for the file, downloading it if not already present.
        *
    -   * @returns mixed
    +   * @return mixed
        *
        * @throws ParseException
        */
     @@ -50,7 +50,7 @@ public function getData()
       /**
        * Return the URL for the file, if saved.
        *
    -   * @returns string|null
    +   * @return string|null
        */
       public function getURL()
       {
     @@ -112,7 +112,7 @@ public function getMimeType()
        * @param string $name The file name on Parse, can be used to detect mimeType
        * @param string $mimeType Optional, The mime-type to use when saving the file
        *
    -   * @returns ParseFile
    +   * @return ParseFile
        */
       public static function createFromData($contents, $name, $mimeType = null)
       {
     @@ -132,7 +132,7 @@ public static function createFromData($contents, $name, $mimeType = null)
        * @param string $name Filename to use on Parse, can be used to detect mimeType
        * @param string $mimeType Optional, The mime-type to use when saving the file
        *
    -   * @returns ParseFile
    +   * @return ParseFile
        */
       public static function createFromFile($path, $name, $mimeType = null)
       {