scribe icon indicating copy to clipboard operation
scribe copied to clipboard

Query and URL Parameters Use Together

Open oralunal opened this issue 1 year ago • 2 comments

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 avatar Jun 21 '23 22:06 oralunal

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.

oralunal avatar Jun 22 '23 22:06 oralunal

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:

  • all parameters will be body parameters by default
  • if you want any parameters in the array to be query or URL parameters, add them to a queryParameters() or urlParameters() method

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

shalvah avatar Jun 30 '23 21:06 shalvah