zircote / swagger-php

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

Warning: Skipping unknown \Controller Warning: Required @OA\PathItem() not found Warning: Required @OA\Info() not found #1519

Closed rongzedong closed 10 months ago

rongzedong commented 11 months ago

install :

composer require zircote/swagger-php composer require doctrine/annotations

ENV:

`a@debian:/var/www$ uname -a Linux debian 5.10.0-18-amd64 #1 SMP Debian 5.10.140-1 (2022-09-02) x86_64 GNU/Linux

a@debian:/var/www$ php -v PHP 7.4.30 (cli) (built: Jul 7 2022 15:51:43) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies `

swagger-php version (maybe) 4.8.1 ?

a@debian:/var/www$ ./vendor/bin/openapi --version

Error: Specify at least one path.

Usage: openapi [--option value] [/path/to/project ...]

Options: --config (-c) Generator config ex: -c operationId.hash=false --legacy (-l) Use legacy TokenAnalyser; default is the new ReflectionAnalyser --output (-o) Path to store the generated documentation. ex: --output openapi.yaml --exclude (-e) Exclude path(s). ex: --exclude vendor,library/Zend --pattern (-n) Pattern of files to scan. ex: --pattern "*.php" or --pattern "/.(phps|php)$/" --bootstrap (-b) Bootstrap php file(s) for defining constants, etc. ex: --bootstrap config/constants.php --processor (-p) Register an additional processor. --format (-f) Force yaml or json. --debug (-d) Show additional error information. --version The OpenAPI version; defaults to 3.0.0. --help (-h) Display this help message.

try :

a@debian:/var/www$ ./vendor/bin/openapi -b ./vendor/autoload.php api Warning: Skipping unknown \Controller Warning: Required @OA\PathItem() not found Warning: Required @OA\Info() not found openapi: 3.0.0

try again:

a@debian:/var/www$ ./vendor/bin/openapi api Warning: Skipping unknown \Controller Warning: Required @OA\PathItem() not found Warning: Required @OA\Info() not found openapi: 3.0.0

demo...:

a@debian:/var/www$ cat api/doc.php

无标题

rongzedong commented 11 months ago

After the installation was completed, I copied a simple demo, but an error occurred. After reading the documentation, I didn't find how to solve it. I don't know how to use it, or it's a bug?

DerManoMann commented 11 months ago

Swagger-php requires the code that it processes to be autoloadabe. Have a look here: https://zircote.github.io/swagger-php/guide/faq.html#warning-required-oa-info-not-found

rongzedong commented 10 months ago

no, have a easy demo ?

DerManoMann commented 10 months ago

Using -b to bootstrap composer autoloading is a good first step, but you will have to make sure your classes are correctly registered in composer.json too. Seems they do not use namespaces so I would suspect that is not the case.

If you are only playing with a single demo .php file then you can also use the bootstrap option to just pre-load that file.

 ./vendor/bin/openapi -b api/doc.php api/doc.php
GlasgowKid commented 10 months ago

Could you specify, what you mean by autoloadable? Or could you give an example how composer.json should look, so that the classes are registered correctly?

In my case I could use composer only to install swagger-php locally by calling php composer.phar require zircote/swagger-php, i.e. composer is not running globally but locally.

DerManoMann commented 10 months ago

https://getcomposer.org/doc/01-basic-usage.md#autoloading

matmper commented 10 months ago

I had the same problem in all my projects when upgrading from version 4.7 to 4.8. For now, I locked the package version: "zircote/swagger-php": "4.7.16"

DerManoMann commented 10 months ago

@matmper in your case I'd wager that it is the missing doctrine/annotations dependency which got made optional in 5.8

rongzedong commented 10 months ago

Could you specify, what you mean by autoloadable? Or could you give an example how composer.json should look, so that the classes are registered correctly?

In my case I could use composer only to install swagger-php locally by calling php composer.phar require zircote/swagger-php, i.e. composer is not running globally but locally.

cat composer.json { "require": { "zircote/swagger-php": "^4.8", "doctrine/annotations": "^2.0" } }

DerManoMann commented 10 months ago

