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 documentation generation when using binded interfaces instead of model classes as typed arguments #494

Merged
merged 3 commits into from
Jul 9, 2022
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
26 changes: 19 additions & 7 deletions camel/Extraction/ExtractedEndpointData.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,21 @@ public function forSerialisation()
return $copy;
}

public static function getFieldBindingForUrlParam(Route $route, string $paramName, array $typeHintedArguments = [],
protected static function instantiateTypedArgument(\ReflectionNamedType $argumentType): ?object
{
$argumentInstance = null;
$argumentClassName = $argumentType->getName();

if (class_exists($argumentClassName)) {
$argumentInstance = new $argumentClassName;
} else if (interface_exists($argumentClassName)) {
$argumentInstance = app($argumentClassName);
}

return $argumentInstance;
}

public static function getFieldBindingForUrlParam(Route $route, string $paramName, array $typeHintedArguments = [],
string $default = null): ?string
{
$binding = null;
Expand All @@ -222,9 +236,8 @@ public static function getFieldBindingForUrlParam(Route $route, string $paramNam
// Search for a type-hinted variable whose name matches the route segment name
if (is_null($binding) && array_key_exists($paramName, $typeHintedArguments)) {
$argumentType = $typeHintedArguments[$paramName]->getType();
$argumentClassName = $argumentType->getName();
$argumentInstance = new $argumentClassName;
$binding = $argumentInstance->getRouteKeyName();
$argumentInstance = self::instantiateTypedArgument($argumentType);
$binding = $argumentInstance instanceof Model ? $argumentInstance->getRouteKeyName() : null;
}

return $binding ?: $default;
Expand Down Expand Up @@ -254,12 +267,11 @@ protected function getTypeHintedArguments(): array
protected function argumentHasModelType(\ReflectionParameter $argument): bool
{
$argumentType = $argument->getType();
if (!$argumentType) {
if (!($argumentType instanceof \ReflectionNamedType)) {
// The argument does not have a type-hint
return false;
} else {
$argumentClassName = $argumentType->getName();
$argumentInstance = new $argumentClassName;
$argumentInstance = self::instantiateTypedArgument($argumentType);
return ($argumentInstance instanceof Model);
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/Fixtures/TestPostBindedInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Knuckles\Scribe\Tests\Fixtures;

interface TestPostBindedInterface
{
}
10 changes: 10 additions & 0 deletions tests/Fixtures/TestPostBindedInterfaceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Knuckles\Scribe\Tests\Fixtures;

class TestPostBindedInterfaceController
{
public function update(TestPostBindedInterface $post)
{
}
}
20 changes: 20 additions & 0 deletions tests/GenerateDocumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Knuckles\Scribe\Tests\Fixtures\TestIgnoreThisController;
use Knuckles\Scribe\Tests\Fixtures\TestPartialResourceController;
use Knuckles\Scribe\Tests\Fixtures\TestPost;
use Knuckles\Scribe\Tests\Fixtures\TestPostBindedInterface;
use Knuckles\Scribe\Tests\Fixtures\TestPostController;
use Knuckles\Scribe\Tests\Fixtures\TestPostBindedInterfaceController;
use Knuckles\Scribe\Tests\Fixtures\TestPostUserController;
use Knuckles\Scribe\Tests\Fixtures\TestResourceController;
use Knuckles\Scribe\Tests\Fixtures\TestUser;
Expand Down Expand Up @@ -433,6 +435,24 @@ public function generates_correct_url_params_from_resource_routes_and_model_bind
$this->assertEquals('posts/{post_slug}/users/{id}', $group['endpoints'][1]['uri']);
}

/** @test */
public function generates_correct_url_params_from_resource_routes_and_model_binding_with_binded_interfaces()
{
$this->app->bind(TestPostBindedInterface::class, function(){
return new TestPost();
});

RouteFacade::resource('posts', TestPostBindedInterfaceController::class)->only('update');

config(['scribe.routes.0.match.prefixes' => ['*']]);
config(['scribe.routes.0.apply.response_calls.methods' => []]);

$this->artisan('scribe:generate');

$group = Yaml::parseFile('.scribe/endpoints/00.yaml');
$this->assertEquals('posts/{slug}', $group['endpoints'][0]['uri']);
}

/** @test */
public function generates_correct_url_params_from_non_resource_routes_and_model_binding()
{
Expand Down