Skip to content

Commit 4977c61

Browse files
committed
Fix error when using 'exists' validation without second argument
1 parent b070c8d commit 4977c61

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

src/Mpociot/ApiDoc/Generators/AbstractGenerator.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ protected function getParameters($routeData, $routeAction, $bindings)
4646
'value' => '',
4747
'description' => [],
4848
];
49-
foreach ($rules as $rule) {
50-
$this->parseRule($rule, $attributeData, $routeData['id']);
49+
foreach ($rules as $ruleName => $rule) {
50+
$this->parseRule($rule, $attribute, $attributeData, $routeData['id']);
5151
}
5252
$routeData['parameters'][$attribute] = $attributeData;
5353
}
@@ -181,11 +181,13 @@ protected function fancyImplode($arr, $first, $last)
181181

182182
/**
183183
* @param string $rule
184+
* @param string $ruleName
184185
* @param array $attributeData
186+
* @param int $seed
185187
*
186188
* @return void
187189
*/
188-
protected function parseRule($rule, &$attributeData, $seed)
190+
protected function parseRule($rule, $ruleName, &$attributeData, $seed)
189191
{
190192
$faker = Factory::create();
191193
$faker->seed(crc32($seed));
@@ -300,7 +302,8 @@ protected function parseRule($rule, &$attributeData, $seed)
300302
$attributeData['value'] = $faker->timezone;
301303
break;
302304
case 'exists':
303-
$attributeData['description'][] = Description::parse($rule)->with([Str::singular($parameters[0]), $parameters[1]])->getDescription();
305+
$fieldName = isset($parameters[1]) ? $parameters[1] : $ruleName;
306+
$attributeData['description'][] = Description::parse($rule)->with([Str::singular($parameters[0]), $fieldName])->getDescription();
304307
break;
305308
case 'active_url':
306309
$attributeData['type'] = 'url';

tests/ApiDocGeneratorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ public function testCanParseFormRequestRules()
186186
$this->assertCount(1, $attribute['description']);
187187
$this->assertSame('Valid user firstname', $attribute['description'][0]);
188188
break;
189+
case 'single_exists':
190+
$this->assertFalse($attribute['required']);
191+
$this->assertSame('string', $attribute['type']);
192+
$this->assertCount(1, $attribute['description']);
193+
$this->assertSame('Valid user single_exists', $attribute['description'][0]);
194+
break;
189195
case 'image':
190196
$this->assertFalse($attribute['required']);
191197
$this->assertSame('image', $attribute['type']);

tests/DingoGeneratorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ public function testCanParseFormRequestRules()
189189
$this->assertCount(1, $attribute['description']);
190190
$this->assertSame('Valid user firstname', $attribute['description'][0]);
191191
break;
192+
case 'single_exists':
193+
$this->assertFalse($attribute['required']);
194+
$this->assertSame('string', $attribute['type']);
195+
$this->assertCount(1, $attribute['description']);
196+
$this->assertSame('Valid user single_exists', $attribute['description'][0]);
197+
break;
192198
case 'image':
193199
$this->assertFalse($attribute['required']);
194200
$this->assertSame('image', $attribute['type']);

tests/Fixtures/DingoTestRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function rules()
2626
'digits' => 'digits:2',
2727
'digits_between' => 'digits_between:2,10',
2828
'exists' => 'exists:users,firstname',
29+
'single_exists' => 'exists:users',
2930
'in' => 'in:jpeg,png,bmp,gif,svg',
3031
'integer' => 'integer',
3132
'ip' => 'ip',

tests/Fixtures/TestRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function rules()
2626
'digits' => 'digits:2',
2727
'digits_between' => 'digits_between:2,10',
2828
'exists' => 'exists:users,firstname',
29+
'single_exists' => 'exists:users',
2930
'in' => 'in:jpeg,png,bmp,gif,svg',
3031
'integer' => 'integer',
3132
'ip' => 'ip',

0 commit comments

Comments
 (0)