L5-Swagger icon indicating copy to clipboard operation
L5-Swagger copied to clipboard

Component/Schema not found

Open kevinpallado opened this issue 3 years ago • 3 comments

  • L5-Swagger Version: #.#.# (composer show | grep l5-swagger) : 8.3.1
  • PHP Version (php -v): 8.0.17
  • OS: Ubuntu 20.04.3 LTS

Description:

As I am following the steps how it is done here I got this error says

image

Steps To Reproduce:

Add this to your controller

/**
  * @OA\Get(
  *     path="/accounts",
  *     tags={"Account"},
  *     summary="Get list of accounts",
  *     @OA\Response(
  *          response=200,
  *          description="Successful operation",
  *          @OA\JsonContent(ref="#/components/schemas/AccountsResource")
  *     )
  * )
  */

Run php artisan l5-swagger:generate

kevinpallado avatar Apr 20 '22 08:04 kevinpallado

Looks like the schema for AccountsResource is not found.

So either you do not have annotated that resource at all or for some reason the annotations for it is in a place where PHP reflection cannot find it. Typically this happens for annotations that are not associated with a class/method or similar. The swagger-php FAQ has some more details.

DerManoMann avatar Apr 20 '22 20:04 DerManoMann

@DerManoMann I tried to use my own resource replacing

@OA\JsonContent(ref="#/components/schemas/AccountsResource") into

@OA\JsonContent(ref="App\Http\Resources\AccountsResource") and then I run generate all good.

But then have this error image

How do I reference my own json resource response?

kevinpallado avatar Apr 21 '22 04:04 kevinpallado

Using "#/components/schemas/AccountsResource" is the correct way.

My guess as to why this is not working is that the AccountsResource annotation is not found.

One way to prove this would be to take out that reference at all from your controller, so you get a working spec. Then you can inspect the generated JSON/YAML and I would expect that AccountsResource is missing in there.

If that is the case then it would be good if we could see the annotation for that and also if that annotation has a class or other structural element associated. What I mean with that is something like:

/**
 * @OA\Schema()
 */
class AccountsResource { /* .... */}

Without the class AccountsResource { /* .... */} the code will ignore the annotation and therefore it cannot be referenced.

DerManoMann avatar Apr 21 '22 22:04 DerManoMann

where to put #/components/schemas/AccountsResource actually when I am using Laravel??

rickysut avatar Jun 17 '23 03:06 rickysut

Hii,

@DerManoMann, @DarkaOnLine Can you please show this with some code reference?

bhattaxay avatar Aug 09 '23 19:08 bhattaxay

All files/classes must be autoloadable and all direcories in the list of scanned directories (which should be the case using L5)

Controller.php

<?php

namespace App\Http\Controllers;

use OpenApi\Annotations as OA;

/**
 * @OA\Get(
 *     path="/accounts",
 *     tags={"Account"},
 *     summary="Get list of accounts",
 *     @OA\Response(
 *          response=200,
 *          description="Successful operation",
 *          @OA\JsonContent(ref="#/components/schemas/AccountsResource")
 *     )
 * )
 */
class Controller {}

AccountsResource.php

<?php

namespace App\Http\Resources;

use OpenApi\Annotations as OA;

/**
 * @OA\Schema()
 */
class AccountsResource {}

Api.php (this is for shared annotations, etc)

<?php

namespace App;

use OpenApi\Annotations as OA;

/**
 * @OA\Info(title="Components", version="0.1")
 */
class Api {}

spec

openapi: 3.0.0
info:
  title: Components
  version: '0.1'
paths:
  /accounts:
    get:
      tags:
        - Account
      summary: 'Get list of accounts'
      responses:
        '200':
          description: 'Successful operation'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AccountsResource'
components:
  schemas:
    AccountsResource: {  }

DerManoMann avatar Aug 09 '23 20:08 DerManoMann