Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: RequestValidateFacade Variable could not be converted to string #925

Merged

Conversation

TZK-
Copy link
Contributor

@TZK- TZK- commented Dec 4, 2024

This PR fixes a bug when RequestValidateFacade is parsing controller methods in specific context.

Given this exemple :

#[Group('ExampleGroup')]
#[ResponseFromApiResource(ExampleResource::class, ExampleModel::class, collection: true, paginate: 20)]
public function handle(Request $req, EnumType $type, int $val): ResourceCollection
{
    $modelType = match ($type) {
        EnumType::OPTION_A => ModelA::class,
    };

    // Ensure presence in DB or fail to 404
    $model = $modelType::findOrFail($val);

    return ExampleResource::collection($model);
}

When generating doc with artisan RequestValidateFacade will end in the exception

Object of class PhpParser\Node\Expr\Variable could not be converted to string

  at vendor/knuckleswtf/scribe/src/Extracting/Shared/ValidationRulesFinders/RequestValidateFacade.php:28
     24▕         }
     25▕ 
     26▕         if (
     27▕             $expr instanceof Node\Expr\StaticCall
  ➜  28▕             && in_array((string) $expr->class, ['Request', \Illuminate\Support\Facades\Request::class])
     29▕         ) {
     30▕             if ($expr->name->name == "validate") {
     31▕                 return $expr->args[0]->value;
     32▕             }

      +22 vendor frames

Because line 28 is calling toString method on $expr->class but it has no toString method in PhpParser\Node\Expr\Variable object. It is $model = $modelType::findOrFail($val); which causes the problem.

Thi PR ensure we check class name onto the right objects to avoid this bug.

@shalvah
Copy link
Contributor

shalvah commented Dec 8, 2024

Thanks!

@shalvah shalvah merged commit e8267c9 into knuckleswtf:master Dec 8, 2024
6 checks passed
@TZK-
Copy link
Contributor Author

TZK- commented Dec 18, 2024

Is it possible to draft a new release for this fix ?

@TZK- TZK- deleted the fix/RequestValidateFacade_tostring branch December 18, 2024 18:13
@shalvah
Copy link
Contributor

shalvah commented Dec 31, 2024

Coming later today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants