knuckleswtf / scribe

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

Validation rule 'required_if' breaks docs generation #111

Closed VladimirBerezkin closed 3 years ago

VladimirBerezkin commented 3 years ago

What happened? Controller method:

/**
     * Create Person
     *
     * @apiResource App\Http\Resources\Person\PersonWithDetails
     * @apiResourceModel App\Models\Person
     */
    public function store(CreateRequest $request)
    {
        $person = $this->service->create($request->validated());

        return new Resources\Person\PersonWithDetails($person);
    }

Inside of the form request:

public function rules()
    {
        return [
            'homeLocationKey' =>[
                'required_if:isCaregiver,false',
            ],
        ];
    }

On documentation generation it gives the following error for php example language:

  Facade\Ignition\Exceptions\ViewException 

  Object of class stdClass could not be converted to string (View: /app/vendor/knuckleswtf/scribe/resources/views/partials/example-requests/php.blade.php)

  at vendor/knuckleswtf/scribe/src/../resources/views//partials/example-requests/php.blade.php:20
     16▕ @foreach($route['cleanBodyParameters'] as $parameter => $value)
     17▕ @foreach(\Knuckles\Scribe\Tools\WritingUtils::getParameterNamesAndValuesForFormData($parameter,$value) as $key => $actualValue)
     18▕             [
     19▕                 'name' => '{!! $key !!}',
  ➜  20▕                 'contents' => '{!! $actualValue !!}'
     21▕             ],
     22▕ @endforeach
     23▕ @endforeach
     24▕ @foreach($route['fileParameters'] as $parameter => $file)

For bash example:

 Facade\Ignition\Exceptions\ViewException 

  Object of class stdClass could not be converted to string (View: /app/vendor/knuckleswtf/scribe/resources/views/partials/example-requests/bash.blade.php)

  at vendor/knuckleswtf/scribe/src/../resources/views//partials/example-requests/bash.blade.php:12
      8▕ @endif
      9▕ @if(count($route['fileParameters']))
     10▕ @foreach($route['cleanBodyParameters'] as $parameter => $value)
     11▕ @foreach( \Knuckles\Scribe\Tools\WritingUtils::getParameterNamesAndValuesForFormData($parameter,$value) as $key => $actualValue)
  ➜  12▕     -F "{!! "$key=".$actualValue !!}" \
     13▕ @endforeach
     14▕ @endforeach
     15▕ @foreach($route['fileParameters'] as $parameter => $file)
     16▕     -F "{!! "$parameter=@".$file->path() !!}" @if(! ($loop->last))\@endif

For JavaScript:

 Facade\Ignition\Exceptions\ViewException 

  Object of class stdClass could not be converted to string (View: /app/vendor/knuckleswtf/scribe/resources/views/partials/example-requests/javascript.blade.php)

  at vendor/knuckleswtf/scribe/src/../resources/views//partials/example-requests/javascript.blade.php:27
     23▕ @if(count($route['fileParameters']))
     24▕ const body = new FormData();
     25▕ @foreach($route['cleanBodyParameters'] as $parameter => $value)
     26▕ @foreach( \Knuckles\Scribe\Tools\WritingUtils::getParameterNamesAndValuesForFormData($parameter,$value) as $key => $actualValue)
  ➜  27▕ body.append('{!! $key !!}', '{!! $actualValue !!}');
     28▕ @endforeach
     29▕ @endforeach
     30▕ @foreach($route['fileParameters'] as $parameter => $file)
     31▕ body.append('{!! $parameter !!}', document.querySelector('input[name="{!! $parameter !!}"]').files[0]);

Solution: In form request rules, changing this:

'required_if:isCaregiver,false',

to this:

Rule::requiredIf($this->input('isCaregiver') == false),

helps to avoid this error.

My environment:

My Scribe config (minus the comments): default

shalvah commented 3 years ago

Hmmm. Can you add a dd($key, $actualValue) in there, so we can see the object(s)?

VladimirBerezkin commented 3 years ago

@shalvah sure! Here's what I get: "name" "quia"

Create Person

Example request:



$client = new \GuzzleHttp\Client();
$response = $client->post(
    'http://localhost/api/people',
    [
        'headers' => [
            'Content-Type' => 'multipart/form-data',
            'Accept' => 'application/json',
        ],
        'multipart' => [
shalvah commented 3 years ago

Sorry, that should be a dump().

VladimirBerezkin commented 3 years ago

@shalvah here's what I get with dump():

� info Writing source Markdown files to: resources/docs
"name"
"quia"
"isCaregiver"
false
"secondName"
"labore"
"middleInitial"
"accusantium"
"dateOfBirth"
"2020-10-19"
"gender"
"m"
"homeLocationKey"
{#2471}

   Facade\Ignition\Exceptions\ViewException 

  Object of class stdClass could not be converted to string (View: /app/vendor/knuckleswtf/scribe/resources/views/partials/example-requests/php.blade.php)
matyhaty commented 3 years ago

We are also a little stumped, and require this feature. We can help test if required

bethanyamber commented 3 years ago

I would also like to see 'required_if's added to the list of supported validation rules when using the FormRequests if possible please!

shalvah commented 3 years ago

I would also like to see 'required_if's added to the list of supported validation rules when using the FormRequests if possible please!

I gave an explanation for this in https://github.com/knuckleswtf/scribe/issues/140#issuecomment-724163926

But I'll look into the current buggy behaviour.