knuckleswtf / scribe

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

I have a request that receives a JSON string that I decode and then validate. I'm getting an exception when trying to generate documentation. #761

Open RoudyTarabay opened 1 year ago

RoudyTarabay commented 1 year ago

Scribe version

4.25.0

Your question

Hi everyone,

My normal routes are working fine except two where I have a nested array of objects which I'm sending through a formdata. In order to be able to access the data before validating it I have to loop through it and decode it. As the title says I'm running into an issue following the below validation method:

private function validate(Request $request)
    {
        $request_data = $request->all();

        foreach ($request_data as $key => $value) {
            $request_data[$key] = json_decode($value, true);
        }
        $rules = [
            'data' => 'required|array',
            'data.*.id' => 'required|numeric',
            'data.*.countries' => 'required|array',
            'data.*.countries.*' => 'required|numeric',
            'data.*.leadTime' => 'required',
            'data.*.leadTime.value' => 'required|numeric',
            'data.*.leadTime.unit' => 'required|string',
            'data.*.quote' => 'required|numeric',
        ];
        $validator = Validator::make($request_data, $rules);
        if ($validator->fails()) {
            throw  ValidationException::withMessages($validator->errors()->toArray());
        }
        return $request_data;
    }

Should I handle my validation differently for it to work?

Docs

shalvah commented 1 year ago

You can't just say "I'm running into an issue"... What's the issue?

RoudyTarabay commented 1 year ago

You can't just say "I'm running into an issue"... What's the issue?

Apologies, thought it's clear due to not using validation immediately on the request object.

Here is the stacktrace i'm receiving:

✖ Failed processing route: [POST] api/seller/quotes - Exception encountered.

   ErrorException 

  Undefined array key 1

  at vendor/knuckleswtf/scribe/src/Extracting/Shared/ValidationRulesFinders/ThisValidate.php:32
     28▕             && $expr->var instanceof Node\Expr\Variable
     29▕             && $expr->var->name === "this"
     30▕         ) {
     31▕             if ($expr->name->name == "validate") {
  ➜  32▕                 return $expr->args[1]->value;
     33▕             }
     34▕         }
     35▕     }
     36▕ }

  1   vendor/knuckleswtf/scribe/src/Extracting/Shared/ValidationRulesFinders/ThisValidate.php:32
      Illuminate\Foundation\Bootstrap\HandleExceptions::Illuminate\Foundation\Bootstrap\{closure}("Undefined array key 1", "/Applications/XAMPP/xamppfiles/htdocs/marketplace-laravel/vendor/knuckleswtf/scribe/src/Extracting/Shared/ValidationRulesFinders/ThisValidate.php")

  2   vendor/knuckleswtf/scribe/src/Extracting/Strategies/GetFromInlineValidatorBase.php:183
      Knuckles\Scribe\Extracting\Shared\ValidationRulesFinders\ThisValidate::find(Object(PhpParser\Node\Stmt\Expression))

  3   vendor/knuckleswtf/scribe/src/Extracting/Strategies/GetFromInlineValidatorBase.php:36
      Knuckles\Scribe\Extracting\Strategies\GetFromInlineValidatorBase::findValidationExpression()

  4   vendor/knuckleswtf/scribe/src/Extracting/Strategies/GetFromInlineValidatorBase.php:25
      Knuckles\Scribe\Extracting\Strategies\GetFromInlineValidatorBase::lookForInlineValidationRules(Object(PhpParser\Node\Stmt\ClassMethod))

  5   vendor/knuckleswtf/scribe/src/Extracting/Extractor.php:209
      Knuckles\Scribe\Extracting\Strategies\GetFromInlineValidatorBase::__invoke(Object(Knuckles\Camel\Extraction\ExtractedEndpointData), [])

  6   vendor/knuckleswtf/scribe/src/Extracting/Extractor.php:146
      Knuckles\Scribe\Extracting\Extractor::iterateThroughStrategies("queryParameters", Object(Knuckles\Camel\Extraction\ExtractedEndpointData), [], Object(Closure))

  7   vendor/knuckleswtf/scribe/src/Extracting/Extractor.php:70
      Knuckles\Scribe\Extracting\Extractor::fetchQueryParameters(Object(Knuckles\Camel\Extraction\ExtractedEndpointData), [])

  8   vendor/knuckleswtf/scribe/src/GroupedEndpoints/GroupedEndpointsFromApp.php:124
      Knuckles\Scribe\Extracting\Extractor::processRoute(Object(Illuminate\Routing\Route), [])

  9   vendor/knuckleswtf/scribe/src/GroupedEndpoints/GroupedEndpointsFromApp.php:71
      Knuckles\Scribe\GroupedEndpoints\GroupedEndpointsFromApp::extractEndpointsInfoFromLaravelApp()

  10  vendor/knuckleswtf/scribe/src/GroupedEndpoints/GroupedEndpointsFromApp.php:49
      Knuckles\Scribe\GroupedEndpoints\GroupedEndpointsFromApp::extractEndpointsInfoAndWriteToDisk(Object(Knuckles\Scribe\Matching\RouteMatcher))

  11  vendor/knuckleswtf/scribe/src/Commands/GenerateDocumentation.php:51
      Knuckles\Scribe\GroupedEndpoints\GroupedEndpointsFromApp::get()

  12  vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
      Knuckles\Scribe\Commands\GenerateDocumentation::handle(Object(Knuckles\Scribe\Matching\RouteMatcher), Object(Knuckles\Scribe\GroupedEndpoints\GroupedEndpointsFactory))

  13  vendor/laravel/framework/src/Illuminate/Container/Util.php:41
      Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()

  14  vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
      Illuminate\Container\Util::unwrapIfClosure(Object(Closure))

  15  vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
      Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Object(Closure))

  16  vendor/laravel/framework/src/Illuminate/Container/Container.php:661
      Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), [])

  17  vendor/laravel/framework/src/Illuminate/Console/Command.php:183
      Illuminate\Container\Container::call()

  18  vendor/symfony/console/Command/Command.php:291
      Illuminate\Console\Command::execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))

  19  vendor/laravel/framework/src/Illuminate/Console/Command.php:153
      Symfony\Component\Console\Command\Command::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))

  20  vendor/symfony/console/Application.php:1014
      Illuminate\Console\Command::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

  21  vendor/symfony/console/Application.php:301
      Symfony\Component\Console\Application::doRunCommand(Object(Knuckles\Scribe\Commands\GenerateDocumentation), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

  22  vendor/symfony/console/Application.php:171
      Symfony\Component\Console\Application::doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

  23  vendor/laravel/framework/src/Illuminate/Console/Application.php:102
      Symfony\Component\Console\Application::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

  24  vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:155
      Illuminate\Console\Application::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

  25  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
shalvah commented 1 year ago

That seems odd...are you sure the issue is related to the first code snippet? Looking at the stacktrace, that error (which shouldn't happen, yes) is triggered when it encounters code of the form $this->validate(...).