Skip to content

Commit

Permalink
Fix schema.nullable (#155)
Browse files Browse the repository at this point in the history
* Fix schema.nullable

* Add implicit nullable test
  • Loading branch information
scaytrase authored Feb 10, 2022
1 parent 54e3744 commit c3daf7c
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/SchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
72 changes: 72 additions & 0 deletions tests/FromCommunity/IssueWithNullableMergeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace League\OpenAPIValidation\Tests\FromCommunity;

use GuzzleHttp\Psr7\Response;
use League\OpenAPIValidation\PSR7\OperationAddress;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use League\OpenAPIValidation\Tests\PSR7\BaseValidatorTest;

final class IssueWithNullableMergeTest extends BaseValidatorTest
{
public function testNullableMergeOneOf(): void
{
$yaml = /** @lang yaml */
<<<'YAML'
openapi: 3.0.0
paths:
/api/nullable-merge:
get:
description: 'Test'
responses:
'200':
description: 'ok'
content:
application/json:
schema:
$ref: "#/components/schemas/Thing"
components:
schemas:
FooResult:
type: object
properties:
id:
type: integer
foo:
type: string
BarResult:
type: object
nullable: true
properties:
id:
type: integer
bar:
type: string
Thing:
type: object
properties:
result:
oneOf:
- $ref: "#/components/schemas/FooResult"
- $ref: "#/components/schemas/BarResult"
YAML;

$validator = (new ValidatorBuilder())->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);
}
}
62 changes: 62 additions & 0 deletions tests/FromCommunity/NullableSchemaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace League\OpenAPIValidation\Tests\FromCommunity;

use GuzzleHttp\Psr7\Response;
use League\OpenAPIValidation\PSR7\OperationAddress;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use League\OpenAPIValidation\Tests\PSR7\BaseValidatorTest;

final class NullableSchemaTest extends BaseValidatorTest
{
public function testNullableImplicitResult(): void
{
$yaml = /** @lang yaml */
<<<'YAML'
openapi: 3.0.0
paths:
/api/nullable:
get:
description: 'Test'
responses:
'200':
description: 'ok'
content:
application/json:
schema:
$ref: "#/components/schemas/Thing"
components:
schemas:
FooResult:
properties:
id:
type: integer
foo:
type: string
Thing:
type: object
properties:
result:
schema:
- $ref: "#/components/schemas/FooResult"
YAML;

$validator = (new ValidatorBuilder())->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);
}
}

0 comments on commit c3daf7c

Please sign in to comment.