DarkaOnLine / L5-Swagger

OpenApi or Swagger integration to Laravel
https://github.com/DarkaOnLine/L5-Swagger
MIT License
2.64k stars 394 forks source link

Skipping unknown \Controller #457

Closed tahasethwala closed 2 years ago

tahasethwala commented 2 years ago

Description: After installing as per this post: https://blog.quickadminpanel.com/laravel-api-documentation-with-openapiswagger/ I created a Controller name Controller.php at path: app/Http/Controller/Api/Controller.php. And this file contains as below

<``` ?php

class Controller { /**

Steps To Reproduce:

After that I did php artisan l5-swagger:generate

image

-->

DarkaOnLine commented 2 years ago

Your annotations should go before class Controller:

<?php

/**
     * @OA\Info(
     *      version="1.0.0",
     *      title="Laravel OpenApi Demo Documentation",
     *      description="L5 Swagger OpenApi description",
     *      @OA\Contact(
     *          email="admin@admin.com"
     *      ),
     *      @OA\License(
     *          name="Apache 2.0",
     *          url="http://www.apache.org/licenses/LICENSE-2.0.html"
     *      )
     * )

     *
     * @OA\Tag(
     *     name="Projects",
     *     description="API Endpoints of Projects"
     * )
    */
class Controller
{

}
tahasethwala commented 2 years ago

I did as mentioned by you

File: app/Http/Controllers/Api/Controller.php

<?php

/**
     * @OA\Info(
     *      version="1.0.0",
     *      title="Laravel OpenApi Demo Documentation",
     *      description="L5 Swagger OpenApi description",
     *      @OA\Contact(
     *          email="admin@admin.com"
     *      ),
     *      @OA\License(
     *          name="Apache 2.0",
     *          url="http://www.apache.org/licenses/LICENSE-2.0.html"
     *      )
     * )

     *
     * @OA\Tag(
     *     name="Projects",
     *     description="API Endpoints of Projects"
     * )
    */

class Controller
{

}

But getting same error.

DarkaOnLine commented 2 years ago

Not sure if it's just copy/paste, but can you try to remove the blank line between class and annotations? Not sure if this could help though

DerManoMann commented 2 years ago

The reason is that swagger-php cannot load the class \Controller.

The library relies on autoloading in order to use PHP reflections. Unless you have custom autoload rules in your composer.json I would think the file needs to be in the proper namespace. Have you actually tried the endpoint itself? I'd be surprised if Laravel can find it...

namespace App/Http/Controller/Api;
tahasethwala commented 2 years ago

Yes I tried this too:

<?php

namespace App\Http\Controller\Api;

 /**
 * @OA\Info(
 *      version="1.0.0",
 *      title="Laravel OpenApi Demo Documentation",
 *      description="L5 Swagger OpenApi description",
 *      @OA\Contact(
 *          email="admin@admin.com"
 *      ),
 *      @OA\License(
 *          name="Apache 2.0",
 *          url="http://www.apache.org/licenses/LICENSE-2.0.html"
 *      )
 * )
 *
 * @OA\Server(
 *      url=L5_SWAGGER_CONST_HOST,
 *      description="Demo API Server"
 * )
 *
 * @OA\Tag(
 *     name="Projects",
 *     description="API Endpoints of Projects"
 * )
 */
class Controller
{

}
DarkaOnLine commented 2 years ago

Could this be related to this: this:https://github.com/zircote/swagger-php/issues/1136 and this comment: https://github.com/zircote/swagger-php/issues/1136#issuecomment-1048238037

DerManoMann commented 2 years ago

It definitel comes down to autoloading. Does your controller implement an actual endpoint that works (via Laravel)?

KellsWorks commented 2 years ago

After adding in your base controller (Or any controller without Request calls):

/**

Make sure you add the proper annotations on top of every method in API Controllers. It worked for me.

For example:

/**

WhiteShadow77 commented 1 year ago

I Had similar problem. I had error message "Skipping unknown \Category" Before I had created classe in files: app\Virtual\Models\Category.php and app\Virtual\Resoures\CategoryResource.php. And when I wrote: namespace App\Virtual\Models; (in Category.php) and namespace App\Virtual\Resources; (in CategoryResource.php), problem has gone.