Skip to content

Commit c3daf7c

Browse files
authored
Fix schema.nullable (#155)
* Fix schema.nullable * Add implicit nullable test
1 parent 54e3744 commit c3daf7c

4 files changed

Lines changed: 136 additions & 2 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"require": {
2424
"php": ">=7.2",
2525
"ext-json": "*",
26-
"cebe/php-openapi": "^1.3",
26+
"cebe/php-openapi": "^1.6",
2727
"league/uri": "^6.3",
2828
"psr/cache": "^1.0 || ^2.0 || ^3.0",
2929
"psr/http-message": "^1.0",

src/Schema/SchemaValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function validate($data, CebeSchema $schema, ?BreadCrumb $breadCrumb = nu
5757

5858
try {
5959
// These keywords are not part of the JSON Schema at all (new to OAS)
60-
(new Nullable($schema))->validate($data, $schema->nullable);
60+
(new Nullable($schema))->validate($data, $schema->nullable ?? true);
6161

6262
// We don't want to validate any more if the value is a valid Null
6363
if ($data === null) {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace League\OpenAPIValidation\Tests\FromCommunity;
6+
7+
use GuzzleHttp\Psr7\Response;
8+
use League\OpenAPIValidation\PSR7\OperationAddress;
9+
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
10+
use League\OpenAPIValidation\Tests\PSR7\BaseValidatorTest;
11+
12+
final class IssueWithNullableMergeTest extends BaseValidatorTest
13+
{
14+
public function testNullableMergeOneOf(): void
15+
{
16+
$yaml = /** @lang yaml */
17+
<<<'YAML'
18+
openapi: 3.0.0
19+
paths:
20+
/api/nullable-merge:
21+
get:
22+
description: 'Test'
23+
responses:
24+
'200':
25+
description: 'ok'
26+
content:
27+
application/json:
28+
schema:
29+
$ref: "#/components/schemas/Thing"
30+
components:
31+
schemas:
32+
FooResult:
33+
type: object
34+
properties:
35+
id:
36+
type: integer
37+
foo:
38+
type: string
39+
BarResult:
40+
type: object
41+
nullable: true
42+
properties:
43+
id:
44+
type: integer
45+
bar:
46+
type: string
47+
Thing:
48+
type: object
49+
properties:
50+
result:
51+
oneOf:
52+
- $ref: "#/components/schemas/FooResult"
53+
- $ref: "#/components/schemas/BarResult"
54+
YAML;
55+
56+
$validator = (new ValidatorBuilder())->fromYaml($yaml)->getResponseValidator();
57+
$operation = new OperationAddress('/api/nullable-merge', 'get');
58+
59+
$responseContent = /** @lang JSON */
60+
'
61+
{
62+
"result": null
63+
}
64+
';
65+
66+
$response = new Response(200, ['Content-Type' => 'application/json'], $responseContent);
67+
68+
$validator->validate($operation, $response);
69+
70+
$this->addToAssertionCount(1);
71+
}
72+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace League\OpenAPIValidation\Tests\FromCommunity;
6+
7+
use GuzzleHttp\Psr7\Response;
8+
use League\OpenAPIValidation\PSR7\OperationAddress;
9+
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
10+
use League\OpenAPIValidation\Tests\PSR7\BaseValidatorTest;
11+
12+
final class NullableSchemaTest extends BaseValidatorTest
13+
{
14+
public function testNullableImplicitResult(): void
15+
{
16+
$yaml = /** @lang yaml */
17+
<<<'YAML'
18+
openapi: 3.0.0
19+
paths:
20+
/api/nullable:
21+
get:
22+
description: 'Test'
23+
responses:
24+
'200':
25+
description: 'ok'
26+
content:
27+
application/json:
28+
schema:
29+
$ref: "#/components/schemas/Thing"
30+
components:
31+
schemas:
32+
FooResult:
33+
properties:
34+
id:
35+
type: integer
36+
foo:
37+
type: string
38+
Thing:
39+
type: object
40+
properties:
41+
result:
42+
schema:
43+
- $ref: "#/components/schemas/FooResult"
44+
YAML;
45+
46+
$validator = (new ValidatorBuilder())->fromYaml($yaml)->getResponseValidator();
47+
$operation = new OperationAddress('/api/nullable', 'get');
48+
49+
$responseContent = /** @lang JSON */
50+
'
51+
{
52+
"result": null
53+
}
54+
';
55+
56+
$response = new Response(200, ['Content-Type' => 'application/json'], $responseContent);
57+
58+
$validator->validate($operation, $response);
59+
60+
$this->addToAssertionCount(1);
61+
}
62+
}

0 commit comments

Comments
 (0)