skip to Main Content

I am installed

composer require "darkaonline/l5-swagger:5.6.*",

and i have a api-docs.json from a swagger that I added to storage/api-docs/api-docs.json.
But when i go to

http://127.0.0.1:8000/api/documentation

I got error

Fetch error Not Found http://127.0.0.1:8000/docs/api-docs.json

I tried to run

php artisan l5-swagger:generate

but it logically returns an error

Required @OAInfo() not found

because it wants me to manually enter all the api information in the classes, but my api is quite large so it will take me a long time to rewrite it.

Can darkaonline/l5-swagger just read my api-docs.json file from the storage?

2

Answers


  1. You need to add the Info as mentioned in the error.

    A simple, yet powerful solution would be adding it to AppHttpController::class:

    <?php
    
    namespace AppHttpControllers;
    
    use IlluminateFoundationAuthAccessAuthorizesRequests;
    use IlluminateFoundationValidationValidatesRequests;
    use IlluminateRoutingController as BaseController;
    use OpenApiAttributes as OA;
    
    
    
    #[OAInfo(title: "My First API", version: "0.1",)]
    
    class Controller extends BaseController
    {
        use AuthorizesRequests, ValidatesRequests;
    }
    

    This will ensure that the Info is added to all your requests as well.

    You can then easily add your PathItems, as en example for AppHttpControllerApiHeartbeatController::class:

    <?php
    
    namespace AppHttpControllersApi;
    
    use AppHttpControllersController;
    use OpenApiAttributes as OA;
    
    class HeartbeatController extends Controller
    {
        #[OAGet(
            path: '/heartbeat',
            responses: [
                new OAResponse(response: '200', description: 'API is running', content: new OAJsonContent(example: ['status' => 'ok', 'message' => 'API is running']))
            ],
        )]
        public function __invoke(): IlluminateHttpJsonResponse
        {
            return response()->json([
                'status' => 'ok',
                'message' => 'API is running',
            ]);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search