From 5fbe5fcab009139809c8a3e316ca753b23eb18ce Mon Sep 17 00:00:00 2001 From: shalvah Date: Sat, 8 Apr 2023 14:46:56 +0200 Subject: [PATCH] Support No-example in inline validators --- src/Extracting/ParsesValidationRules.php | 4 ++-- .../Strategies/GetFromInlineValidatorBase.php | 18 +++++++++--------- tests/Fixtures/TestController.php | 13 ++++++------- .../Strategies/GetFromInlineValidatorTest.php | 1 + 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Extracting/ParsesValidationRules.php b/src/Extracting/ParsesValidationRules.php index f5dc3f9e..a3f035d5 100644 --- a/src/Extracting/ParsesValidationRules.php +++ b/src/Extracting/ParsesValidationRules.php @@ -88,8 +88,8 @@ public function getParametersFromValidationRules(array $validationRules, array $ } // 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 { diff --git a/src/Extracting/Strategies/GetFromInlineValidatorBase.php b/src/Extracting/Strategies/GetFromInlineValidatorBase.php index 9e9edb48..6e17b44c 100644 --- a/src/Extracting/Strategies/GetFromInlineValidatorBase.php +++ b/src/Extracting/Strategies/GetFromInlineValidatorBase.php @@ -97,23 +97,23 @@ enum_exists($enum) && method_exists($enum, 'tryFrom') 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]; diff --git a/tests/Fixtures/TestController.php b/tests/Fixtures/TestController.php index c19d2c0d..d6fbb232 100644 --- a/tests/Fixtures/TestController.php +++ b/tests/Fixtures/TestController.php @@ -427,7 +427,7 @@ public function withInlineRequestValidate(Request $request) '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 @@ public function withInlineRequestValidateNoAssignment(Request $request) '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 @@ public function withInlineRequestValidateQueryParams(Request $request) '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 @@ public function withInlineValidatorMake(Request $request) '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 @@ public function withInlineRequestValidateWithBag(Request $request) '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 @@ public function withInlineRequestValidateWithBag(Request $request) // Do stuff } - public function withInlineThisValidate(Request $request) { $this->validate($request, [ @@ -558,7 +557,7 @@ public function withInlineThisValidate(Request $request) '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', diff --git a/tests/Strategies/GetFromInlineValidatorTest.php b/tests/Strategies/GetFromInlineValidatorTest.php index 2b022008..8d4d9d93 100644 --- a/tests/Strategies/GetFromInlineValidatorTest.php +++ b/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',