Skip to content

Commit

Permalink
Support to read custom attribute for validation when using swagger. (…
Browse files Browse the repository at this point in the history
…#6379)
  • Loading branch information
limingxinleo authored Dec 12, 2023
1 parent cfc1647 commit e3e7f2c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Annotation/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function __construct(
AdditionalProperties|bool|null $additionalProperties = null,
?array $x = null,
?array $attachables = null,
public mixed $rules = null
public mixed $rules = null,
public mixed $attribute = null
) {
parent::__construct(
$property,
Expand Down
1 change: 1 addition & 0 deletions src/Annotation/QueryParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function __construct(
?array $x = null,
?array $attachables = null,
public mixed $rules = null,
public mixed $attribute = null,
) {
parent::__construct(
$parameter,
Expand Down
59 changes: 59 additions & 0 deletions src/Request/AttributeCollector.php
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;
}
}
16 changes: 16 additions & 0 deletions src/Request/SwaggerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,20 @@ public function rules(): array

return RuleCollector::get($callback[0], $callback[1]);
}

public function attributes(): array
{
/** @var Dispatched $dispatched */
$dispatched = RequestContext::getOrNull()?->getAttribute(Dispatched::class);
if (! $dispatched) {
throw new RuntimeException('The request is invalid.');
}

$callback = $dispatched->handler?->callback;
if (! $callback || ! is_array($callback)) {
throw new RuntimeException('The SwaggerRequest is only used by swagger annotations.');
}

return AttributeCollector::get($callback[0], $callback[1]);
}
}

0 comments on commit e3e7f2c

Please sign in to comment.