فهرست منبع

allow bodyParams to be noted in a FormRequest subclass

Michael Wallner 6 سال پیش
والد
کامیت
84e425d964
4فایلهای تغییر یافته به همراه96 افزوده شده و 1 حذف شده
  1. 27 1
      src/Tools/Generator.php
  2. 5 0
      tests/Fixtures/TestController.php
  3. 22 0
      tests/Fixtures/TestRequest.php
  4. 42 0
      tests/Unit/GeneratorTestCase.php

+ 27 - 1
src/Tools/Generator.php

@@ -49,7 +49,7 @@ class Generator
 
         $routeGroup = $this->getRouteGroup($controller, $method);
         $docBlock = $this->parseDocBlock($method);
-        $bodyParameters = $this->getBodyParametersFromDocBlock($docBlock['tags']);
+        $bodyParameters = $this->getBodyParameters($method, $docBlock['tags']);
         $queryParameters = $this->getQueryParametersFromDocBlock($docBlock['tags']);
         $content = ResponseResolver::getResponse($route, $docBlock['tags'], [
             'rules' => $rulesToApply,
@@ -76,6 +76,32 @@ class Generator
         return $parsedRoute;
     }
 
+    protected function getBodyParameters(ReflectionMethod $method, array $tags)
+    {
+        /** @var ReflectionClass $cls */
+        $cls = collect($method->getParameters())
+            ->reduce(function ($carry, $param) use ($method) {
+                if (!$param->getType()) {
+                    return $carry;
+                }
+
+                $cls = new ReflectionClass($param->getType()->getName());
+
+                if ($cls->isSubclassOf(\Illuminate\Foundation\Http\FormRequest::class)) {
+                    return $cls;
+                }
+
+                return $carry;
+            }, null);
+
+        if ($cls) {
+            $docBlock = new DocBlock($cls->getDocComment());
+            return $this->getBodyParametersFromDocBlock($docBlock->getTags());
+        }
+
+        return $this->getBodyParametersFromDocBlock($tags);
+    }
+
     /**
      * @param array $tags
      *

+ 5 - 0
tests/Fixtures/TestController.php

@@ -52,6 +52,11 @@ class TestController extends Controller
         return '';
     }
 
+    public function withFormRequestParameter(TestRequest $request)
+    {
+        return '';
+    }
+
     /**
      * @queryParam location_id required The id of the location.
      * @queryParam user_id required The id of the user. Example: me

+ 22 - 0
tests/Fixtures/TestRequest.php

@@ -0,0 +1,22 @@
+<?php namespace Mpociot\ApiDoc\Tests\Fixtures;
+
+use Dingo\Api\Http\FormRequest;
+
+/**
+ * @bodyParam user_id int required The id of the user. Example: 9
+ * @bodyParam room_id string The id of the room.
+ * @bodyParam forever boolean Whether to ban the user forever. Example: false
+ * @bodyParam another_one number Just need something here.
+ * @bodyParam yet_another_param object required
+ * @bodyParam even_more_param array
+ * @bodyParam book.name string
+ * @bodyParam book.author_id integer
+ * @bodyParam book[pages_count] integer
+ * @bodyParam ids.* integer
+ * @bodyParam users.*.first_name string The first name of the user. Example: John
+ * @bodyParam users.*.last_name string The last name of the user. Example: Doe
+ */
+class TestRequest extends FormRequest
+{
+
+}

+ 42 - 0
tests/Unit/GeneratorTestCase.php

@@ -83,6 +83,48 @@ abstract class GeneratorTestCase extends TestCase
         ], $bodyParameters);
     }
 
+    /** @test */
+    public function can_parse_form_request_body_parameters()
+    {
+        $route = $this->createRoute('GET', '/api/test', 'withFormRequestParameter');
+        $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
+
+        $this->assertArraySubset([
+            'user_id' => [
+                'type' => 'integer',
+                'required' => true,
+                'description' => 'The id of the user.',
+                'value' => 9,
+            ],
+            'room_id' => [
+                'type' => 'string',
+                'required' => false,
+                'description' => 'The id of the room.',
+            ],
+            'forever' => [
+                'type' => 'boolean',
+                'required' => false,
+                'description' => 'Whether to ban the user forever.',
+                'value' => false,
+            ],
+            'another_one' => [
+                'type' => 'number',
+                'required' => false,
+                'description' => 'Just need something here.',
+            ],
+            'yet_another_param' => [
+                'type' => 'object',
+                'required' => true,
+                'description' => '',
+            ],
+            'even_more_param' => [
+                'type' => 'array',
+                'required' => false,
+                'description' => '',
+            ],
+        ], $bodyParameters);
+    }
+
     /** @test */
     public function can_parse_query_parameters()
     {