Jelajahi Sumber

Merge pull request #54 from Smudge3806/fix/head_endpoints

[#53] Fixed issue where purposeful head requests aren't rendered
Shalvah 4 tahun lalu
induk
melakukan
cd4fd3661b

+ 9 - 1
src/Extracting/Generator.php

@@ -43,7 +43,15 @@ class Generator
      */
     public function getMethods(Route $route)
     {
-        return array_diff($route->methods(), ['HEAD']);
+        $methods = $route->methods();
+
+        // Laravel adds an automatic "HEAD" endpoint for each GET request, so we'll strip that out,
+        // but not if there's only one method (means it was intentional)
+        if (count($methods) === 1) {
+            return $methods;
+        }
+
+        return array_diff($methods, ['HEAD']);
     }
 
     /**

+ 6 - 1
src/Tools/ConsoleOutputUtils.php

@@ -61,7 +61,12 @@ class ConsoleOutputUtils
      */
     public static function getRouteRepresentation(Route $route): string
     {
-        $routeMethods = implode(',', array_diff($route->methods(), ['HEAD']));
+        $methods = $route->methods();
+        if (count($methods) > 1) {
+            $methods = array_diff($route->methods(), ['HEAD']);
+        }
+
+        $routeMethods = implode(',', $methods);
         $routePath = $route->uri();
         return "[$routeMethods] $routePath";
     }

+ 14 - 0
tests/GenerateDocumentationTest.php

@@ -70,6 +70,20 @@ class GenerateDocumentationTest extends TestCase
     }
 
     /** @test */
+    public function can_process_traditional_laravel_head_routes()
+    {
+        RouteFacade::addRoute('HEAD', '/api/test', TestController::class . '@withEndpointDescription');
+
+        config(['scribe.routes.0.match.prefixes' => ['api/*']]);
+        $output = $this->artisan('scribe:generate');
+
+        $this->assertStringContainsString('Processed route: [HEAD] api/test', $output);
+    }
+
+    /**
+     * @test
+     * @see https://github.com/knuckleswtf/scribe/issues/53
+     */
     public function can_process_closure_routes()
     {
         RouteFacade::get('/api/closure', function () {