knuckleswtf / scribe

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

Try it out sends empty query parameters even if the parameter is an empty value #490

Closed shufo closed 2 years ago

shufo commented 2 years ago

Hi. Thanks for creating the great document generator. Recently I used this and it helped me a lot.

I'd like to ask about a behavior I noticed when I was using the Try it out feature.

What happened?

Current Behaviour

  1. I tried Try it out feature with optional query parameter empty.

e.g.

image

  1. Then it seems Try it out client sends request with empty query parameter.

image

Expect Behaviour

I would like to know if is this a intended behaviour?

When I fix the source below to check if value is empty, it works as expected.

https://github.com/knuckleswtf/scribe/blob/df677461933d26b521e3dd71c5d7bf3b9b0f30c7/resources/js/tryitout.js#L221-L223

Fixed code:

        if (el.type !== "radio" || (el.type === "radio" && el.checked)) {
            if (el.value !== "" && el.required === false) {
                _.set(query, el.name, el.value);
            }
        }

The body parameter appears to work as expected as it does not send any parameters when input is empty.

If this behavior is unintended, please let me know and I will send a PR.

My environment:

➜  ~ php -v
PHP 8.1.6 (cli) (built: May 25 2022 19:55:01) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.6, Copyright (c), by Zend Technologies

My Scribe config (minus the comments):

Config ``` reduce( fn($carry, $item, $key) => sprintf('%s %s: %s', $carry, Str::lower($key), $item) ); return [ 'theme' => 'default', 'title' => '~~~', 'description' => '~~~', 'base_url' => '~~', 'routes' => [ [ 'match' => ['prefixes' => ['api/*'], 'domains' => ['*'], 'versions' => ['v1']], 'include' => [], 'exclude' => [], 'apply' => [ 'headers' => [ 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Device-Type' => '1', 'App-Version' => '1.0.0', ], 'response_calls' => [ 'methods' => ['GET'], 'config' => ['app.env' => 'documentation'], 'queryParams' => [], 'bodyParams' => [], 'fileParams' => [], 'cookies' => [], ], ], ], ], 'type' => 'static', 'static' => ['output_path' => './public/docs'], 'laravel' => ['add_routes' => true, 'docs_url' => '/docs', 'middleware' => [], 'assets_directory' => null], 'try_it_out' => [ 'enabled' => true, 'base_url' => '~~~', 'use_csrf' => false, 'csrf_url' => '/sanctum/csrf-cookie', ], 'auth' => [ 'enabled' => true, 'default' => true, 'in' => 'bearer', 'name' => 'key', 'use_value' => env('SCRIBE_AUTH_KEY'), 'placeholder' => '{ACCESS_TOKEN}', 'extra_info' => << << ['bash', 'javascript', 'php'], 'postman' => ['enabled' => true, 'overrides' => []], 'openapi' => ['enabled' => true, 'overrides' => []], 'default_group' => '~~', 'logo' => false, 'faker_seed' => null, 'strategies' => [ 'metadata' => [Strategies\Metadata\GetFromDocBlocks::class], 'urlParameters' => [ Strategies\UrlParameters\GetFromLaravelAPI::class, Strategies\UrlParameters\GetFromLumenAPI::class, Strategies\UrlParameters\GetFromUrlParamTag::class, ], 'queryParameters' => [ Strategies\QueryParameters\GetFromFormRequest::class, Strategies\QueryParameters\GetFromInlineValidator::class, Strategies\QueryParameters\GetFromQueryParamTag::class, ], 'headers' => [Strategies\Headers\GetFromRouteRules::class, Strategies\Headers\GetFromHeaderTag::class], 'bodyParameters' => [ Strategies\BodyParameters\GetFromFormRequest::class, Strategies\BodyParameters\GetFromInlineValidator::class, Strategies\BodyParameters\GetFromBodyParamTag::class, ], 'responses' => [ Strategies\Responses\UseTransformerTags::class, Strategies\Responses\UseApiResourceTags::class, Strategies\Responses\UseResponseTag::class, Strategies\Responses\UseResponseFileTag::class, Strategies\Responses\ResponseCalls::class, ], 'responseFields' => [Strategies\ResponseFields\GetFromResponseFieldTag::class], ], 'fractal' => ['serializer' => null], 'routeMatcher' => \Knuckles\Scribe\Matching\RouteMatcher::class, 'database_connections_to_transact' => [], ]; ```

Additional info:

shalvah commented 2 years ago

It's not intended. Sure, you can send in a PR.