knuckleswtf / scribe

Generate API documentation for humans from your Laravel codebase.✍
https://scribe.knuckles.wtf/laravel/
MIT License
1.59k stars 281 forks source link

Query and URL Parameters Use Together #692

Open oralunal opened 1 year ago

oralunal commented 1 year ago

Scribe version

4.21.2

PHP version

8.2

Framework

Laravel

Framework version

10.13.5

Scribe config

title => "xxx API Documentation"
type => "laravel"
auth.enabled => true
auth.placeholder => "{BEARER_TOKEN}"

What happened?

I have a form request that validates the email verification URL. I was just trying something.

<?php

namespace App\Http\Requests\Auth;

use Illuminate\Foundation\Http\FormRequest;

class VerifyRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'id' => 'required|integer|exists:users,id',
            'hash' => 'required|string',
            'expires' => 'required|integer',
            'signature' => 'required|string',
        ];
    }

    public function queryParameters(){
        return [
            'expires' => [
                'description' => 'Verification hash expiration time',
                'example' => 1234567890,
            ],
            'signature' => [
                'description' => 'Signature of the verification hash',
                'example' => 'a1b2c3d4e5f6g7h8i9j0',
            ],
        ];
    }

    public function urlParameters(){
        return [
            'id' => [
                'description' => 'The ID of the user to verify',
                'example' => 1,
            ],
            'hash' => [
                'description' => 'Verification hash',
                'example' => 'a1b2c3d4e5f6g7h8i9j0',
            ],
        ];
    }

    public function validationData()
    {
        return array_merge($this->route()->parameters(), $this->all());
    }
}

expires, signature are query parameters and id, hash are url parameters and my route is like below:

Route::get('/verify/{id}/{hash}', 'verify')->name('verify')->where('id', '[0-9]+'); // Make sure to keep this as your route name

If i use both urlParameters() and queryParameters() the output is broken. Output is like below: Ekran görüntüsü 2023-06-22 011140

There are extra id and hash in query params.

Docs

oralunal commented 1 year ago

Another scenario, i just want to verify queryparams and i define the queryparams with anotation, above the controller method. When i generate the document, all the rules in the formrequests seen as bodyparam. If i add an emptry queryParameters() method to the form request. Then it works nice.

shalvah commented 1 year ago

This is a known conflict between Laravel and Scribe. Laravel lets you put all sorts of parameters in the validation rules, but Scribe can only use them for either body or query parameters. Any suggestions? The only way I can think of is this:

Not yet sure how this will work for inline validation rules.