knuckleswtf / scribe

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

Is there a way to handle differently query string array parameters? #638

Open aledeg opened 1 year ago

aledeg commented 1 year ago

I have defined a route using a form request to handle my query parameters. When I make requests on my route, the filter handled by the query parameter is working properly but the documentation does not reflect how to use them. My filter is defined as filter.something in the form request. It is documented as filter.something both in the HTML file and the Postman collection. But in order to trigger it, I need to input filter[something] in the URL. This makes both the HTML file and the Postman collection useless.

Am I missing something ?

Here is an example of a form request I use in an index resource request:

<?php

declare(strict_types=1);

namespace App\Http\Requests\Api;

use Illuminate\Foundation\Http\FormRequest;

class IndexRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'page.size' => [
                'numeric',
                'min:1',
                'max:100',
            ],
        ];
    }

    public function queryParameters(): void
    {
    }
}

This renders into: Capture d’écran, le 2023-03-24 à 08 58 20 And: Capture d’écran, le 2023-03-24 à 09 00 23

When I try to apply the later while making a Postman request, it has no effect unless I rewrite it to page[size].

I have tried to rewrite the name on the code level by leveraging the queryParameters method but it does not support such thing.

What will be the best course of action to overcome this situation? Is there something I could to on my hand or is it something the library should handle?

Thank you


My environment:

My Scribe config (from php artisan scribe:config:diff):

title => "API"
try_it_out.enabled => false
auth.enabled => true
auth.name => "Authorization"
logo => "../media/logos/logo.light.png"
shalvah commented 1 year ago

First off, the standard warning: there is no single standard for query parameters as objects/arrays. https://blog.shalvah.me/posts/fun-stuff-representing-arrays-and-objects-in-query-strings

Re your question: this might work. Try adding a top-level page rule, something like:


        return [
            'page' => ['object'],
            'page.size' => [ ... ],
        ];

This should make Scribe see it as an object with a nested field.

aledeg commented 1 year ago

Thank you for your answer. I've tried that but unfortunately, it does not work. Could it be possible to be able to override parameter names in the queryParameters method? This way, it will be easy to customize without compromising the existing behavior.

shalvah commented 1 year ago

To clarify, the only problem here is the parameter name displayed in the HTML and Postman collection? Other things, like Try It Out, work as expected, yeah?

aledeg commented 1 year ago

I haven't tried that. To be honest, I've never used that. I'll try and get back to you.

aledeg commented 1 year ago

So, I've made some tests. Try It Out works with my parameter as page.size. But it does not work when using in the Postman collection.

While using the collection, I need to manually change page.size to page[size].

shalvah commented 1 year ago

Hmmm 🤔