-
-
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 collect swagger validation rules and attribute for mediaType …
…request body. (#6380)
- Loading branch information
1 parent
e3e7f2c
commit 2776915
Showing
7 changed files
with
294 additions
and
112 deletions.
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 was deleted.
Oops, something went wrong.
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,111 @@ | ||
<?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\MediaType; | ||
use Hyperf\Swagger\Annotation\Property; | ||
use Hyperf\Swagger\Annotation\QueryParameter; | ||
use Hyperf\Swagger\Annotation\RequestBody; | ||
use Hyperf\Swagger\Util; | ||
|
||
class ValidationCollector | ||
{ | ||
protected static array $data = []; | ||
|
||
/** | ||
* @param string $field rules or attribute | ||
*/ | ||
public static function get(string $class, string $method, string $field): array | ||
{ | ||
if (! empty(static::$data[$class][$method][$field])) { | ||
return static::$data[$class][$method][$field]; | ||
} | ||
|
||
$methodAnnotations = AnnotationCollector::getClassMethodAnnotation($class, $method); | ||
|
||
$data = []; | ||
$data = self::collectQueryParameter($methodAnnotations, $data, $field); | ||
$data = self::collectJsonContentRequestBody($methodAnnotations, $data, $field); | ||
$data = self::collectMediaTypeRequestBody($methodAnnotations, $data, $field); | ||
|
||
return static::$data[$class][$method][$field] = $data; | ||
} | ||
|
||
protected static function collectQueryParameter($methodAnnotations, array $data, string $field): array | ||
{ | ||
/** @var QueryParameter[] $queryParameters */ | ||
$queryParameters = Util::findAnnotations($methodAnnotations, QueryParameter::class); | ||
|
||
foreach ($queryParameters as $parameter) { | ||
if (isset($property->{$field}) && $property->{$field}) { | ||
$data[$parameter->name] = $parameter->{$field}; | ||
} | ||
} | ||
return $data; | ||
} | ||
|
||
protected static function collectJsonContentRequestBody($methodAnnotations, array $data, string $field): array | ||
{ | ||
/** @var RequestBody $body */ | ||
$body = Util::findAnnotations($methodAnnotations, RequestBody::class)[0] ?? null; | ||
if (! $body) { | ||
return $data; | ||
} | ||
|
||
if (! $body->_content instanceof JsonContent) { | ||
return $data; | ||
} | ||
|
||
if (! is_array($body->_content->properties)) { | ||
return $data; | ||
} | ||
|
||
foreach ($body->_content->properties as $property) { | ||
if ($property instanceof Property) { | ||
if (isset($property->{$field}) && $property->{$field}) { | ||
$data[$property->property] = $property->{$field}; | ||
} | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
protected static function collectMediaTypeRequestBody($methodAnnotations, array $data, string $field): array | ||
{ | ||
/** @var RequestBody $body */ | ||
$body = Util::findAnnotations($methodAnnotations, RequestBody::class)[0] ?? null; | ||
if (! $body || ! is_array($body->content)) { | ||
return $data; | ||
} | ||
|
||
foreach ($body->content as $content) { | ||
if (! $content instanceof MediaType) { | ||
continue; | ||
} | ||
if (! $content->schema || ! is_array($content->schema->properties)) { | ||
continue; | ||
} | ||
foreach ($content->schema->properties as $property) { | ||
if (! $property instanceof Property) { | ||
continue; | ||
} | ||
if (isset($property->{$field}) && $property->{$field}) { | ||
$data[$property->property] = $property->{$field}; | ||
} | ||
} | ||
} | ||
return $data; | ||
} | ||
} |
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,61 @@ | ||
<?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 HyperfTest\Swagger\Stub; | ||
|
||
use Hyperf\Swagger\Annotation as SA; | ||
use Hyperf\Swagger\Request\SwaggerRequest; | ||
|
||
class ExampleController | ||
{ | ||
#[SA\Post('/hyperf/example/index', summary: '单测 index', tags: ['hyperf'])] | ||
#[SA\QueryParameter( | ||
name: 'token', | ||
description: 'token', | ||
example: 'token', | ||
rules: 'required|string|max:25', | ||
)] | ||
#[SA\RequestBody( | ||
description: '请求参数', | ||
content: [ | ||
new SA\MediaType( | ||
mediaType: 'application/x-www-form-urlencoded', | ||
schema: new SA\Schema( | ||
properties: [ | ||
new SA\Property(property: 'name', description: '昵称', type: 'string', example: 'user-2', rules: 'required|string|max:3', attribute: 'nickname'), | ||
] | ||
), | ||
), | ||
], | ||
)] | ||
public function index(SwaggerRequest $request): void | ||
{ | ||
} | ||
|
||
#[SA\Post('/hyperf/example/json', summary: '单测 json', tags: ['hyperf'])] | ||
#[SA\QueryParameter( | ||
name: 'token', | ||
description: 'token', | ||
example: 'token', | ||
rules: 'required|string|max:25', | ||
)] | ||
#[SA\RequestBody( | ||
description: '请求参数', | ||
content: new SA\JsonContent( | ||
properties: [ | ||
new SA\Property(property: 'name', description: '昵称', type: 'string', example: 'user-2', rules: 'required|int', attribute: 'json-name'), | ||
] | ||
), | ||
)] | ||
public function json(SwaggerRequest $request): void | ||
{ | ||
} | ||
} |
Oops, something went wrong.