Merge branch 'master' into 3.1

# Conflicts:
#	src/redis/composer.json
#	src/validation/tests/Cases/ValidateAttributesTest.php
This commit is contained in:
李铭昕 2023-09-14 15:30:36 +08:00
commit 1d6dd5b989
9 changed files with 73 additions and 11 deletions

View File

@ -46,19 +46,21 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Upgrade
run: |
sudo apt-get clean
sudo apt-get update
sudo apt-get upgrade -f
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: phpize
extensions: redis, pdo, pdo_mysql, bcmath
extensions: redis-5.3.7, pdo, pdo_mysql, bcmath
ini-values: opcache.enable_cli=0
coverage: none
- name: Setup Swoole
run: |
sudo apt-get clean
sudo apt-get update
sudo apt-get upgrade -f
sudo apt-get install libcurl4-openssl-dev libc-ares-dev libpq-dev
wget https://github.com/swoole/swoole-src/archive/${SW_VERSION}.tar.gz -O swoole.tar.gz
mkdir -p swoole

View File

@ -3,10 +3,12 @@
# Added
- [#6062](https://github.com/hyperf/hyperf/pull/6057) Added `RequestTraceListener` for `hyperf/tracer`.
- [#6143](https://github.com/hyperf/hyperf/pull/6143) Added `ignore_exceptions` for tracer
## Fixed
- [#6117](https://github.com/hyperf/hyperf/pull/6117) Fixed bug that grpc client cannot able to be reused.
- [#6146](https://github.com/hyperf/hyperf/pull/6146) Fixed bug that `validateJson` cannot work when using php 8.0.
# v3.0.35 - 2023-09-01

View File

@ -17,7 +17,7 @@
},
"require": {
"php": ">=8.1",
"ext-redis": "*",
"ext-redis": "<6.0",
"hyperf/contract": "~3.1.0",
"hyperf/pool": "~3.1.0",
"hyperf/support": "~3.1.0",

View File

@ -21,6 +21,7 @@ return [
'db' => env('TRACER_ENABLE_DB', false),
'method' => env('TRACER_ENABLE_METHOD', false),
'exception' => env('TRACER_ENABLE_EXCEPTION', false),
'ignore_exceptions' => [],
],
'tracer' => [
'zipkin' => [

View File

@ -77,7 +77,7 @@ class RequestTraceListener implements ListenerInterface
$span = TracerContext::getRoot();
$span->setTag($this->spanTagManager->get('response', 'status_code'), $response->getStatusCode());
if ($event->exception && $this->switchManager->isEnable('exception')) {
if ($event->exception && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($event->exception::class)) {
$this->appendExceptionToSpan($span, $exception = $event->exception);
if ($exception instanceof HttpException) {

View File

@ -58,7 +58,9 @@ class TraceMiddleware implements MiddlewareInterface
}
$span->setTag($this->spanTagManager->get('response', 'status_code'), $response->getStatusCode());
} catch (Throwable $exception) {
$this->switchManager->isEnable('exception') && $this->appendExceptionToSpan($span, $exception);
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($exception::class)) {
$this->appendExceptionToSpan($span, $exception);
}
if ($exception instanceof HttpException) {
$span->setTag($this->spanTagManager->get('response', 'status_code'), $exception->getStatusCode());
}

View File

@ -27,6 +27,7 @@ class SwitchManager
// beta feature, please don't enable 'method' in production environment
'method' => false,
'error' => false,
'ignore_exceptions' => [],
];
/**
@ -48,4 +49,15 @@ class SwitchManager
return $this->config[$identifier] && Context::get('tracer.root') instanceof Span;
}
public function isIgnoreException(string $className): bool
{
$ignoreExceptions = $this->config['ignore_exceptions'] ?? [];
foreach ($ignoreExceptions as $ignoreException) {
if (is_a($className, $ignoreException, true)) {
return true;
}
}
return false;
}
}

View File

@ -29,6 +29,7 @@ use Hyperf\Validation\Rules\Unique;
use Hyperf\Validation\ValidationData;
use InvalidArgumentException;
use SplFileInfo;
use Stringable;
use Throwable;
use function Hyperf\Collection\last;
@ -888,13 +889,17 @@ trait ValidatesAttributes
*/
public function validateJson(string $attribute, $value): bool
{
if (! is_scalar($value) && ! method_exists($value, '__toString')) {
if ($value instanceof Stringable) {
$value = (string) $value;
}
try {
json_decode($value, flags: JSON_THROW_ON_ERROR);
} catch (Throwable) {
return false;
}
json_decode($value);
return json_last_error() === JSON_ERROR_NONE;
return true;
}
/**

View File

@ -14,6 +14,7 @@ namespace HyperfTest\Validation\Cases;
use HyperfTest\Validation\Cases\Stub\ValidatesAttributesStub;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Stringable;
/**
* @internal
@ -93,4 +94,41 @@ class ValidateAttributesTest extends TestCase
$this->assertFalse($validator->validateAscii('', 'ユニコードを基盤技術と-_123'));
$this->assertFalse($validator->validateAscii('', 'नमस्कार-_'));
}
public function testValidateJson()
{
$validator = new ValidatesAttributesStub();
// null
$this->assertFalse($validator->validateJson('', null));
$this->assertTrue($validator->validateJson('', 'null'));
// int
$this->assertTrue($validator->validateJson('', '3'));
$this->assertFalse($validator->validateJson('', 3));
// float
$this->assertTrue($validator->validateJson('', '3.14'));
$this->assertFalse($validator->validateJson('', 3.14));
// string
$this->assertFalse($validator->validateJson('', 'plain_text'));
$this->assertTrue($validator->validateJson('', '{"foo": "bar"}'));
$this->assertFalse($validator->validateJson('', '{"foo": "bar",a}'));
// array
$this->assertTrue($validator->validateJson('', '[3.14]'));
$this->assertFalse($validator->validateJson('', [3.14]));
$this->assertTrue($validator->validateJson('', '["a"]'));
// object
$this->assertFalse($validator->validateJson('', new class() {}));
$this->assertTrue($validator->validateJson('', new class() implements Stringable {
public function __toString(): string
{
return json_encode(['foo' => 'bar'], JSON_UNESCAPED_UNICODE);
}
}));
$this->assertTrue($validator->validateJson('', new class() {
public function __toString(): string
{
return json_encode(['foo' => 'bar'], JSON_UNESCAPED_UNICODE);
}
}));
}
}