Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with analyser & analysis config parameters #414

Open
jlouche opened this issue Oct 20, 2021 · 3 comments
Open

Bug with analyser & analysis config parameters #414

jlouche opened this issue Oct 20, 2021 · 3 comments

Comments

@jlouche
Copy link

jlouche commented Oct 20, 2021

  • L5-Swagger Version: 8.0.9
  • PHP Version: 7.4.22

Description:

Hello,

I try to implement a custom analysis but it's impossible due to the way Laravel caches the configuration.
I solved the problem but I do not have the rights to create a new branch to do a pull request.

The problem is also present with the analyzer option.

I describe below how to reproduce the problem and fix it.

Thank you

Steps To Reproduce:

If I try to indicate the custom class to use :

        // ...
        'scanOptions' => [
            // ....   

            /**
             * analysis: defaults to a new \OpenApi\Analysis .
             *
             * @see \OpenApi\scan
             */
            'analysis' => CustomAnalysis::class,
            //...
        ]
        //...

When I use the command php artisan l5-swagger:generate, I have a PHP TypeError : Argument 2 passed to OpenApi\Generator::generate() must be an instance of OpenApi\Analysis or null, string given,

And if I try to indicate a new instance of the custom class :

        // ...
        'scanOptions' => [
            // ....   

            /**
             * analysis: defaults to a new \OpenApi\Analysis .
             *
             * @see \OpenApi\scan
             */
            'analysis' => new CustomAnalysis
            //...
        ]
        //...

When I use the command php artisan config:cache, I have a PHP LogicException : Your configuration files are not serializable.

Correction of the problem:

In class \L5Swagger\Generator, in function protected function getScanOptions line 223, I add these lines :

        if (isset($options[self::SCAN_OPTION_ANALYSER]) && is_string($options[self::SCAN_OPTION_ANALYSER])) {
            $options[self::SCAN_OPTION_ANALYSER] = new $options[self::SCAN_OPTION_ANALYSER];
        }

        if (isset($options[self::SCAN_OPTION_ANALYSIS]) && is_string($options[self::SCAN_OPTION_ANALYSIS])) {
            $options[self::SCAN_OPTION_ANALYSIS] = new $options[self::SCAN_OPTION_ANALYSIS];
        }
@AlexandreBellas
Copy link

You can fork the project and open a PR with these changes. I would really appreciate it because I'm having the exact same problem.

@AlexandreBellas
Copy link

My workaround was to create a new command which changes the config file on runtime.

In my case, I couldn't set up a different analyser. So I kept the l5-swagger.php like so:

        // ...
        'scanOptions' => [
            /**
             * analyser: defaults to \OpenApi\StaticAnalyser .
             *
             * @see \OpenApi\scan
             */
            'analyser' => \OpenApi\Analysers\TokenAnalyser::class,

            // ...
        ],
        // ...

Then I created a new command (fitting my needs) in app\Console\Commands\GenerateDocs.php that does something like this:

class GenerateDocs extends Command
{
    protected $signature = 'make:docs';

    protected $description = 'Generate API docs.';

    public function handle() {
        $analyser_class = config('l5-swagger.defaults.scanOptions.analyser');
        config(['l5-swagger.defaults.scanOptions.analyser' => new $analyser_class]);

        $this->call('l5-swagger:generate');
    }
}

Finally, you can run php artisan make:docs to achieve the desired result.
Hope this helps. 😃

@AlexandreBellas
Copy link

I just figured out another fancier way to solve it.
Just add in your boot method of AppServiceProvider

class AppServiceProvider extends ServiceProvider
{
    // ...

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // ...

        $analyser_class = config('l5-swagger.defaults.scanOptions.analyser');
        config(['l5-swagger.defaults.scanOptions.analyser' => new $analyser_class]);
    }

    // ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants