Browse Source

Merge branch 'master' into v4

# Conflicts:
#	CHANGELOG.md
#	src/Tools/Globals.php
shalvah 2 years ago
parent
commit
d557ba7bba

+ 16 - 11
CHANGELOG.md

@@ -20,45 +20,50 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 - Nested response fields are now collapsed ([00b09bb](https://github.com/knuckleswtf/scribe/commit/00b09bbea8ec64006db864bf807004d48926c6d3))
 
 
+## 3.33.2 (9 July 2022)
+### Fixed
+- Infer URL parameter name correctly when an interface is used for binding ([#494](https://github.com/knuckleswtf/scribe/pull/494))
+
+
 ## 3.33.1 (8 July 2022)
 ### Fixed
-- Don't send empty query parameter field if it's optional ([#493](https://github.com/knuckleswtf/scribe/commit/493)))
-- Infer URL parameter name correctly when `getRouteKeyName()` is set ([#492](https://github.com/knuckleswtf/scribe/commit/492)))
+- Don't send empty query parameter field if it's optional ([#493](https://github.com/knuckleswtf/scribe/commit/493))
+- Infer URL parameter name correctly when `getRouteKeyName()` is set ([#492](https://github.com/knuckleswtf/scribe/pull/492))
 
 
 ## 3.33.0 (27 June 2022)
 ### Added
-- Include description in Postman collection for formdata body parameters ([10faa500](https://github.com/knuckleswtf/scribe/commit/10faa500e36e02d4efcecf8ad5e1d91ba1c7728d)))
-- Support for attributes in `@apiResource` ([8b8bc6b0](https://github.com/knuckleswtf/scribe/commit/8b8bc6b04242ea5d35916db84e2a7cbe73e0cef5)))
+- Include description in Postman collection for formdata body parameters ([10faa500](https://github.com/knuckleswtf/scribe/commit/10faa500e36e02d4efcecf8ad5e1d91ba1c7728d))
+- Support for attributes in `@apiResource` ([8b8bc6b0](https://github.com/knuckleswtf/scribe/commit/8b8bc6b04242ea5d35916db84e2a7cbe73e0cef5))
 
 
 ## 3.32.0 (23 June 2022)
 ### Modified
-- Improved code blocks hiding ([#486](https://github.com/knuckleswtf/scribe/pull/486)))
+- Improved code blocks hiding ([#486](https://github.com/knuckleswtf/scribe/pull/486))
 
 
 ## 3.31.0 (16 June 2022)
 ### Modified
-- Postman collection: replace multipart PUT/PATCH requests with POST & `_method` ([#480](https://github.com/knuckleswtf/scribe/pull/480)))
+- Postman collection: replace multipart PUT/PATCH requests with POST & `_method` ([#480](https://github.com/knuckleswtf/scribe/pull/480))
 
 ### Fixed
-- Fix logo image partially covered by sidebar ([#481](https://github.com/knuckleswtf/scribe/pull/481)))
+- Fix logo image partially covered by sidebar ([#481](https://github.com/knuckleswtf/scribe/pull/481))
 
 
 ## 3.30.0 (11 June 2022)
 ### Added
-- Support for more inline validator forms (`$request->validate(...)` without assignment, and `$this->validate($request, ...)`) ([29940c2e](https://github.com/knuckleswtf/scribe/commit/29940c2e05c8035a1ab85d9482c2335e1747ab41)))
+- Support for more inline validator forms (`$request->validate(...)` without assignment, and `$this->validate($request, ...)`) ([29940c2e](https://github.com/knuckleswtf/scribe/commit/29940c2e05c8035a1ab85d9482c2335e1747ab41))
 
 ### Fixed
-- Fix incorrect public_path check on Lumen ([64ad2f6e](https://github.com/knuckleswtf/scribe/commit/64ad2f6e059bea03e9ba5b209818e916758ca36a)))
+- Fix incorrect public_path check on Lumen ([64ad2f6e](https://github.com/knuckleswtf/scribe/commit/64ad2f6e059bea03e9ba5b209818e916758ca36a))
 
 ## 3.29.1 (22 May 2022)
 ### Fixed
-- Make output path for `laravel` type configurable ([48b2b90](https://github.com/knuckleswtf/scribe/commit/48b2b90580f92dbe2fa5ebdad207fa082c875532)))
+- Make output path for `laravel` type configurable ([48b2b90](https://github.com/knuckleswtf/scribe/commit/48b2b90580f92dbe2fa5ebdad207fa082c875532))
 
 ## 3.29.0 (22 May 2022)
 ### Added
-- 🎉🎉 Support multiple docs with the `--config` flag ([#472](https://github.com/knuckleswtf/scribe/pull/472), [cc6c95e](https://github.com/knuckleswtf/scribe/commit/cc6c95eed2a999a640666eab8b7dad1b417c9aca)))
+- 🎉🎉 Support multiple docs with the `--config` flag ([#472](https://github.com/knuckleswtf/scribe/pull/472), [cc6c95e](https://github.com/knuckleswtf/scribe/commit/cc6c95eed2a999a640666eab8b7dad1b417c9aca))
 
 ## 3.28.0 (14 May 2022)
 ### Added

+ 25 - 11
camel/Extraction/ExtractedEndpointData.php

@@ -211,8 +211,24 @@ class ExtractedEndpointData extends BaseDTO
         return $copy;
     }
 
-    public static function getFieldBindingForUrlParam(Route $route, string $paramName, array $typeHintedArguments = [],
-                                                      string $default = null): ?string
+    protected static function instantiateTypedArgument(\ReflectionNamedType $argumentType): ?object
+    {
+        $argumentClassName = $argumentType->getName();
+
+        if (class_exists($argumentClassName)) {
+            return new $argumentClassName;
+        }
+
+        if (interface_exists($argumentClassName)) {
+            return app($argumentClassName);
+        }
+
+        return null;
+    }
+
+    public static function getFieldBindingForUrlParam(
+        Route $route, string $paramName, array $typeHintedArguments = [], string $default = null
+    ): ?string
     {
         $binding = null;
         // Was added in Laravel 7.x
@@ -223,9 +239,8 @@ class ExtractedEndpointData extends BaseDTO
         // 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;
@@ -255,13 +270,12 @@ class ExtractedEndpointData extends BaseDTO
     protected function argumentHasModelType(\ReflectionParameter $argument): bool
     {
         $argumentType = $argument->getType();
-        if (!$argumentType) {
-            // The argument does not have a type-hint
+        if (!($argumentType instanceof \ReflectionNamedType)) {
+            // The argument does not have a type-hint, or is a primitive type (`string`, ..)
             return false;
-        } else {
-            $argumentClassName = $argumentType->getName();
-            $argumentInstance = new $argumentClassName;
-            return ($argumentInstance instanceof Model);
         }
+
+        $argumentInstance = self::instantiateTypedArgument($argumentType);
+        return ($argumentInstance instanceof Model);
     }
 }

+ 7 - 0
tests/Fixtures/TestPostBindedInterface.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Knuckles\Scribe\Tests\Fixtures;
+
+interface TestPostBindedInterface
+{
+}

+ 10 - 0
tests/Fixtures/TestPostBindedInterfaceController.php

@@ -0,0 +1,10 @@
+<?php
+
+namespace Knuckles\Scribe\Tests\Fixtures;
+
+class TestPostBindedInterfaceController
+{
+    public function update(TestPostBindedInterface $post)
+    {
+    }
+}

+ 20 - 0
tests/GenerateDocumentation/OutputTest.php

@@ -10,7 +10,9 @@ use Knuckles\Scribe\Tests\Fixtures\TestController;
 use Knuckles\Scribe\Tests\Fixtures\TestGroupController;
 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\TestUser;
 use Knuckles\Scribe\Tests\TestHelpers;
@@ -327,6 +329,24 @@ class OutputTest extends BaseLaravelTest
         $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()
     {