From ef0e02469324a55a2eeb5a06044f03f7b9d08c14 Mon Sep 17 00:00:00 2001 From: toyi Date: Sat, 9 Jul 2022 10:56:22 +0200 Subject: [PATCH 1/3] Fix documentation generation when using binded interfaces instead of model classes as typed arguments --- camel/Extraction/ExtractedEndpointData.php | 24 ++++++++++++++----- tests/Fixtures/TestPostBindedInterface.php | 7 ++++++ .../TestPostBindedInterfaceController.php | 10 ++++++++ tests/GenerateDocumentationTest.php | 20 ++++++++++++++++ 4 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 tests/Fixtures/TestPostBindedInterface.php create mode 100644 tests/Fixtures/TestPostBindedInterfaceController.php diff --git a/camel/Extraction/ExtractedEndpointData.php b/camel/Extraction/ExtractedEndpointData.php index cc5fce16..c367885c 100644 --- a/camel/Extraction/ExtractedEndpointData.php +++ b/camel/Extraction/ExtractedEndpointData.php @@ -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; @@ -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; @@ -258,8 +271,7 @@ protected function argumentHasModelType(\ReflectionParameter $argument): bool // 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); } } diff --git a/tests/Fixtures/TestPostBindedInterface.php b/tests/Fixtures/TestPostBindedInterface.php new file mode 100644 index 00000000..96e0ce19 --- /dev/null +++ b/tests/Fixtures/TestPostBindedInterface.php @@ -0,0 +1,7 @@ +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() { From 27f51ad9aac7737df591f3d1b9b6f38d38751110 Mon Sep 17 00:00:00 2001 From: toyi Date: Sat, 9 Jul 2022 11:04:33 +0200 Subject: [PATCH 2/3] fix lint --- camel/Extraction/ExtractedEndpointData.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/camel/Extraction/ExtractedEndpointData.php b/camel/Extraction/ExtractedEndpointData.php index c367885c..47803cbd 100644 --- a/camel/Extraction/ExtractedEndpointData.php +++ b/camel/Extraction/ExtractedEndpointData.php @@ -267,7 +267,7 @@ 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 { From 5993e2debc8e582b6bd103e77842734315c2688b Mon Sep 17 00:00:00 2001 From: Shalvah Date: Sat, 9 Jul 2022 23:31:32 +0200 Subject: [PATCH 3/3] Update camel/Extraction/ExtractedEndpointData.php --- camel/Extraction/ExtractedEndpointData.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/camel/Extraction/ExtractedEndpointData.php b/camel/Extraction/ExtractedEndpointData.php index 47803cbd..4dbea3ca 100644 --- a/camel/Extraction/ExtractedEndpointData.php +++ b/camel/Extraction/ExtractedEndpointData.php @@ -267,7 +267,7 @@ protected function getTypeHintedArguments(): array protected function argumentHasModelType(\ReflectionParameter $argument): bool { $argumentType = $argument->getType(); - if (!$argumentType instanceof \ReflectionNamedType) { + if (!($argumentType instanceof \ReflectionNamedType)) { // The argument does not have a type-hint return false; } else {