Skip to content

Commit

Permalink
Do not perform further validation on nullable null data
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel.Batanov committed May 13, 2019
1 parent f805092 commit ab8f04a
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 25 deletions.
3 changes: 1 addition & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true">
processIsolation="false">
<testsuites>
<testsuite name="All tests">
<directory suffix="Test.php">./tests</directory>
Expand Down
4 changes: 4 additions & 0 deletions src/Schema/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
79 changes: 79 additions & 0 deletions tests/FromCommunity/Issue12Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* @author Pavel Batanov <[email protected]>
* 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 */
<<<YAML
openapi: 3.0.0
info:
title: Product import API
version: '1.0'
servers:
- url: 'http://localhost:8000/api/v1'
paths:
/products.create:
post:
requestBody:
required: true
content:
application/json:
schema:
properties:
test:
nullable: true
type: array
items:
type: integer
minItems: 1
responses:
'200':
description: OK
content:
application/json:
schema:
properties:
result:
type: string
YAML;

$validator = ServerRequestValidator::fromYaml($yaml);

$psrRequest = (new ServerRequest('post', 'http://localhost:8000/api/v1/products.create'))
->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]],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
<<<YAML
Expand Down Expand Up @@ -60,4 +57,4 @@ function test_issue3()

$this->addToAssertionCount(1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
}

}
}

0 comments on commit ab8f04a

Please sign in to comment.