DarkaOnLine / L5-Swagger

OpenApi or Swagger integration to Laravel

Home Page:https://github.com/DarkaOnLine/L5-Swagger

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Uninitialized string offset: 1

sandygoreraza opened this issue · comments

  • darkaonline/l5-swagger Version ^8.5
  • PHP Version 8.0
  • OS: windows 11
  • Laravel Framework Version 8.83.17

Description:

After running all installation and configuration steps for the package in my laravel project After running command php artisan l5-swagger:generate on my Terminal. I am getting the following error :
errorcode
errorcode

ErrorException

Uninitialized string offset: 1

at C:...............\...........\vendor\zircote\swagger-php\src\Analysers\TokenScanner.php:128
124▕ break;
125▕ }
126▕
127▕ $isInterface = false;
➜ 128▕ $currentName = $namespace . '\' . $token[1];
129▕ $unitLevel = count($stack);
130▕ $units[$currentName] = $initUnit($uses);
131▕ break;
132▕

1 C:..............................\vendor\zircote\swagger-php\src\Analysers\TokenScanner.php:128
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Uninitialized string offset: 1", "C:................................\vendor\zircote\swagger-php\src\Analysers\TokenScanner.php")

2 C:................................\vendor\zircote\swagger-php\src\Analysers\TokenScanner.php:21
OpenApi\Analysers\TokenScanner::scanTokens()

Steps To Reproduce:

below is my Controller definition with annotations :

json([]) } }

@sandygoreraza looks like adding your controller code didn't quite work, could you try adding it again?

@DerManoMann my controller is as follows:

MY CONTROLLER

json(["************"]); } } My route definition : Route::post("assets/trashed",[\App\Http\Controllers\BI\Assets::class,'TrashedAssets']);

2023-06-17_23-39-19

Online PHP Compiler (1)

Online PHP Compiler

ROUTI API DEFINITION
Route::post("assets/trashed",[\App\Http\Controllers\BI\Assets::class,'TrashedAssets']);

Ah, I was looking for something to cut/paste - you can use three backticks to format code without having to use an image...

Details here: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks

For example:

    new OA\Property(
        property: "property name 1",
        type: "string",
        example: "value 1"
    )

/**
   * @OA\Post(
   *     path="/api/assets/trashed",
   *     operationId="restoreTrashedAssets",
   *     tags={"Assets"},
   *     summary="Restore trashed assets",
   *     description="This endpoint restores trashed assets based on the provided date. The date should be in the format 'Y-m-d' or null to restore all trashed assets.",
   *     @OA\RequestBody(
   *         required=true,
   *         description="Date to filter trashed assets",
   *         @OA\MediaType(
   *             mediaType="application/json",
   *             @OA\Schema(
   *                 @OA\Property(property="date", type="string", format="date", example="2023-06-15"),
   *             )
   *         )
   *     ),
   *     @OA\Response(response="200", description="Assets restored successfully"),
   *     @OA\Response(response="400", description="Bad request"),
   *     @OA\Response(response="401", description="Unauthorized"),
   *     security={{"bearerAuth":{}}}
   * )
   */
  public function TrashedAssets(Request $request){
      $date = $request->date ?? today();
      $TrashedAssets = DB::table("asset_management")
          ->select('asset_code')
          ->whereDate('asset_management.deleted_at', $date)
          ->get();
      $total = count($TrashedAssets);
      return response()->json(["success" => true, "message" => "Operation Successfull",
         "totalDisplayRecords" => $total,
          "data" => $TrashedAssets]);

  }

Hmm, that in itself seems fine.

I created a file scratch/offset-1.php with the following content:

<?php

use OpenApi\Annotations as OA;

class Controller
{
    /**
     * @OA\Post(
     *     path="/api/assets/trashed",
     *     operationId="restoreTrashedAssets",
     *     tags={"Assets"},
     *     summary="Restore trashed assets",
     *     description="This endpoint restores trashed assets based on the provided date. The date should be in the
     *     format 'Y-m-d' or null to restore all trashed assets.",
     *     @OA\RequestBody(
     *         required=true,
     *         description="Date to filter trashed assets",
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property(property="date", type="string", format="date", example="2023-06-15"),
     *             )
     *         )
     *     ),
     *     @OA\Response(response="200", description="Assets restored successfully"),
     *     @OA\Response(response="400", description="Bad request"),
     *     @OA\Response(response="401", description="Unauthorized"),
     *     security={{"bearerAuth":{}}}
     * )
     */
    public function TrashedAssets(Request $request)
    {
        $date = $request->date ?? today();
        $TrashedAssets = DB::table("asset_management")
            ->select('asset_code')
            ->whereDate('asset_management.deleted_at', $date)
            ->get();
        $total = count($TrashedAssets);
        return response()->json(["success" => true, "message" => "Operation Successfull",
            "totalDisplayRecords" => $total,
            "data" => $TrashedAssets]);
    }
}

Running swagger-php manually against this file returns some YAML + the expected warning about @OA\Info() missing:

 ./bin/openapi -b scratch/offset-1.php scratch/offset-1.php

Warning: Required @OA\Info() not found
openapi: 3.0.0
paths:
  /api/assets/trashed:
    post:
      tags:
        - Assets
      summary: 'Restore trashed assets'
      description: "This endpoint restores trashed assets based on the provided date. The date should be in the\\n\n     *     format 'Y-m-d' or null to restore all trashed assets."
      operationId: restoreTrashedAssets
      requestBody:
        description: 'Date to filter trashed assets'
        required: true
        content:
          application/json:
            schema:
              properties:
                date:
                  type: string
                  format: date
                  example: '2023-06-15'
              type: object
      responses:
        '200':
          description: 'Assets restored successfully'
        '400':
          description: 'Bad request'
        '401':
          description: Unauthorized
      security:
        -
          bearerAuth: []

So, either it is an issue in combination with L5 or it is related to a different file (or different part of your controller).

Maybe you could try something similar yourself and see where/when things break. Without a way to reproduce the error this is impossible to fix.