From ab8f04a5ef0df19988cd5e79eb9ac7952a1aed32 Mon Sep 17 00:00:00 2001 From: "Pavel.Batanov" Date: Mon, 13 May 2019 12:39:33 +0300 Subject: [PATCH] Do not perform further validation on nullable null data --- phpunit.xml | 3 +- src/Schema/Validator.php | 4 + tests/FromCommunity/Issue12Test.php | 79 +++++++++++++++++++ .../{ScayTraseTest.php => Issue3Test.php} | 9 +-- ...{ScayReferenceIssue.php => Issue4Test.php} | 31 ++++---- 5 files changed, 101 insertions(+), 25 deletions(-) create mode 100644 tests/FromCommunity/Issue12Test.php rename tests/FromCommunity/{ScayTraseTest.php => Issue3Test.php} (93%) rename tests/FromCommunity/{ScayReferenceIssue.php => Issue4Test.php} (73%) diff --git a/phpunit.xml b/phpunit.xml index 4e4ff438..98e9a264 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -6,8 +6,7 @@ convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" - processIsolation="false" - stopOnFailure="true"> + processIsolation="false"> ./tests diff --git a/src/Schema/Validator.php b/src/Schema/Validator.php index d94f98af..abb426a5 100644 --- a/src/Schema/Validator.php +++ b/src/Schema/Validator.php @@ -64,6 +64,10 @@ public function __construct(CebeSchema $schema, $data, int $validationStrategy = public function validate() : void { try { + if ($this->schema->nullable && $this->data === null) { + return; + } + // These keywords are not part of the JSON Schema at all (new to OAS) (new Nullable($this->schema))->validate($this->data, $this->schema->nullable); diff --git a/tests/FromCommunity/Issue12Test.php b/tests/FromCommunity/Issue12Test.php new file mode 100644 index 00000000..b76582f9 --- /dev/null +++ b/tests/FromCommunity/Issue12Test.php @@ -0,0 +1,79 @@ + + * Date: 13 May 2019 + */ +declare(strict_types=1); + +namespace OpenAPIValidationTests\FromCommunity; + +use GuzzleHttp\Psr7\ServerRequest; +use OpenAPIValidation\PSR7\ServerRequestValidator; +use PHPUnit\Framework\TestCase; +use function GuzzleHttp\Psr7\stream_for; + +final class Issue12Test extends TestCase +{ + /** + * https://github.com/lezhnev74/openapi-psr7-validator/issues/12 + * + * @dataProvider getNullableTypeExamples + * + * @param $example + */ + public function test_issue12($example): void + { + $yaml = /** @lang yaml */ + <<withHeader('Content-Type', 'application/json') + ->withBody(stream_for(json_encode(['test' => $example]))); + + $validator->validate($psrRequest); + + $this->addToAssertionCount(1); + } + + public function getNullableTypeExamples(): array + { + return [ + 'nullable null' => [null], + 'nullable array' => [[123]], + ]; + } +} diff --git a/tests/FromCommunity/ScayTraseTest.php b/tests/FromCommunity/Issue3Test.php similarity index 93% rename from tests/FromCommunity/ScayTraseTest.php rename to tests/FromCommunity/Issue3Test.php index ce424017..e35dad2c 100644 --- a/tests/FromCommunity/ScayTraseTest.php +++ b/tests/FromCommunity/Issue3Test.php @@ -5,20 +5,17 @@ */ declare(strict_types=1); - namespace OpenAPIValidationTests\FromCommunity; - -use cebe\openapi\Reader; use GuzzleHttp\Psr7\ServerRequest; use OpenAPIValidation\PSR7\ServerRequestValidator; use PHPUnit\Framework\TestCase; use function GuzzleHttp\Psr7\stream_for; -class ScayTraseTest extends TestCase +class Issue3Test extends TestCase { # https://github.com/lezhnev74/openapi-psr7-validator/issues/3 - function test_issue3() + public function test_issue3(): void { $yaml = /** @lang yaml */ <<addToAssertionCount(1); } -} \ No newline at end of file +} diff --git a/tests/FromCommunity/ScayReferenceIssue.php b/tests/FromCommunity/Issue4Test.php similarity index 73% rename from tests/FromCommunity/ScayReferenceIssue.php rename to tests/FromCommunity/Issue4Test.php index 44dad495..2d154327 100644 --- a/tests/FromCommunity/ScayReferenceIssue.php +++ b/tests/FromCommunity/Issue4Test.php @@ -5,27 +5,34 @@ */ declare(strict_types=1); - namespace OpenAPIValidationTests\FromCommunity; - use GuzzleHttp\Psr7\ServerRequest; use OpenAPIValidation\PSR7\ServerRequestValidator; use PHPUnit\Framework\TestCase; # @see https://github.com/lezhnev74/openapi-psr7-validator/issues/4 -class ScayReferenceIssue extends TestCase +class Issue4Test extends TestCase { - function test_it_resolves_schema_refs_from_yaml_string_green() + public function test_it_resolves_schema_refs_from_yaml_string_green(): void { - $yamlFile = __DIR__ . "/../stubs/SchemaWithRefs.yaml"; + $yamlFile = __DIR__ . "/../stubs/SchemaWithRefs.yaml"; $validator = ServerRequestValidator::fromYamlFile($yamlFile); $validator->validate($this->makeRequest()); $this->addToAssertionCount(1); } - protected function makeRequest() + public function test_it_resolves_schema_refs_from_yaml_file_green(): void + { + $yamlFile = __DIR__ . "/../stubs/SchemaWithRefs.yaml"; + $validator = ServerRequestValidator::fromYaml(file_get_contents($yamlFile)); + + $validator->validate($this->makeRequest()); + $this->addToAssertionCount(1); + } + + protected function makeRequest(): ServerRequest { return new ServerRequest( 'POST', @@ -42,14 +49,4 @@ protected function makeRequest() JSON ); } - - function test_it_resolves_schema_refs_from_yaml_file_green() - { - $yamlFile = __DIR__ . "/../stubs/SchemaWithRefs.yaml"; - $validator = ServerRequestValidator::fromYaml(file_get_contents($yamlFile)); - - $validator->validate($this->makeRequest()); - $this->addToAssertionCount(1); - } - -} \ No newline at end of file +}