mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Fixed bug that validateJson was incompatibility with php 8.0 (#6146)
* Fixed bug that `validateJson` cannot work when using php 8.0. * Fixed unit cases for redis cannot work. Co-authored-by: 李铭昕 <715557344@qq.com> Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
This commit is contained in:
parent
cfe11e3af3
commit
aa5d2bb629
10
.github/workflows/test.yml
vendored
10
.github/workflows/test.yml
vendored
@ -27,19 +27,21 @@ jobs:
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v2
|
||||
- name: Update Core
|
||||
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
|
||||
|
@ -8,6 +8,7 @@
|
||||
## 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
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.0",
|
||||
"ext-redis": "*",
|
||||
"ext-redis": "<6.0",
|
||||
"hyperf/contract": "~3.0.0",
|
||||
"hyperf/pool": "~3.0.0",
|
||||
"hyperf/support": "~3.0.0",
|
||||
|
@ -27,6 +27,7 @@ use Hyperf\Validation\Rules\Unique;
|
||||
use Hyperf\Validation\ValidationData;
|
||||
use InvalidArgumentException;
|
||||
use SplFileInfo;
|
||||
use Stringable;
|
||||
use Throwable;
|
||||
|
||||
use function Hyperf\Collection\last;
|
||||
@ -743,13 +744,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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -13,6 +13,7 @@ namespace HyperfTest\Validation\Cases;
|
||||
|
||||
use HyperfTest\Validation\Cases\Stub\ValidatesAttributesStub;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Stringable;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
@ -68,4 +69,41 @@ class ValidateAttributesTest extends TestCase
|
||||
|
||||
$this->assertFalse($validator->validateDate('', 123));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user