Procházet zdrojové kódy

Support No-example in inline validators

shalvah před 2 roky
rodič
revize
5fbe5fcab0

+ 2 - 2
src/Extracting/ParsesValidationRules.php

@@ -88,8 +88,8 @@ trait ParsesValidationRules
                 }
 
                 // Make sure the user-specified example overwrites others.
-                if (isset($userSpecifiedParameterInfo['example'])) {
-                    if ($this->shouldCastUserExample()) {
+                if (array_key_exists('example', $userSpecifiedParameterInfo)) {
+                    if ($userSpecifiedParameterInfo['example'] != null && $this->shouldCastUserExample()) {
                         // Examples in comments are strings, we need to cast them properly
                         $parameterData['example'] = $this->castToType($userSpecifiedParameterInfo['example'], $parameterData['type'] ?? 'string');
                     } else {

+ 9 - 9
src/Extracting/Strategies/GetFromInlineValidatorBase.php

@@ -97,23 +97,23 @@ class GetFromInlineValidatorBase extends Strategy
                 continue;
             }
 
-            $description = $example = null;
+            $dataFromComment = [];
             $comments = join("\n", array_map(
                     fn($comment) => ltrim(ltrim($comment->getReformattedText(), "/")),
                     $item->getComments()
-                )
-            );
+                ));
 
             if ($comments) {
-                $description = trim(str_replace(['No-example.', 'No-example'], '', $comments));
-                $example = null;
-                if (preg_match('/(.*\s+|^)Example:\s*([\s\S]+)\s*/s', $description, $matches)) {
-                    $description = trim($matches[1]);
-                    $example = $matches[2];
+                if (str_contains($comments, 'No-example')) $dataFromComment['example'] = null;
+
+                $dataFromComment['description'] = trim(str_replace(['No-example.', 'No-example'], '', $comments));
+                if (preg_match('/(.*\s+|^)Example:\s*([\s\S]+)\s*/s', $dataFromComment['description'], $matches)) {
+                    $dataFromComment['description'] = trim($matches[1]);
+                    $dataFromComment['example'] = $matches[2];
                 }
             }
 
-            $customParameterData[$paramName] = compact('description', 'example');
+            $customParameterData[$paramName] = $dataFromComment;
         }
 
         return [$rules, $customParameterData];

+ 6 - 7
tests/Fixtures/TestController.php

@@ -427,7 +427,7 @@ class TestController extends Controller
             'room_id' => ['string', 'in:3,5,6'],
             // Whether to ban the user forever. Example: false
             'forever' => 'boolean',
-            // Just need something here
+            // Just need something here. No-example
             'another_one' => 'numeric',
             'even_more_param' => 'array',
             'book.name' => 'string',
@@ -452,7 +452,7 @@ class TestController extends Controller
             'room_id' => ['string', 'in:3,5,6'],
             // Whether to ban the user forever. Example: false
             'forever' => 'boolean',
-            // Just need something here
+            // Just need something here. No-example
             'another_one' => 'numeric',
             'even_more_param' => 'array',
             'book.name' => 'string',
@@ -478,7 +478,7 @@ class TestController extends Controller
             'room_id' => ['string', 'in:3,5,6'],
             // Whether to ban the user forever. Example: false
             'forever' => 'boolean',
-            // Just need something here
+            // Just need something here. No-example
             'another_one' => 'numeric',
             'even_more_param' => 'array',
             'book.name' => 'string',
@@ -504,7 +504,7 @@ class TestController extends Controller
             'room_id' => ['string', 'in:3,5,6'],
             // Whether to ban the user forever. Example: false
             'forever' => 'boolean',
-            // Just need something here
+            // Just need something here. No-example
             'another_one' => 'numeric',
             'even_more_param' => 'array',
             'book.name' => 'string',
@@ -532,7 +532,7 @@ class TestController extends Controller
             'room_id' => ['string', 'in:3,5,6'],
             // Whether to ban the user forever. Example: false
             'forever' => 'boolean',
-            // Just need something here
+            // Just need something here. No-example
             'another_one' => 'numeric',
             'even_more_param' => 'array',
             'book.name' => 'string',
@@ -548,7 +548,6 @@ class TestController extends Controller
         // Do stuff
     }
 
-
     public function withInlineThisValidate(Request $request)
     {
         $this->validate($request, [
@@ -558,7 +557,7 @@ class TestController extends Controller
             'room_id' => ['string', 'in:3,5,6'],
             // Whether to ban the user forever. Example: false
             'forever' => 'boolean',
-            // Just need something here
+            // Just need something here. No-example
             'another_one' => 'numeric',
             'even_more_param' => 'array',
             'book.name' => 'string',

+ 1 - 0
tests/Strategies/GetFromInlineValidatorTest.php

@@ -38,6 +38,7 @@ class GetFromInlineValidatorTest extends BaseLaravelTest
             'type' => 'number',
             'required' => false,
             'description' => 'Just need something here.',
+            'example' => null,
         ],
         'even_more_param' => [
             'type' => 'object',