-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support to read custom attribute for validation when using swagger. (…
…#6379)
- Loading branch information
1 parent
cfc1647
commit e3e7f2c
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
namespace Hyperf\Swagger\Request; | ||
|
||
use Hyperf\Di\Annotation\AnnotationCollector; | ||
use Hyperf\Swagger\Annotation\JsonContent; | ||
use Hyperf\Swagger\Annotation\Property; | ||
use Hyperf\Swagger\Annotation\QueryParameter; | ||
use Hyperf\Swagger\Annotation\RequestBody; | ||
use Hyperf\Swagger\Util; | ||
|
||
class AttributeCollector | ||
{ | ||
protected static $attributes = []; | ||
|
||
public static function get(string $class, string $method): array | ||
{ | ||
if (! empty(static::$attributes[$class][$method])) { | ||
return static::$attributes[$class][$method]; | ||
} | ||
$methodAnnotations = AnnotationCollector::getClassMethodAnnotation($class, $method); | ||
/** @var QueryParameter[] $queryParameters */ | ||
$queryParameters = Util::findAnnotations($methodAnnotations, QueryParameter::class); | ||
|
||
$attributes = []; | ||
foreach ($queryParameters as $parameter) { | ||
if ($parameter->attribute) { | ||
$attributes[$parameter->name] = $parameter->attribute; | ||
} | ||
} | ||
|
||
/** @var RequestBody $body */ | ||
$body = Util::findAnnotations($methodAnnotations, RequestBody::class)[0] ?? null; | ||
if ($body) { | ||
if ($body->_content instanceof JsonContent) { | ||
if (is_array($body->_content->properties)) { | ||
foreach ($body->_content->properties as $property) { | ||
if ($property instanceof Property) { | ||
if ($property->attribute) { | ||
$attributes[$property->property] = $property->attribute; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return static::$attributes[$class][$method] = $attributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters