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

Improved default documentation for explicitly bound route params #640

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/Support/OperationExtensions/RequestEssentialsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ private function getRoutePathParameters(RouteInfo $routeInfo)
* 2. PhpDoc Typehint
* 3. String (?)
*/
$params = array_map(function (string $paramName) use ($routeInfo, $route, $aliases, $reflectionParamsByKeys, $phpDocTypehintParam) {
$params = array_map(function (string $paramName) use ($routeInfo, $route, $aliases, $reflectionParamsByKeys, $phpDocTypehintParam, $paramsValuesClasses) {
$originalParamName = $paramName;
$paramName = $aliases[$paramName];

$description = $phpDocTypehintParam[$paramName]?->description ?? '';
Expand All @@ -255,6 +256,7 @@ private function getRoutePathParameters(RouteInfo $routeInfo)
$route,
$phpDocTypehintParam[$paramName] ?? null,
$reflectionParamsByKeys[$paramName] ?? null,
$paramsValuesClasses[$originalParamName] ?? null,
);

$param = Parameter::make($paramName, 'path')
Expand Down Expand Up @@ -300,9 +302,16 @@ private function getExplicitlyBoundParamType(string $name): ?string
return null;
}

private function getParameterType(string $paramName, string $description, RouteInfo $routeInfo, Route $route, ?ParamTagValueNode $phpDocParam, ?ReflectionParameter $reflectionParam)
{
$type = new UnknownType;
private function getParameterType(
string $paramName,
string $description,
RouteInfo $routeInfo,
Route $route,
?ParamTagValueNode $phpDocParam,
?ReflectionParameter $reflectionParam,
?string $boundClass,
) {
$type = $boundClass ? new ObjectType($boundClass) : new UnknownType;
if ($routeInfo->reflectionMethod()) {
$type->setAttribute('file', $routeInfo->reflectionMethod()->getFileName());
$type->setAttribute('line', $routeInfo->reflectionMethod()->getStartLine());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,37 @@ class Foo_RequestRulesTest_Controller
public function foo(ModelWithRulesMethod $model) {}
}

it('uses explicit binding to infer more info about path params using Route::model', function () {
RouteFacade::model('model_bound', RequestEssentialsExtensionTest_SimpleModel::class);

$openApiDocument = generateForRoute(function () {
return RouteFacade::get('api/test/{model_bound}', [Foo_RequestExplicitlyBoundTest_Controller::class, 'foo']);
});

expect($openApiDocument['paths']['/test/{model_bound}']['get']['parameters'][0])
->toHaveKey('schema.type', 'integer')
->toHaveKey('description', 'The model bound ID');
});
it('uses explicit binding to infer more info about path params using Route::bind with typehint', function () {
RouteFacade::bind('model_bound', fn ($value): RequestEssentialsExtensionTest_SimpleModel => RequestEssentialsExtensionTest_SimpleModel::findOrFail($value));

$openApiDocument = generateForRoute(function () {
return RouteFacade::get('api/test/{model_bound}', [Foo_RequestExplicitlyBoundTest_Controller::class, 'foo']);
});

expect($openApiDocument['paths']['/test/{model_bound}']['get']['parameters'][0])
->toHaveKey('schema.type', 'integer')
->toHaveKey('description', 'The model bound ID');
});
class RequestEssentialsExtensionTest_SimpleModel extends \Illuminate\Database\Eloquent\Model
{
protected $table = 'users';
}
class Foo_RequestExplicitlyBoundTest_Controller
{
public function foo() {}
}

it('handles custom key from route to determine model route key type', function () {
$openApiDocument = generateForRoute(function () {
return RouteFacade::get('api/test/{user:name}', [CustomKey_RequestEssentialsExtensionTest_Controller::class, 'foo']);
Expand Down
Loading