Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your fix should also work with older versions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test from #146 fails because of lack of logic invented here cebe/php-openapi#132
So in order to make this test pass - we should have ^1.6.0

"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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I didn't understand why we want nullable true by default instead of false ?)


// 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);
}
}