swagger-php: Warning: Skipping unknown \Controller Warning: Required @OA\PathItem() not found Warning: Required @OA\Info() not found

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 © The PHP Group Zend Engine v3.4.0, Copyright © Zend Technologies with Zend OPcache v7.4.30, Copyright ©, 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

无标题

About this issue

  • Original URL
  • State: closed
  • Created 7 months ago
  • Reactions: 1
  • Comments: 20

Most upvoted comments

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.

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