Skip to content

Commit 4b07bd8

Browse files
authored
Merge pull request #14 from weretyczx/bug-fix
fix Str::startsWith need string error
2 parents 56813cb + a204e3a commit 4b07bd8

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

src/Parameters/Concerns/GeneratesFromRules.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ protected function getEnumValues(array $paramRules)
6262
private function getInParameter(array $paramRules)
6363
{
6464
foreach ($paramRules as $rule) {
65-
if (Str::startsWith($rule, 'in:')) {
65+
if ((is_string($rule) || method_exists($rule , '__toString')) && Str::startsWith($rule, 'in:')) {
6666
return $rule;
6767
}
6868
}
6969

7070
return false;
7171
}
72-
}
72+
}

tests/Parameters/BodyParameterGeneratorTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Mtrajano\LaravelSwagger\Tests\Parameters;
44

5+
use Illuminate\Validation\Rule;
56
use Mtrajano\LaravelSwagger\Tests\TestCase;
67
use Mtrajano\LaravelSwagger\Parameters\BodyParameterGenerator;
8+
use Mtrajano\LaravelSwagger\Tests\Stubs\Rules\Uppercase as UppercaseRule;
79

810
class BodyParameterGeneratorTest extends TestCase
911
{
@@ -153,6 +155,63 @@ public function testSingleObjectSyntax()
153155
], $bodyParameters['schema']['properties']);
154156
}
155157

158+
public function testResolvesRuleEnum()
159+
{
160+
161+
$bodyParameters = $this->getBodyParameters([
162+
'type' => [
163+
Rule::in(1,2,3),
164+
'integer',
165+
]
166+
]);
167+
168+
$this->assertEquals([
169+
'type' => [
170+
'type' => 'integer',
171+
'enum' => ['"1"','"2"','"3"'], //using Rule::in parameters are cast to string
172+
]
173+
], $bodyParameters['schema']['properties']);
174+
175+
}
176+
177+
public function testIgnoresRuleObject()
178+
{
179+
180+
$bodyParameters = $this->getBodyParameters([
181+
'name' => [
182+
'string',
183+
new UppercaseRule
184+
],
185+
]);
186+
187+
$this->assertEquals([
188+
'name' => [
189+
'type' => 'string',
190+
]
191+
], $bodyParameters['schema']['properties']);
192+
193+
}
194+
195+
public function testIgnoresClosureRules()
196+
{
197+
$bodyParameters = $this->getBodyParameters([
198+
'name' => [
199+
'string',
200+
function ($attribute, $value, $fail) {
201+
if ($value === 'foo') {
202+
$fail($attribute.' is invalid.');
203+
}
204+
},
205+
],
206+
]);
207+
208+
$this->assertEquals([
209+
'name' => [
210+
'type' => 'string',
211+
]
212+
], $bodyParameters['schema']['properties']);
213+
}
214+
156215
private function getBodyParameters(array $rules)
157216
{
158217
$bodyParameters = (new BodyParameterGenerator($rules))->getParameters();

tests/Stubs/Rules/Uppercase.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger\Tests\Stubs\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
7+
class Uppercase implements Rule
8+
{
9+
/**
10+
* Determine if the validation rule passes.
11+
*
12+
* @param string $attribute
13+
* @param mixed $value
14+
* @return bool
15+
*/
16+
public function passes($attribute, $value)
17+
{
18+
return strtoupper($value) === $value;
19+
}
20+
21+
/**
22+
* Get the validation error message.
23+
*
24+
* @return string
25+
*/
26+
public function message()
27+
{
28+
return 'The :attribute must be uppercase.';
29+
}
30+
}

0 commit comments

Comments
 (0)