From b2880085ab7f3225a6a093abdb76320116d319dc Mon Sep 17 00:00:00 2001 From: Joseph Bassey Date: Fri, 21 Mar 2025 12:36:14 +0000 Subject: [PATCH] Fix issue with route scope binding --- .../Strategies/UrlParameters/GetFromLaravelAPI.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php b/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php index 682f66f8..6bbb7f23 100644 --- a/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php +++ b/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php @@ -86,7 +86,7 @@ protected function inferBetterTypesAndExamplesForEloquentUrlParameters(array $pa // If User model has `id` as an integer, then {id} param should be an integer $typeHintedEloquentModels = UrlParamsNormalizer::getTypeHintedEloquentModels($endpointData->method); foreach ($typeHintedEloquentModels as $argumentName => $modelInstance) { - $routeKey = $modelInstance->getRouteKeyName(); + $routeKey = $endpointData->route->bindingFields()[$argumentName] ?? $modelInstance->getRouteKeyName(); // Find the param name. In our normalized URL, argument $user might be param {user}, or {user_id}, or {id}, if (isset($parameters[$argumentName])) { @@ -116,9 +116,8 @@ protected function inferBetterTypesAndExamplesForEloquentUrlParameters(array $pa // Now infer. foreach ($modelInstances as $paramName => $modelInstance) { // If the routeKey is the same as the primary key in the database, use the PK's type. - $routeKey = $modelInstance->getRouteKeyName(); - $type = $modelInstance->getKeyName() === $routeKey - ? static::normalizeTypeName($modelInstance->getKeyType()) : 'string'; + $routeKey = in_array($paramName, $endpointData->route->bindingFields()) ? $paramName : $modelInstance->getRouteKeyName();; + $type = $modelInstance->getKeyName() === $routeKey ? static::normalizeTypeName($modelInstance->getKeyType()) : 'string'; $parameters[$paramName]['type'] = $type;