From b1c05a0242a2289c0750818933ee56c804634cd3 Mon Sep 17 00:00:00 2001 From: Merleur l'enchantin Date: Thu, 2 May 2024 13:46:52 +0200 Subject: [PATCH 1/3] fix `"nullable": false` validation --- src/Schema/Keywords/Nullable.php | 4 +- .../IssueNullablePropertyTest.php | 81 +++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 tests/FromCommunity/IssueNullablePropertyTest.php diff --git a/src/Schema/Keywords/Nullable.php b/src/Schema/Keywords/Nullable.php index 9569ac27..68c4350f 100644 --- a/src/Schema/Keywords/Nullable.php +++ b/src/Schema/Keywords/Nullable.php @@ -7,7 +7,7 @@ use League\OpenAPIValidation\Schema\Exception\KeywordMismatch; use function in_array; -use function is_string; +use function is_array; class Nullable extends BaseKeyword { @@ -27,6 +27,6 @@ public function validate($data, bool $nullable): void public function nullableByType(): bool { - return ! is_string($this->parentSchema->type) && in_array('null', $this->parentSchema->type); + return is_array($this->parentSchema->type) && in_array('null', $this->parentSchema->type); } } diff --git a/tests/FromCommunity/IssueNullablePropertyTest.php b/tests/FromCommunity/IssueNullablePropertyTest.php new file mode 100644 index 00000000..81ee29c0 --- /dev/null +++ b/tests/FromCommunity/IssueNullablePropertyTest.php @@ -0,0 +1,81 @@ +fromJson($json) + ->getRequestValidator(); + + $request = new Request( + 'POST', + 'headers:/api/data', + ['Content-Type' => 'application/json'], + json_encode( + ['value' => null], + ) + ); + + $this->expectException(InvalidBody::class); + + $requestValidator->validate($request); + } +} From b1c48f92a4c629a3d8495b2b9c200b92e824d658 Mon Sep 17 00:00:00 2001 From: Merleur l'enchantin Date: Thu, 2 May 2024 14:07:19 +0200 Subject: [PATCH 2/3] fix test ? --- .../IssueNullablePropertyTest.php | 83 ++++++++++--------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/tests/FromCommunity/IssueNullablePropertyTest.php b/tests/FromCommunity/IssueNullablePropertyTest.php index 81ee29c0..f5c4263c 100644 --- a/tests/FromCommunity/IssueNullablePropertyTest.php +++ b/tests/FromCommunity/IssueNullablePropertyTest.php @@ -15,51 +15,52 @@ final class IssueNullablePropertyTest extends BaseValidatorTest { public function testNullableFalseThrowInvalidBody(): void { - $json = <<fromJson($json) From 74edcec24228d7605932cbc66a3bb0afe53169ac Mon Sep 17 00:00:00 2001 From: Merleur l'enchantin Date: Thu, 2 May 2024 14:12:59 +0200 Subject: [PATCH 3/3] fix test for older PHP version --- tests/FromCommunity/IssueNullablePropertyTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/FromCommunity/IssueNullablePropertyTest.php b/tests/FromCommunity/IssueNullablePropertyTest.php index f5c4263c..3086e36c 100644 --- a/tests/FromCommunity/IssueNullablePropertyTest.php +++ b/tests/FromCommunity/IssueNullablePropertyTest.php @@ -70,9 +70,7 @@ public function testNullableFalseThrowInvalidBody(): void 'POST', 'headers:/api/data', ['Content-Type' => 'application/json'], - json_encode( - ['value' => null], - ) + json_encode(['value' => null]) ); $this->expectException(InvalidBody::class);