From 047037f9b80fa0ae599d5e5aef540832a1ededd0 Mon Sep 17 00:00:00 2001 From: Pavel Batanov Date: Thu, 10 Feb 2022 13:46:39 +0300 Subject: [PATCH 1/2] Fix schema.nullable --- composer.json | 2 +- src/Schema/SchemaValidator.php | 2 +- .../IssueWithNullableMergeTest.php | 72 +++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 tests/FromCommunity/IssueWithNullableMergeTest.php diff --git a/composer.json b/composer.json index 3f573380..81e0ebe3 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "require": { "php": ">=7.2", "ext-json": "*", - "cebe/php-openapi": "^1.3", + "cebe/php-openapi": "^1.6", "league/uri": "^6.3", "psr/cache": "^1.0 || ^2.0 || ^3.0", "psr/http-message": "^1.0", diff --git a/src/Schema/SchemaValidator.php b/src/Schema/SchemaValidator.php index 55471b44..f1f75937 100644 --- a/src/Schema/SchemaValidator.php +++ b/src/Schema/SchemaValidator.php @@ -57,7 +57,7 @@ public function validate($data, CebeSchema $schema, ?BreadCrumb $breadCrumb = nu try { // These keywords are not part of the JSON Schema at all (new to OAS) - (new Nullable($schema))->validate($data, $schema->nullable); + (new Nullable($schema))->validate($data, $schema->nullable ?? true); // We don't want to validate any more if the value is a valid Null if ($data === null) { diff --git a/tests/FromCommunity/IssueWithNullableMergeTest.php b/tests/FromCommunity/IssueWithNullableMergeTest.php new file mode 100644 index 00000000..9a73f0d5 --- /dev/null +++ b/tests/FromCommunity/IssueWithNullableMergeTest.php @@ -0,0 +1,72 @@ +fromYaml($yaml)->getResponseValidator(); + $operation = new OperationAddress('/api/nullable-merge', 'get'); + + $responseContent = /** @lang JSON */ + ' + { + "result": null + } +'; + + $response = new Response(200, ['Content-Type' => 'application/json'], $responseContent); + + $validator->validate($operation, $response); + + $this->addToAssertionCount(1); + } +} From bd249947778bd91315088b07516298d63fa2de15 Mon Sep 17 00:00:00 2001 From: Pavel Batanov Date: Thu, 10 Feb 2022 14:10:07 +0300 Subject: [PATCH 2/2] Add implicit nullable test --- tests/FromCommunity/NullableSchemaTest.php | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/FromCommunity/NullableSchemaTest.php diff --git a/tests/FromCommunity/NullableSchemaTest.php b/tests/FromCommunity/NullableSchemaTest.php new file mode 100644 index 00000000..223b0004 --- /dev/null +++ b/tests/FromCommunity/NullableSchemaTest.php @@ -0,0 +1,62 @@ +fromYaml($yaml)->getResponseValidator(); + $operation = new OperationAddress('/api/nullable', 'get'); + + $responseContent = /** @lang JSON */ + ' +{ + "result": null +} +'; + + $response = new Response(200, ['Content-Type' => 'application/json'], $responseContent); + + $validator->validate($operation, $response); + + $this->addToAssertionCount(1); + } +}