laravel-openapi icon indicating copy to clipboard operation
laravel-openapi copied to clipboard

ReflectionException with controller from `make:controller --api`

Open h3xx opened this issue 3 years ago • 1 comments

The following error is seen when generating the OpenAPI JSON from the command line (artisan openapi:generate). This is for a bare controller with no editing beyond the Laravel boilerplate for artisan make:controller --api; no #[attribute] annotations were added to the controller, only a Route::resource() entry in routes/api.php.

   ReflectionException

  Method App\Http\Controllers\FooController::create() does not exist

  at vendor/vyuldashev/laravel-openapi/src/RouteInformation.php:75
     71▕                 ]);
     72▕             }
     73▕
     74▕             $reflectionClass = new ReflectionClass($controller);
  ➜  75▕             $reflectionMethod = $reflectionClass->getMethod($action);
     76▕
     77▕             $docComment = $reflectionMethod->getDocComment();
     78▕             $docBlock = $docComment ? DocBlockFactory::createInstance()->create($docComment) : null;
     79▕

      +4 vendor frames
  5   [internal]:0
      Vyuldashev\LaravelOpenApi\Builders\PathsBuilder::Vyuldashev\LaravelOpenApi\Builders\{closure}(Object(Illuminate\Routing\Route))

      +19 vendor frames
  25  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

Steps to Reproduce

  1. Start a new Laravel project using PHP 8.1.6
composer create-project laravel/laravel myproject
cd myproject
  1. Run the following commands to add dependencies and make a controller:
composer require 'php=^8.0'
composer require 'vyuldashev/laravel-openapi'
php artisan make:controller --api FooController
  1. Add the following line to the routes/api.php file:
Route::resource('/foo', App\Http\Controllers\FooController::class);
  1. Run php artisan openapi:generate.

This results in the error I included above.

Extra Info

Click for composer.json contents
{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "php": "^8.0",
        "guzzlehttp/guzzle": "^7.2",
        "laravel/framework": "^9.11",
        "laravel/sanctum": "^2.14.1",
        "laravel/tinker": "^2.7",
        "vyuldashev/laravel-openapi": "^1.5"
    },
    "require-dev": {
        "fakerphp/faker": "^1.9.1",
        "laravel/sail": "^1.0.1",
        "mockery/mockery": "^1.4.4",
        "nunomaduro/collision": "^6.1",
        "phpunit/phpunit": "^9.5.10",
        "spatie/laravel-ignition": "^1.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

h3xx avatar Jun 10 '22 16:06 h3xx

Hi @h3xx, I'm not quite sure if this issue is still present on your side, but you're referencing a method which does not exist (as the exception says)

Please see https://laravel.com/docs/9.x/controllers#restful-partial-resource-routes and use the ->only or ->except function to prevent the resource function to expose endpoints which do not exist

mrmstn avatar Oct 05 '22 15:10 mrmstn