zircote / swagger-php

A php swagger annotation and parsing library
http://zircote.github.io/swagger-php/
Apache License 2.0
5.1k stars 934 forks source link

Class resolver causes parse error #136

Closed thinkjson closed 10 years ago

thinkjson commented 10 years ago

Using SomeClass::class to get the fully resolved class name causes a parse error when constructing a Swagger instance:

exception 'ErrorException' with message 'Uninitialized string offset: 1' in /.../vendor/zircote/swagger-php/library/Swagger/Parser.php:260
Stack trace:
#0 /.../vendor/zircote/swagger-php/library/Swagger/Parser.php(260): Slim\Slim::handleErrors(8, 'Uninitialized s...', '/cdn/cdnws/curr...', 260, Array)
#1 /.../vendor/zircote/swagger-php/library/Swagger/Parser.php(207): Swagger\Parser->parseTokens(Object(Doctrine\Common\Annotations\TokenParser))
#2 /.../vendor/zircote/swagger-php/library/Swagger/Parser.php(97): Swagger\Parser->parseFile('/cdn/cdnws/curr...')
#3 /.../vendor/zircote/swagger-php/library/Swagger/Swagger.php(209): Swagger\Parser->__construct(Array, '/...')
#4 /.../vendor/zircote/swagger-php/library/Swagger/Swagger.php(65): Swagger\Swagger->scan('/cdn/cdnws/curr...', Array)
#5 /.../src/Hwcdn/Web/CdnDocs/Controller/DocsResourceController.php(43): Swagger\Swagger->__construct('/...', Array, NULL)
...

Hard-coding the fully namespaced class name causes the error to go away.

bfanger commented 10 years ago

Could you post the offending annotation?

thinkjson commented 10 years ago

The following code:

<?php

require('../vendor/autoload.php');

use Swagger\Swagger;
use Swagger\Annotations as SWG;

/**
 * @SWG\Resource(resourcePath="/")
 */

/**
 * This is a test
 *
 * @SWG\Api(
 *   path="/docstest.php",
 *   description="Send a test response",
 * @SWG\Operations(
 * @SWG\Operation(
 *       method="GET", summary="Gets a test response"
 *       )
 *   )
 * )
 *
 */
class DocsTest {
    public function test() {
        $cls = Swagger::class;
        $swagger = new Swagger(dirname(__FILE__), []);
        $apiJson = $swagger->getResourceList();
        print json_encode($apiJson);
    }   
}

error_reporting(E_ALL);
$test = new DocsTest();
$test->test();

produces the following notice:

"PHP message: PHP Notice:  Uninitialized string offset: 1 in /.../vendor/zircote/swagger-php/library/Swagger/Parser.php on line 260
PHP message: PHP Stack trace:
PHP message: PHP   1. {main}() /.../public/docstest.php:0
PHP message: PHP   2. DocsTest->test() /.../public/docstest.php:37
PHP message: PHP   3. Swagger\Swagger->__construct() /.../public/docstest.php:29
PHP message: PHP   4. Swagger\Swagger->scan() /.../vendor/zircote/swagger-php/library/Swagger/Swagger.php:65
PHP message: PHP   5. Swagger\Parser->__construct() /.../vendor/zircote/swagger-php/library/Swagger/Swagger.php:209
PHP message: PHP   6. Swagger\Parser->parseFile() /.../vendor/zircote/swagger-php/library/Swagger/Parser.php:97
PHP message: PHP   7. Swagger\Parser->parseTokens() /.../vendor/zircote/swagger-php/library/Swagger/Parser.php:207

Removing the class resolver removes the notice.

bfanger commented 10 years ago

Fixed in 0.9.2 by @alkev

thinkjson commented 10 years ago

Looks good. Thanks!