@rongzedong you are missing a autoload section that specifies how to autoload your application code. Something like

    "autoload": {
        "psr-4": {
            "My_API_Namespace\\": "api/"
        }
    },
rongzedong commented 10 months ago

@rongzedong you are missing a autoload section that specifies how to autoload your application code. Something like

    "autoload": {
        "psr-4": {
            "My_API_Namespace\\": "api/"
        }
    },

ron@debian:/var/www$ ./vendor/bin/openapi -b ./vendor/autoload.php api Warning: Skipping unknown \Controller Warning: Required @OA\PathItem() not found Warning: Required @OA\Info() not found openapi: 3.0.0

ron@debian:/var/www$ cat composer.json { "require": { "zircote/swagger-php": "^4.8", "doctrine/annotations": "^2.0" },

"autoload": {
    "psr-4": {
        "My_API_Namespace\\": "api/"
    }
},

}

DerManoMann commented 10 months ago

Well, you should replace My_API_Namespace with whatever namespace your code uses... You really need to get your autoloading sorted first, sorry.

rongzedong commented 10 months ago

> Well, you should replaceMy_API_Namespace` with whatever namespace your code uses... You really need to get your autoloading sorted first, sorry.

ron@debian:/var/www$ ./vendor/bin/openapi api Warning: Skipping unknown demo\OpenApi Warning: Skipping unknown demo\MyController Warning: Skipping unknown demo\Controller Warning: Required @OA\PathItem() not found Warning: Required @OA\Info() not found openapi: 3.0.0

ron@debian:/var/www$ cat api/doc.php |more \<?php namespace demo; `use OpenApi\Annotations as OA;` /** * @OA\Info( * title="My First API", * version="0.1" * ) */ class OpenApi {} `class MyController {` /** * @OA\Get( * path="/api/data.json", * @OA\Response( * response="200", * description="The data" * ) * ) ron@debian:/var/www$ cat composer.json { "require": { "zircote/swagger-php": "^4.8", "doctrine/annotations": "^2.0" }, ` "autoload": {`

"psr-4": {

"demo\\": "api/"

}

},

``

}

ron@debian:/var/www$ cat openapi.yaml openapi: 3.0.0 ``

rongzedong commented 10 months ago

ron@debian:/var/www$ ./vendor/bin/openapi api -b ./vendor/autoload.php Warning: Skipping unknown demo\OpenApi Warning: Skipping unknown demo\MyController Warning: Skipping unknown demo\Controller Warning: Required @OA\PathItem() not found Warning: Required @OA\Info() not found openapi: 3.0.0

rongzedong commented 10 months ago

somebody give me a simple project demo? like this:

demodir/demo.php

rongzedong commented 10 months ago

Using -b to bootstrap composer autoloading is a good first step, but you will have to make sure your classes are correctly registered in composer.json too. Seems they do not use namespaces so I would suspect that is not the case.

If you are only playing with a single demo .php file then you can also use the bootstrap option to just pre-load that file.

 ./vendor/bin/openapi -b api/doc.php api/doc.php

it's running ok.

./vendor/bin/openapi api -b api/doc.php Warning: Only one @OA\Info() allowed for @OA\OpenApi() multiple found in: Using: /var/www/api/doc.php on line 12 Skipped: /var/www/api/doc.php on line 33 openapi: 3.0.0 info: title: 'My First API' version: '0.1' paths: /api/data.json: get: operationId: ba1b14bced5bf0c7483fd4b5477bfb7e responses: '200': description: 'The data' '/api/v0.0.2/notifications/{id}': delete: summary: 'Delete notification by ID.' operationId: deleteNotificationById parameters:

      name: id
      in: path
      schema:
        type: integer
  responses:
    '200':
      description: OK
    '400':
      description: 'Bad Request'

but how put this config in to composer.json?

DerManoMann commented 10 months ago
require zircote/swagger-php doctrine/annotations

composer.json

{
    "require": {
        "zircote/swagger-php": "^4.8",
        "doctrine/annotations": "^2.0"
    }
}
mkdir api

Add autoload config to composer.json composer.json

{
    "require": {
        "zircote/swagger-php": "^4.8",
        "doctrine/annotations": "^2.0"
    },
    "autoload": {
        "psr-4": {
            "demo\\": "api/"
        }
    }
}

You might be missing this step:

composer dumpautoload

see: https://getcomposer.org/doc/01-basic-usage.md#autoloading

api/OpenApi.php

<?php

namespace demo;

use OpenApi\Annotations as OA;

/**
 * @OA\Info(
 *     title="My First API",
 *     version="0.1"
 * )
 */
class OpenApi
{
}

api/MyController.php

<?php

namespace demo;

use OpenApi\Annotations as OA;

class MyController
{
    /**
     * @OA\Get(
     *     path="/api/data.json",
     *     @OA\Response(
     *         response="200",
     *         description="The data"
     *     )
     * )
     */
    public function data()
    {
    }
}

For autoloading each file must contain only a single class/interface/... and the filename must match the classname.

> $ ./vendor/bin/openapi api
openapi: 3.0.0
info:
  title: 'My First API'
  version: '0.1'
paths:
  /api/data.json:
    get:
      operationId: 39e76b66fba13d233ee1e73caaec0251
      responses:
        '200':
          description: 'The data'

Finally, using ``` to wrap code makes your post a lot more readable... see: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks

rongzedong commented 10 months ago

thk's for your help. but it's not running ok:

ron@debian:/var/www$ php -v
PHP 7.4.30 (cli) (built: Jul  7 2022 15:51:43) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies

ron@debian:/var/www$ composer dump-autoload
Generating autoload files
Generated autoload files

ron@debian:/var/www$ cat composer.json
{
    "require": {
        "zircote/swagger-php": "^4.8",
        "doctrine/annotations": "^2.0"
    },

    "autoload": {
        "psr-4": {
            "demo\\": "api/doc.php"
        }
    }

}
ron@debian:/var/www$ ls api/
MyController.php  OpenApi.php

ron@debian:/var/www$ cat api/MyController.php
<?php

namespace demo;

use OpenApi\Annotations as OA;

class MyController
{
    /**
     * @OA\Get(
     *     path="/api/data.json",
     *     @OA\Response(
     *         response="200",
     *         description="The data"
     *     )
     * )
     */
    public function data()
    {
    }
}
ron@debian:/var/www$ cat api/OpenApi.php
<?php

namespace demo;

use OpenApi\Annotations as OA;

/**
 * @OA\Info(
 *     title="My First API",
 *     version="0.1"
 * )
 */
class OpenApi
{

}
ron@debian:/var/www$ ./vendor/bin/openapi api
Warning: Skipping unknown demo\MyController
Warning: Skipping unknown demo\OpenApi
Warning: Required @OA\PathItem() not found
Warning: Required @OA\Info() not found
openapi: 3.0.0

ron@debian:/var/www$
rongzedong commented 10 months ago
ron@debian:/var/www$ composer require zircote/swagger-php doctrine/annotations
Using version ^4.8 for zircote/swagger-php
Using version ^2.0 for doctrine/annotations
./composer.json has been updated
Running composer update zircote/swagger-php doctrine/annotations
Loading composer repositories with package information
Updating dependencies
Lock file operations: 0 installs, 1 update, 0 removals
  - Upgrading zircote/swagger-php (4.8.1 => 4.8.3)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 1 update, 0 removals
  - Downloading zircote/swagger-php (4.8.3)
  - Upgrading zircote/swagger-php (4.8.1 => 4.8.3): Extracting archive
Generating autoload files
6 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

it's ok now.

after composer require zircote/swagger-php doctrine/annotations, update version. it's ok now.

matodrobec commented 8 months ago

@rongzedong thank you

digininja commented 1 month ago

Using -b to bootstrap composer autoloading is a good first step, but you will have to make sure your classes are correctly registered in composer.json too. Seems they do not use namespaces so I would suspect that is not the case.

If you are only playing with a single demo .php file then you can also use the bootstrap option to just pre-load that file.

 ./vendor/bin/openapi -b api/doc.php api/doc.php

It would be really good to include this in the examples here https://zircote.github.io/swagger-php/ and https://zircote.github.io/swagger-php/guide/generating-openapi-documents.html .

I just wanted to test the tool out so copied the example PHP code, ran the commands provided, and got nothing.