Browse Source

Fix error when using 'exists' validation without second argument

Marcel Pociot 8 years ago
parent
commit
4977c61f8e

+ 7 - 4
src/Mpociot/ApiDoc/Generators/AbstractGenerator.php

@@ -46,8 +46,8 @@ abstract class AbstractGenerator
                 'value' => '',
                 'description' => [],
             ];
-            foreach ($rules as $rule) {
-                $this->parseRule($rule, $attributeData, $routeData['id']);
+            foreach ($rules as $ruleName => $rule) {
+                $this->parseRule($rule, $attribute, $attributeData, $routeData['id']);
             }
             $routeData['parameters'][$attribute] = $attributeData;
         }
@@ -181,11 +181,13 @@ abstract class AbstractGenerator
 
     /**
      * @param  string  $rule
+     * @param  string  $ruleName
      * @param  array  $attributeData
+     * @param  int  $seed
      *
      * @return void
      */
-    protected function parseRule($rule, &$attributeData, $seed)
+    protected function parseRule($rule, $ruleName, &$attributeData, $seed)
     {
         $faker = Factory::create();
         $faker->seed(crc32($seed));
@@ -300,7 +302,8 @@ abstract class AbstractGenerator
                 $attributeData['value'] = $faker->timezone;
                 break;
             case 'exists':
-                $attributeData['description'][] = Description::parse($rule)->with([Str::singular($parameters[0]), $parameters[1]])->getDescription();
+                $fieldName = isset($parameters[1]) ? $parameters[1] : $ruleName;
+                $attributeData['description'][] = Description::parse($rule)->with([Str::singular($parameters[0]), $fieldName])->getDescription();
                 break;
             case 'active_url':
                 $attributeData['type'] = 'url';

+ 6 - 0
tests/ApiDocGeneratorTest.php

@@ -186,6 +186,12 @@ class ApiDocGeneratorTest extends TestCase
                     $this->assertCount(1, $attribute['description']);
                     $this->assertSame('Valid user firstname', $attribute['description'][0]);
                     break;
+                case 'single_exists':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Valid user single_exists', $attribute['description'][0]);
+                    break;
                 case 'image':
                     $this->assertFalse($attribute['required']);
                     $this->assertSame('image', $attribute['type']);

+ 6 - 0
tests/DingoGeneratorTest.php

@@ -189,6 +189,12 @@ class DingoGeneratorTest extends TestCase
                     $this->assertCount(1, $attribute['description']);
                     $this->assertSame('Valid user firstname', $attribute['description'][0]);
                     break;
+                case 'single_exists':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Valid user single_exists', $attribute['description'][0]);
+                    break;
                 case 'image':
                     $this->assertFalse($attribute['required']);
                     $this->assertSame('image', $attribute['type']);

+ 1 - 0
tests/Fixtures/DingoTestRequest.php

@@ -26,6 +26,7 @@ class DingoTestRequest extends FormRequest
             'digits' => 'digits:2',
             'digits_between' => 'digits_between:2,10',
             'exists' => 'exists:users,firstname',
+            'single_exists' => 'exists:users',
             'in' => 'in:jpeg,png,bmp,gif,svg',
             'integer' => 'integer',
             'ip' => 'ip',

+ 1 - 0
tests/Fixtures/TestRequest.php

@@ -26,6 +26,7 @@ class TestRequest extends FormRequest
             'digits' => 'digits:2',
             'digits_between' => 'digits_between:2,10',
             'exists' => 'exists:users,firstname',
+            'single_exists' => 'exists:users',
             'in' => 'in:jpeg,png,bmp,gif,svg',
             'integer' => 'integer',
             'ip' => 'ip',